Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore (dbt): better default target #114

Merged
merged 1 commit into from
Oct 13, 2022
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
4 changes: 2 additions & 2 deletions src/preset_cli/cli/superset/sync/dbt/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@click.command()
@click.argument("manifest", type=click.Path(exists=True, resolve_path=True))
@click.option("--project", help="Name of the dbt project", default="default")
@click.option("--target", help="Target name", default="dev")
@click.option("--target", help="Target name", default=None)
@click.option(
"--profiles",
help="Location of profiles.yml file",
Expand Down Expand Up @@ -65,7 +65,7 @@ def dbt_core( # pylint: disable=too-many-arguments, too-many-locals
ctx: click.core.Context,
manifest: str,
project: str,
target: str,
target: Optional[str],
select: Tuple[str, ...],
exclude: Tuple[str, ...],
profiles: Optional[str] = None,
Expand Down
6 changes: 4 additions & 2 deletions src/preset_cli/cli/superset/sync/dbt/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from pathlib import Path
from typing import Any
from typing import Any, Optional

from yarl import URL

Expand All @@ -19,7 +19,7 @@ def sync_database( # pylint: disable=too-many-locals, too-many-arguments
client: SupersetClient,
profiles_path: Path,
project_name: str,
target_name: str,
target_name: Optional[str],
import_db: bool,
disallow_edits: bool, # pylint: disable=unused-argument
external_url_prefix: str,
Expand All @@ -32,6 +32,8 @@ def sync_database( # pylint: disable=too-many-locals, too-many-arguments
profiles = load_profiles(profiles_path, project_name, target_name)
project = profiles[project_name]
outputs = project["outputs"]
if target_name is None:
target_name = project["target"]
target = outputs[target_name]

# read additional metadata that should be applied to the DB
Expand Down
6 changes: 5 additions & 1 deletion src/preset_cli/cli/superset/sync/dbt/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ class Target(TypedDict):
threads: int


def load_profiles(path: Path, project_name: str, target_name: str) -> Dict[str, Any]:
def load_profiles(
path: Path, project_name: str, target_name: Optional[str],
) -> Dict[str, Any]:
"""
Load the file and apply Jinja2 templating.
"""
Expand All @@ -242,6 +244,8 @@ def load_profiles(path: Path, project_name: str, target_name: str) -> Dict[str,
raise Exception(f"Project {project_name} not found in {path}")
project = profiles[project_name]
outputs = project["outputs"]
if target_name is None:
target_name = project["target"]
if target_name not in outputs:
raise Exception(f"Target {target_name} not found in the outputs of {path}")
target = outputs[target_name]
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/superset/sync/dbt/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_dbt_core(mocker: MockerFixture, fs: FakeFilesystem) -> None:
client,
profiles,
"default",
"dev",
None,
False,
False,
"",
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_dbt(mocker: MockerFixture, fs: FakeFilesystem) -> None:
client,
profiles,
"default",
"dev",
None,
False,
False,
"",
Expand Down Expand Up @@ -279,7 +279,7 @@ def test_dbt_core_default_profile(mocker: MockerFixture, fs: FakeFilesystem) ->
client,
profiles,
"default",
"dev",
None,
False,
False,
"",
Expand Down
35 changes: 35 additions & 0 deletions tests/cli/superset/sync/dbt/databases_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ def test_sync_database_new(mocker: MockerFixture, fs: FakeFilesystem) -> None:
)


def test_sync_database_new_default_target(
mocker: MockerFixture, fs: FakeFilesystem
) -> None:
"""
Test ``sync_database`` when we want to import a new DB using the default target.
"""
fs.create_file(
"/path/to/.dbt/profiles.yml",
contents=yaml.dump({"my_project": {"outputs": {"dev": {}}, "target": "dev"}}),
)
mocker.patch(
"preset_cli.cli.superset.sync.dbt.databases.build_sqlalchemy_params",
return_value={"sqlalchemy_uri": "dummy://"},
)
client = mocker.MagicMock()
client.get_databases.return_value = []

sync_database(
client=client,
profiles_path=Path("/path/to/.dbt/profiles.yml"),
project_name="my_project",
target_name=None,
import_db=True,
disallow_edits=False,
external_url_prefix="",
)

client.create_database.assert_called_with(
database_name="my_project_dev",
is_managed_externally=False,
masked_encrypted_extra=None,
sqlalchemy_uri="dummy://",
)


def test_sync_database_new_custom_sqlalchemy_uri(
mocker: MockerFixture,
fs: FakeFilesystem,
Expand Down
57 changes: 57 additions & 0 deletions tests/cli/superset/sync/dbt/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,63 @@ def test_load_profiles(monkeypatch: pytest.MonkeyPatch, fs: FakeFilesystem) -> N
}


def test_load_profiles_default_target(
monkeypatch: pytest.MonkeyPatch, fs: FakeFilesystem
) -> None:
"""
Test ``load_profiles`` when no target is specified.
"""
monkeypatch.setenv("REDSHIFT_HOST", "127.0.0.1")
monkeypatch.setenv("REDSHIFT_PORT", "1234")
monkeypatch.setenv("REDSHIFT_USER", "username")
monkeypatch.setenv("REDSHIFT_PASSWORD", "password123")
monkeypatch.setenv("REDSHIFT_DATABASE", "db")
monkeypatch.setenv("THREADS", "3")

path = Path("/path/to/profiles.yml")
fs.create_file(
path,
contents="""
jaffle_shop:
outputs:
dev:
host: "{{ env_var('REDSHIFT_HOST') | as_text }}"
port: "{{ env_var('REDSHIFT_PORT') | as_number }}"
user: "{{ env_var('REDSHIFT_USER') }}"
pass: "{{ env_var('REDSHIFT_PASSWORD') }}"
dbname: "{{ env_var('REDSHIFT_DATABASE') }}"
schema: public
threads: "{{ env_var('THREADS') | as_native }}"
type: postgres
enabled: "{{ (target.name == 'prod') | as_bool }}"
a_list: [1, 2, 3]
a_value: 10
target: dev
""",
)

assert load_profiles(path, "jaffle_shop", None) == {
"jaffle_shop": {
"outputs": {
"dev": {
"host": "127.0.0.1",
"port": 1234,
"user": "username",
"pass": "password123",
"dbname": "db",
"schema": "public",
"threads": 3,
"type": "postgres",
"enabled": False,
"a_list": [1, 2, 3],
"a_value": 10,
},
},
"target": "dev",
},
}


def test_filter_models() -> None:
"""
Test ``filter_models``.
Expand Down