Skip to content

Commit

Permalink
Docs: profile overrides (#19)
Browse files Browse the repository at this point in the history
* overrides in readme

* profile overrides in docs

---------

Co-authored-by: Nico Gelders <[email protected]>
  • Loading branch information
nicogelders and Nico Gelders authored Oct 28, 2023
1 parent 4072b71 commit 1051c5f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Here's an example of how to use prefect-dbt-flow to create a Prefect flow for yo

```python
from prefect_dbt_flow import dbt_flow
from prefect_dbt_flow.dbt import DbtProfile, DbtProject, DbtDagOptions
from prefect_dbt_flow.dbt import DbtProfile, DbtProject

my_flow = dbt_flow(
project=DbtProject(
Expand All @@ -49,13 +49,18 @@ my_flow = dbt_flow(
),
profile=DbtProfile(
target="dev",
overrides={
"type": "duckdb",
"path": "path_to/duckdb.db",
},
),
)

if __name__ == "__main__":
my_flow()
```
![jaffle_shop_dag](https://raw.githubusercontent.com/datarootsio/prefect-dbt-flow/main/docs/images/jaffle_shop_dag.png)

<img src="https://raw.githubusercontent.com/datarootsio/prefect-dbt-flow/main/docs/images/jaffle_shop_dag.png" alt="jaffle_shop_dag" width="100%">

For more information consult the [docs](https://datarootsio.github.io/prefect-dbt-flow/)

Expand Down
70 changes: 70 additions & 0 deletions docs/profile_overrides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Profile overrides

When using dbt, you might want to dynamically change the profile configuration based for example on the environment you are running in. Also some profile configurations might be sensitive and you might not want to store them in your repository. Profile overrides allow you to inject profile configurations via python code, at runtime. In this section we will show you how to use profile overrides.

## Basic usage

To override a profile you must past the `DbtProfile` dataclass to the dbt_flow function.

Profile overrides handle the following cases:

- When a `profile.yml` is detected in the configured `profiles_dir` it will read in the profile and override the values with the values in the `overrides` dict. The values that are not overridden will be kept as is.
- When no `profile.yml` is detected it will create a new `profile.yml` with the values in the `overrides` dict. In this case you need to provide all the values needed for your dbt adapter.

## Example


Contents of `path_to/jaffle_shop/profile.yml` BEFORE override:
```yaml
example_jaffle_shop:
target: dev
outputs:
dev:
type: postgres
host: data-db
port: 5432
dbname: data
```
Configuring the profile overrides:
```python
import os

from prefect import variables
from prefect.blocks.system import Secret

postgres_user = variables.get("POSTGRES_USER")
postgres_pw = Secret.load("POSTGRES_PASSWORD").get()

my_flow = dbt_flow(
project=DbtProject(
...,
profiles_dir="path_to/jaffle_shop",
),
profile=DbtProfile(
target="dev",
overrides={
"user": postgres_user,
"password": postgres_pw,
"schema": os.environ["POSTGRES_SCHEMA"],
"connect_timeout": 30,
},
),
)
```

Contents of `path_to/jaffle_shop/profile.yml` AFTER override:
```yaml
example_jaffle_shop:
target: dev
outputs:
dev:
type: postgres
host: data-db
port: 5432
dbname: data
user: admin
password: super-secret-password
schema: example
connect_timeout: 30
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nav:
- Getting started: getting_started.md
- How it works: how_it_works.md
- DAG integration: dag_integration.md
- Profile overrides: profile_overrides.md
- Changelog: https://github.com/datarootsio/prefect-dbt-flow/releases
- License: license.md
- API:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duckdb
import pytest
from prefect.task_runners import SequentialTaskRunner
from prefect.testing.utilities import prefect_test_harness

from prefect_dbt_flow import dbt_flow
from prefect_dbt_flow.dbt import DbtDagOptions, DbtProfile, DbtProject
Expand All @@ -11,6 +12,12 @@
JAFFLE_SHOP_PATH = Path(__file__).parent.parent / "examples" / "jaffle_shop_duckdb"


@pytest.fixture(autouse=True, scope="session")
def prefect_test_fixture():
with prefect_test_harness():
yield


@pytest.fixture
def duckdb_db_file(monkeypatch, tmp_path: Path):
duckdb_db_file = tmp_path / "dbt_test.duckdb"
Expand Down

0 comments on commit 1051c5f

Please sign in to comment.