Skip to content

Commit

Permalink
Prevent introspection on compile (#98)
Browse files Browse the repository at this point in the history
* Preliminary changes to handle compile not connecting to the warehouse for runtime calls

* Conditionally handles introspective query failures based on env var

* Removes test logic

* Fixes formatting

* Removes unused vars

* Fixes typo
  • Loading branch information
racheldaniel authored Sep 26, 2022
1 parent f30bd52 commit 8a2b8ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions dbt_server/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ class StateNotFoundException(InvalidRequestException):

class dbtCoreCompilationException(InvalidRequestException):
pass


class UnsupportedQueryException(InvalidRequestException):
pass
16 changes: 16 additions & 0 deletions dbt_server/services/dbt_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
InvalidConfigurationException,
InternalException,
dbtCoreCompilationException,
UnsupportedQueryException,
)
from dbt_server import tracer

Expand All @@ -28,10 +29,16 @@
)

from dbt_server.logging import GLOBAL_LOGGER as logger
from dbt.exceptions import InvalidConnectionException


# Temporary default to match dbt-cloud behavior
PROFILE_NAME = os.getenv("DBT_PROFILE_NAME", "user")
ALLOW_INTROSPECTION = str(os.environ.get("__DBT_ALLOW_INTROSPECTION", "1")).lower() in (
"true",
"1",
"on",
)

CONFIG_GLOBAL_LOCK = threading.Lock()

Expand Down Expand Up @@ -169,6 +176,15 @@ def execute_sql(manifest, project_path, sql):
def compile_sql(manifest, project_path, sql):
try:
result = dbt_compile_sql(manifest, project_path, sql)
except InvalidConnectionException:
if ALLOW_INTROSPECTION:
# Raise original error if introspection is not disabled
# and therefore errors are unexpected
raise
else:
msg = "This dbt server environment does not support introspective queries. \n Hint: typically introspective queries use the 'run_query' jinja command or a macro that invokes that command"
logger.exception(msg)
raise UnsupportedQueryException(msg)
except CompilationException as e:
logger.error(
f"Failed to compile sql at {project_path}. Compilation Error: {repr(e)}"
Expand Down
6 changes: 2 additions & 4 deletions dbt_server/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from collections import deque
import dbt.events.functions
import os
import signal

from collections import deque
from sse_starlette.sse import EventSourceResponse
from fastapi import FastAPI, BackgroundTasks, Depends, status
from fastapi.exceptions import RequestValidationError
Expand All @@ -26,9 +27,6 @@
InternalException,
StateNotFoundException,
)

import dbt.events.functions

from dbt_server.logging import GLOBAL_LOGGER as logger

# ORM stuff
Expand Down

0 comments on commit 8a2b8ad

Please sign in to comment.