Skip to content

Commit

Permalink
Profiling and Adapter management work with Click (#5892)
Browse files Browse the repository at this point in the history
  • Loading branch information
iknox-fa authored Sep 21, 2022
1 parent e402241 commit fd778dc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20220920-144842.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Under the Hood
body: Profiling and Adapter Management work with Click CLI
time: 2022-09-20T14:48:42.070256-05:00
custom:
Author: iknox-fa
Issue: "5531"
PR: "5892"
33 changes: 18 additions & 15 deletions core/dbt/adapters/factory.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import threading
from pathlib import Path
from contextlib import contextmanager
from importlib import import_module
from typing import Type, Dict, Any, List, Optional, Set
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Type

from dbt.exceptions import RuntimeException, InternalException
from dbt.include.global_project import (
PACKAGE_PATH as GLOBAL_PROJECT_PATH,
PROJECT_NAME as GLOBAL_PROJECT_NAME,
)
from dbt.adapters.base.plugin import AdapterPlugin
from dbt.adapters.protocol import AdapterConfig, AdapterProtocol, RelationProtocol
from dbt.contracts.connection import AdapterRequiredConfig, Credentials
from dbt.events.functions import fire_event
from dbt.events.types import AdapterImportError, PluginLoadError
from dbt.contracts.connection import Credentials, AdapterRequiredConfig
from dbt.adapters.protocol import (
AdapterProtocol,
AdapterConfig,
RelationProtocol,
)
from dbt.adapters.base.plugin import AdapterPlugin

from dbt.exceptions import InternalException, RuntimeException
from dbt.include.global_project import PACKAGE_PATH as GLOBAL_PROJECT_PATH
from dbt.include.global_project import PROJECT_NAME as GLOBAL_PROJECT_NAME

Adapter = AdapterProtocol

Expand Down Expand Up @@ -217,3 +211,12 @@ def get_adapter_package_names(name: Optional[str]) -> List[str]:

def get_adapter_type_names(name: Optional[str]) -> List[str]:
return FACTORY.get_adapter_type_names(name)


@contextmanager
def adapter_management():
reset_adapters()
try:
yield
finally:
cleanup_connections()
17 changes: 14 additions & 3 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import inspect # This is temporary for RAT-ing
import sys
from copy import copy
from pprint import pformat as pf # This is temporary for RAT-ing

import click
from dbt.adapters.factory import adapter_management
from dbt.cli import params as p
from dbt.cli.flags import Flags
from dbt.profiler import profiler


def cli_runner():
Expand Down Expand Up @@ -51,9 +52,19 @@ def cli(ctx, **kwargs):
"""An ELT tool for managing your SQL transformations and data models.
For more documentation on these commands, visit: docs.getdbt.com
"""
if kwargs.get("version", False):
incomplete_flags = Flags()

# Profiling
if incomplete_flags.RECORD_TIMING_INFO:
ctx.with_resource(profiler(enable=True, outfile=incomplete_flags.RECORD_TIMING_INFO))

# Adapter management
ctx.with_resource(adapter_management())

# Version info
if incomplete_flags.VERSION:
click.echo(f"`version` called\n ctx.params: {pf(ctx.params)}")
sys.exit()
return
else:
del ctx.params["version"]

Expand Down
12 changes: 7 additions & 5 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,18 @@
"--profiles-dir",
envvar="DBT_PROFILES_DIR",
help="Which directory to look in for the profiles.yml file. If not set, dbt will look in the current working directory first, then HOME/.dbt/",
default=None,
type=click.Path(),
default=PurePath.joinpath(Path.home(), ".dbt"),
type=click.Path(
exists=True,
),
)

project_dir = click.option(
"--project-dir",
envvar=None,
help="Which directory to look in for the dbt_project.yml file. Default is the current working directory and its parents.",
default=None,
type=click.Path(),
default=Path.cwd(),
type=click.Path(exists=True),
)

quiet = click.option(
Expand All @@ -241,7 +243,7 @@
"-r",
envvar=None,
help="When this option is passed, dbt will output low-level timing stats to the specified file. Example: `--record-timing-info output.profile`",
is_flag=True,
type=click.Path(exists=False),
)

resource_type = click.option(
Expand Down

0 comments on commit fd778dc

Please sign in to comment.