Skip to content

Commit 4fd04c6

Browse files
committed
WIP
1 parent 5b75b84 commit 4fd04c6

1 file changed

Lines changed: 21 additions & 45 deletions

File tree

src/dbt_core_interface/project.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -324,32 +324,24 @@ def source(self, source_name: str, table_name: str) -> SourceDefinition | None:
324324
"""
325325
return self.manifest.source_lookup.find(f"{source_name}.{table_name}", None, self.manifest)
326326

327-
def execute_sql(self, sql: str) -> ExecutionResult:
328-
"""Execute Jinja SQL against the database."""
329-
with self._manifest_lock:
330-
temp_node, cleanup = self._create_temp_node(sql)
331-
try:
332-
compiled_result = self.compile_from_node(temp_node, update_depends_on=False)
333-
response, table = self.adapter.execute(
334-
compiled_result.compiled_code, auto_begin=False, fetch=True
335-
)
336-
return ExecutionResult(
337-
adapter_response=response, # pyright: ignore[reportUnknownArgumentType]
338-
table=table, # pyright: ignore[reportUnknownArgumentType]
339-
raw_code=sql,
340-
compiled_code=compiled_result.compiled_code,
341-
)
342-
finally:
343-
cleanup()
327+
def execute_sql(self, sql: str, compile: bool = True) -> ExecutionResult:
328+
"""Execute SQL against the database via the adapter optionally compiling it."""
329+
raw_code = compiled_code = sql
330+
if compile:
331+
with self._manifest_lock:
332+
temp_node, cleanup = self._create_temp_node(sql)
333+
try:
334+
compiled_result = self.compile_node(temp_node, update_depends_on=False)
335+
compiled_code = compiled_result.compiled_code
336+
finally:
337+
cleanup()
344338

345-
def execute_raw_sql(self, sql: str) -> ExecutionResult:
346-
"""Execute raw SQL against the database."""
347-
response, table = self.adapter.execute(sql, auto_begin=False, fetch=True)
339+
response, table = self.adapter.execute(compiled_code, auto_begin=False, fetch=True)
348340
return ExecutionResult(
349341
adapter_response=response, # pyright: ignore[reportUnknownArgumentType]
350342
table=table, # pyright: ignore[reportUnknownArgumentType]
351-
raw_code=sql,
352-
compiled_code=sql,
343+
raw_code=raw_code,
344+
compiled_code=compiled_code,
353345
)
354346

355347
def compile_sql(self, sql: str) -> CompilationResult:
@@ -362,17 +354,15 @@ def compile_sql(self, sql: str) -> CompilationResult:
362354
with self._manifest_lock:
363355
temp_node, cleanup = self._create_temp_node(sql)
364356
try:
365-
response = self.compile_from_node(temp_node, update_depends_on=False)
357+
response = self.compile_node(temp_node, update_depends_on=False)
366358
self.__compilation_cache[sql] = response
367359
if len(self.__compilation_cache) > 128:
368360
_ = self.__compilation_cache.pop(next(iter(self.__compilation_cache)))
369361
return response
370362
finally:
371363
cleanup()
372364

373-
def compile_from_node(
374-
self, node: ManifestNode, update_depends_on: bool = True
375-
) -> CompilationResult:
365+
def compile_node(self, node: ManifestNode, update_depends_on: bool = True) -> CompilationResult:
376366
"""Compile a manifest node."""
377367
with contextlib.suppress(Exception):
378368
node.compiled_code = None # pyright: ignore[reportAttributeAccessIssue]
@@ -453,20 +443,6 @@ def macro_parser(self) -> SqlMacroParser:
453443
)
454444
return self._macro_parser
455445

456-
def recycle_adapter(self) -> None:
457-
"""Force refresh the adapter (useful for long-running processes)."""
458-
with self._adapter_lock:
459-
if self._adapter:
460-
try:
461-
self._adapter.connections.cleanup_all()
462-
except Exception as e:
463-
logger.warning(f"Error during adapter cleanup: {e}")
464-
self._adapter = None
465-
self._adapter_created_at = 0
466-
467-
_ = self.adapter
468-
logger.info("Adapter refreshed")
469-
470446
def get_node_by_path(self, path: Path | str) -> ManifestNode | None:
471447
"""Get a node by its path on disk."""
472448
path = Path(path)
@@ -546,7 +522,7 @@ def update_nodes_by_paths(self, *paths: Path | str) -> dict[str, bool]:
546522
results[str(path)] = self.update_node_from_content(path)
547523
return results
548524

549-
def detect_new_files(self) -> list[Path]:
525+
def detect_new_project_resources(self) -> list[Path]:
550526
"""Detect new dbt files that aren't in the manifest."""
551527
known_files = set()
552528

@@ -564,10 +540,10 @@ def detect_new_files(self) -> list[Path]:
564540

565541
return discovered
566542

567-
def add_new_files(self, *paths: Path | str) -> dict[str, bool]:
543+
def add_project_resources(self, *paths: Path | str) -> dict[str, bool]:
568544
"""Add new files to the manifest."""
569545
if not paths:
570-
paths = tuple(self.detect_new_files())
546+
paths = tuple(self.detect_new_project_resources())
571547

572548
results = {}
573549
for path in paths:
@@ -759,10 +735,10 @@ def _check_for_changes(self) -> None:
759735
def _check_for_new_files(self) -> None:
760736
"""Check for new dbt files."""
761737
try:
762-
added_files = self.project.detect_new_files()
738+
added_files = self.project.detect_new_project_resources()
763739
if added_files:
764740
logger.info(f"Detected {len(added_files)} new files")
765-
results = self.project.add_new_files(*added_files)
741+
results = self.project.add_project_resources(*added_files)
766742

767743
for path, success in results.items():
768744
if success:

0 commit comments

Comments
 (0)