Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from pathlib import Path

from sqlbuild.adapter.discovery.main.resolve_adapter import resolve_adapter


def adapter_supports_session_warehouse(*, adapter_name: str, project_dir: Path | None) -> bool:
"""Return whether the adapter, resolved like any project adapter, has a session warehouse."""

from sqlbuild.adapter.discovery.main.resolve_adapter import resolve_adapter

return resolve_adapter(
adapter_name=adapter_name, project_dir=project_dir
).supports_session_warehouse
2 changes: 1 addition & 1 deletion src/sqlbuild/cli/commands/_helpers/compile/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
from sqlbuild.cli.commands.types import CompileLineageMode
from sqlbuild.cli.compile.models import (
CompileAnalysis,
CompileProfileFlags,
CompileWriteResult,
)
from sqlbuild.cli.entry.models import CompileProfileFlags
from sqlbuild.cli.output.models import (
WrittenTarget,
)
Expand Down
6 changes: 4 additions & 2 deletions src/sqlbuild/cli/commands/_helpers/debug/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from sqlbuild.cli.commands._helpers.runtime.connection import (
resolve_project_connection_config,
)
from sqlbuild.cli.commands._helpers.runtime.warehouses import (
apply_command_warehouse,
from sqlbuild.cli.commands._helpers.runtime.warehouse_scope import (
command_warehouse_scope,
current_command_warehouse_scope,
)
from sqlbuild.cli.commands._helpers.runtime.warehouses import (
apply_command_warehouse,
find_target_group_warehouse,
resolve_command_warehouse,
)
Expand Down
24 changes: 12 additions & 12 deletions src/sqlbuild/cli/commands/_helpers/entry/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from sqlbuild.cli.commands._helpers.diff.validation import parse_diff_name_range
from sqlbuild.cli.commands._helpers.entry.parsing import read_selector_file_inputs
from sqlbuild.cli.commands._helpers.lint.positional_paths import format_path_selectors
from sqlbuild.cli.commands._helpers.runtime.warehouses import (
command_warehouse_scope,
validate_cli_warehouse,
)
from sqlbuild.cli.commands._helpers.runtime.warehouse_scope import command_warehouse_scope
from sqlbuild.cli.commands.classes.cli_namespace import CliNamespace
from sqlbuild.cli.commands.constants import (
DBT_INIT_COMMAND,
Expand All @@ -22,12 +19,10 @@
)
from sqlbuild.cli.commands.exceptions import CliUserError
from sqlbuild.cli.commands.types import CliCommand, CompileLineageMode
from sqlbuild.cli.compile.models import (
CompileCommandRequest,
CompileProfileFlags,
)
from sqlbuild.cli.entry.models import (
CliEntrypointHandlers,
CompileCommandRequest,
CompileProfileFlags,
SelectorInputs,
)
from sqlbuild.compiler.lineage.types import ColumnLineageMode
Expand All @@ -37,10 +32,15 @@
def dispatch_cli_command(*, args: CliNamespace, handlers: CliEntrypointHandlers) -> int:
"""Route a parsed CLI namespace to its command handler and return its exit code."""

validate_cli_warehouse(
cli_warehouse=args.warehouse,
project_dir=Path.cwd() if args.project_dir is None else Path(args.project_dir).resolve(),
)
if args.warehouse is not None:
from sqlbuild.cli.commands._helpers.runtime.warehouses import validate_cli_warehouse

validate_cli_warehouse(
cli_warehouse=args.warehouse,
project_dir=(
Path.cwd() if args.project_dir is None else Path(args.project_dir).resolve()
),
)
with command_warehouse_scope(
command=args.command, cli_warehouse=args.warehouse, cli_vars=args.vars
):
Expand Down
3 changes: 2 additions & 1 deletion src/sqlbuild/cli/commands/_helpers/refactor/compile_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
)
from sqlbuild.cli.commands.models import RefactorCompile
from sqlbuild.cli.commands.types import CompileLineageMode
from sqlbuild.cli.compile.models import CompileAnalysis, CompileProfileFlags
from sqlbuild.cli.compile.models import CompileAnalysis
from sqlbuild.cli.entry.models import CompileProfileFlags
from sqlbuild.compiler.compile.exceptions import CompileInputError
from sqlbuild.compiler.compile.models import CompileAnalysisSelection, CompilerDiagnostic
from sqlbuild.compiler.compile.types import DiagnosticPhase, DiagnosticSeverity
Expand Down
43 changes: 43 additions & 0 deletions src/sqlbuild/cli/commands/_helpers/runtime/warehouse_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Active command warehouse scope for CLI warehouse connections."""

from __future__ import annotations

from collections.abc import Iterator
from contextlib import contextmanager
from contextvars import ContextVar, Token

from sqlbuild.cli.commands.constants import COMMAND_WAREHOUSE_GROUPS
from sqlbuild.cli.commands.types import CliCommand
from sqlbuild.cli.entry.models import CommandWarehouseScope

_CURRENT_SCOPE: ContextVar[CommandWarehouseScope | None] = ContextVar(
"sqlbuild_command_warehouse_scope", default=None
)


@contextmanager
def command_warehouse_scope(
*,
command: CliCommand | str | None,
cli_warehouse: str | None,
cli_vars: dict[str, object] | None = None,
) -> Iterator[CommandWarehouseScope]:
"""Install the warehouse group, CLI override, and CLI vars for one command invocation."""

scope: CommandWarehouseScope = CommandWarehouseScope(
group=None if command is None else COMMAND_WAREHOUSE_GROUPS.get(CliCommand(command)),
cli_warehouse=cli_warehouse,
cli_vars={} if cli_vars is None else dict(cli_vars),
)
token: Token[CommandWarehouseScope | None] = _CURRENT_SCOPE.set(scope)
try:
yield scope
finally:
_CURRENT_SCOPE.reset(token)


def current_command_warehouse_scope() -> CommandWarehouseScope:
"""Return the active command's warehouse scope, or an empty one outside the CLI."""

scope: CommandWarehouseScope | None = _CURRENT_SCOPE.get()
return scope if scope is not None else CommandWarehouseScope(group=None)
39 changes: 3 additions & 36 deletions src/sqlbuild/cli/commands/_helpers/runtime/warehouses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
from __future__ import annotations

import re
from collections.abc import Iterator
from contextlib import contextmanager
from contextvars import ContextVar, Token
from pathlib import Path

from sqlbuild.adapter.discovery.main.session_warehouse_support import (
adapter_supports_session_warehouse,
)
from sqlbuild.cli.commands._helpers.runtime.warehouse_scope import (
current_command_warehouse_scope,
)
from sqlbuild.cli.commands.constants import (
CLI_WAREHOUSE_SOURCE,
COMMAND_WAREHOUSE_GROUPS,
CONNECTION_DEFAULT_WAREHOUSE_SOURCE,
CONNECTION_WAREHOUSE_SOURCE,
)
from sqlbuild.cli.commands.exceptions import CliUserError
from sqlbuild.cli.commands.types import CliCommand
from sqlbuild.cli.entry.models import (
AuthoredTargetWarehouse,
CommandWarehouseScope,
Expand Down Expand Up @@ -47,37 +45,6 @@
_CONNECTION_SOURCES: frozenset[str] = frozenset(
{CONNECTION_WAREHOUSE_SOURCE, CONNECTION_DEFAULT_WAREHOUSE_SOURCE}
)
_CURRENT_SCOPE: ContextVar[CommandWarehouseScope | None] = ContextVar(
"sqlbuild_command_warehouse_scope", default=None
)


@contextmanager
def command_warehouse_scope(
*,
command: CliCommand | str | None,
cli_warehouse: str | None,
cli_vars: dict[str, object] | None = None,
) -> Iterator[CommandWarehouseScope]:
"""Install the warehouse group, CLI override, and CLI vars for one command invocation."""

scope: CommandWarehouseScope = CommandWarehouseScope(
group=None if command is None else COMMAND_WAREHOUSE_GROUPS.get(CliCommand(command)),
cli_warehouse=cli_warehouse,
cli_vars={} if cli_vars is None else dict(cli_vars),
)
token: Token[CommandWarehouseScope | None] = _CURRENT_SCOPE.set(scope)
try:
yield scope
finally:
_CURRENT_SCOPE.reset(token)


def current_command_warehouse_scope() -> CommandWarehouseScope:
"""Return the active command's warehouse scope, or an empty one outside the CLI."""

scope: CommandWarehouseScope | None = _CURRENT_SCOPE.get()
return scope if scope is not None else CommandWarehouseScope(group=None)


def validate_cli_warehouse(*, cli_warehouse: str | None, project_dir: Path) -> None:
Expand Down
11 changes: 7 additions & 4 deletions src/sqlbuild/cli/commands/main/entrypoint/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import argparse
from collections.abc import Mapping, Sequence

from sqlbuild.cli.commands._helpers.entry.dispatch_errors import dispatch_and_handle_errors
from sqlbuild.cli.commands._helpers.entry.errors import cli_error_use_color
from sqlbuild.cli.commands._helpers.entry.lazy_handlers import build_lazy_cli_handlers
from sqlbuild.cli.commands._helpers.entry.parser import build_cli_parser
from sqlbuild.cli.commands._helpers.entry.parsing import parse_cli_invocation
from sqlbuild.cli.commands.classes.cli_namespace import CliNamespace
from sqlbuild.cli.commands.main.entrypoint._dispatch_with_compute_logs import (
dispatch_with_compute_logs,
)
from sqlbuild.cli.entry.models import (
CliEntrypointHandlers,
ParsedCliInvocation,
Expand Down Expand Up @@ -58,6 +54,13 @@ def _main_with_dependencies(
if invocation.args is None:
return invocation.exit_code if invocation.exit_code is not None else 1
args: CliNamespace = invocation.args
from sqlbuild.cli.commands._helpers.entry.dispatch_errors import (
dispatch_and_handle_errors,
)
from sqlbuild.cli.commands.main.entrypoint._dispatch_with_compute_logs import (
dispatch_with_compute_logs,
)

return dispatch_with_compute_logs(
args=args,
identity=identity,
Expand Down
2 changes: 1 addition & 1 deletion src/sqlbuild/cli/commands/main/project/_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from sqlbuild.cli.commands.types import CompileLineageMode
from sqlbuild.cli.compile.models import (
CompileAnalysis,
CompileCommandRequest,
CompileWriteResult,
)
from sqlbuild.cli.entry.models import CompileCommandRequest
from sqlbuild.compiler.compile.models import CompileAnalysisSelection, CompilerDiagnostic
from sqlbuild.compiler.compile.types import DiagnosticPhase
from sqlbuild.compiler.profiling.main.collect import collect_compile_timings
Expand Down
4 changes: 2 additions & 2 deletions src/sqlbuild/cli/commands/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
)
from sqlbuild.cli.compile.models import ( # noqa: F401
CompileAnalysis,
CompileCommandRequest,
CompileProfileFlags,
CompileWriteResult,
SqlTestArtifactCacheRecord,
SqlTestArtifactIdentityContext,
)
from sqlbuild.cli.entry.models import ( # noqa: F401
CliEntrypointHandlers,
CompileCommandRequest,
CompileProfileFlags,
ParsedCliInvocation,
SelectorFileSummary,
SelectorInputs,
Expand Down
2 changes: 1 addition & 1 deletion src/sqlbuild/cli/compile/main/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Public compile command entrypoint."""

from sqlbuild.cli.commands.main.project._compile import run_compile as _run_compile
from sqlbuild.cli.compile.models import CompileCommandRequest
from sqlbuild.cli.entry.models import CompileCommandRequest


def run_compile(request: CompileCommandRequest) -> int:
Expand Down
31 changes: 0 additions & 31 deletions src/sqlbuild/cli/compile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

from sqlbuild.adapter.contract.classes.base_adapter import BaseAdapter
from sqlbuild.cli.commands.types import CompileLineageMode
from sqlbuild.cli.output.models import WrittenTarget
from sqlbuild.compiler.compile.models import (
CompiledObjectKey,
Expand All @@ -19,36 +18,6 @@
from sqlbuild.compiler.planner.models import NativeSqlTestArtifact


@dataclass(frozen=True)
class CompileProfileFlags:
"""Profiling toggles that skip compile phases for benchmarking."""

skip_discovery_sql_analysis: bool = False
skip_column_inference: bool = False
skip_contracts: bool = False
skip_write: bool = False


@dataclass(frozen=True)
class CompileCommandRequest:
"""CLI inputs for one `sqb compile` invocation."""

project_dir: Path | None = None
no_sql_validation: bool = False
defer_to: str | None = None
selected_target: str | None = None
json_output: bool = False
manifest: bool = False
dag_path: str | None = None
no_color: bool = False
lineage_mode: CompileLineageMode = CompileLineageMode.FAST
select: tuple[str, ...] = ()
exclude: tuple[str, ...] = ()
cli_vars: dict[str, object] | None = None
profile_flags: CompileProfileFlags = CompileProfileFlags()
no_cache: bool = False


@dataclass(frozen=True)
class CompileAnalysis:
"""Compiled project analysis shared by compile output phases."""
Expand Down
32 changes: 31 additions & 1 deletion src/sqlbuild/cli/entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from sqlbuild.cli.commands.classes.cli_namespace import CliNamespace
from sqlbuild.cli.commands.types import (
CompileLineageMode,
DagCommandHandler,
DebugCommandHandler,
FormatCommandHandler,
Expand All @@ -16,10 +17,39 @@
ScopeCommandHandler,
SkillsUpdateCommandHandler,
)
from sqlbuild.cli.compile.models import CompileCommandRequest
from sqlbuild.spec.contracts.types import WarehouseGroup


@dataclass(frozen=True)
class CompileProfileFlags:
"""Profiling toggles that skip compile phases for benchmarking."""

skip_discovery_sql_analysis: bool = False
skip_column_inference: bool = False
skip_contracts: bool = False
skip_write: bool = False


@dataclass(frozen=True)
class CompileCommandRequest:
"""CLI inputs for one `sqb compile` invocation."""

project_dir: Path | None = None
no_sql_validation: bool = False
defer_to: str | None = None
selected_target: str | None = None
json_output: bool = False
manifest: bool = False
dag_path: str | None = None
no_color: bool = False
lineage_mode: CompileLineageMode = CompileLineageMode.FAST
select: tuple[str, ...] = ()
exclude: tuple[str, ...] = ()
cli_vars: dict[str, object] | None = None
profile_flags: CompileProfileFlags = CompileProfileFlags()
no_cache: bool = False


@dataclass(frozen=True)
class SelectorFileSummary:
"""Non-expanded provenance for one selector file."""
Expand Down
Loading
Loading