Skip to content

dbt CLI

prefect_dbt_flow.dbt.cli

Utility functions for interacting with dbt using command-line commands.

dbt_deps(project, profile, dag_options)

Function that executes dbt deps command

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required

Returns:

Type Description
str

A string representing the output of the dbt deps command

Source code in prefect_dbt_flow/dbt/cli.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def dbt_deps(
    project: DbtProject,
    profile: Optional[DbtProfile],
    dag_options: Optional[DbtDagOptions],
) -> str:
    """
    Function that executes `dbt deps` command

    Args:
        project: A class that represents a dbt project configuration.
        profile: A class that represents a dbt profile configuration.
        dag_options: A class to add dbt DAG configurations.

    Returns:
        A string representing the output of the `dbt deps` command
    """
    dbt_deps_cmd = [DBT_EXE, "deps"]
    dbt_deps_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_deps_cmd.extend(["--profiles-dir", str(project.profiles_dir)])

    if profile:
        dbt_deps_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.vars:
            dbt_deps_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_deps_cmd))

dbt_ls(project, dag_options, profile, output='json')

Code that lists resources from the dbt project, using dbt ls command.

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
output str

Format of output, default is JSON.

'json'

Returns:

Type Description
str

list of JSON objects containing dbt resources.

Source code in prefect_dbt_flow/dbt/cli.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def dbt_ls(
    project: DbtProject,
    dag_options: Optional[DbtDagOptions],
    profile: Optional[DbtProfile],
    output: str = "json",
) -> str:
    """
    Code that lists resources from the dbt project, using `dbt ls` command.

    Args:
        project: A class that represents a dbt project configuration.
        dag_options: A class to add dbt DAG configurations.
        profile: A class that represents a dbt profile configuration.
        output: Format of output, default is JSON.

    Returns:
        list of JSON objects containing dbt resources.
    """
    dbt_ls_cmd = [DBT_EXE, "ls"]
    dbt_ls_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_ls_cmd.extend(["--profiles-dir", str(project.profiles_dir)])
    dbt_ls_cmd.extend(["--output", output])

    if profile:
        dbt_ls_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.select:
            dbt_ls_cmd.extend(["--select", dag_options.select])
        if dag_options.exclude:
            dbt_ls_cmd.extend(["--exclude", dag_options.exclude])
        if dag_options.vars:
            dbt_ls_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_ls_cmd))

dbt_run(project, model, profile, dag_options)

Function that executes dbt run command

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
model str

Name of the model to run.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required

Returns:

Type Description
str

A string representing the output of the dbt run command.

Source code in prefect_dbt_flow/dbt/cli.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def dbt_run(
    project: DbtProject,
    model: str,
    profile: Optional[DbtProfile],
    dag_options: Optional[DbtDagOptions],
) -> str:
    """
    Function that executes `dbt run` command

    Args:
        project: A class that represents a dbt project configuration.
        model: Name of the model to run.
        profile: A class that represents a dbt profile configuration.
        dag_options: A class to add dbt DAG configurations.

    Returns:
        A string representing the output of the `dbt run` command.
    """
    dbt_run_cmd = [DBT_EXE, "run"]
    dbt_run_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_run_cmd.extend(["--profiles-dir", str(project.profiles_dir)])
    dbt_run_cmd.extend(["-m", model])

    if profile:
        dbt_run_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.vars:
            dbt_run_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_run_cmd))

dbt_seed(project, seed, profile, dag_options)

Function that executes dbt seed command

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
seed str

Name of the seed to run.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required

Returns:

Type Description
str

A string representing the output of the dbt seed command

Source code in prefect_dbt_flow/dbt/cli.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def dbt_seed(
    project: DbtProject,
    seed: str,
    profile: Optional[DbtProfile],
    dag_options: Optional[DbtDagOptions],
) -> str:
    """
    Function that executes `dbt seed` command

    Args:
        project: A class that represents a dbt project configuration.
        seed: Name of the seed to run.
        profile: A class that represents a dbt profile configuration.
        dag_options: A class to add dbt DAG configurations.

    Returns:
        A string representing the output of the `dbt seed` command
    """
    dbt_seed_cmd = [DBT_EXE, "seed"]
    dbt_seed_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_seed_cmd.extend(["--profiles-dir", str(project.profiles_dir)])
    dbt_seed_cmd.extend(["--select", seed])

    if profile:
        dbt_seed_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.vars:
            dbt_seed_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_seed_cmd))

dbt_snapshot(project, snapshot, profile, dag_options)

Function that executes dbt snapshot command

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
snapshot str

Name of the snapshot to run.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required

Returns:

Type Description
str

A string representing the output of the dbt snapshot command

Source code in prefect_dbt_flow/dbt/cli.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def dbt_snapshot(
    project: DbtProject,
    snapshot: str,
    profile: Optional[DbtProfile],
    dag_options: Optional[DbtDagOptions],
) -> str:
    """
    Function that executes `dbt snapshot` command

    Args:
        project: A class that represents a dbt project configuration.
        snapshot: Name of the snapshot to run.
        profile: A class that represents a dbt profile configuration.
        dag_options: A class to add dbt DAG configurations.

    Returns:
        A string representing the output of the `dbt snapshot` command
    """
    dbt_snapshot_cmd = [DBT_EXE, "snapshot"]
    dbt_snapshot_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_snapshot_cmd.extend(["--profiles-dir", str(project.profiles_dir)])
    dbt_snapshot_cmd.extend(["--select", snapshot])

    if profile:
        dbt_snapshot_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.vars:
            dbt_snapshot_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_snapshot_cmd))

dbt_test(project, model, profile, dag_options)

Function that executes dbt test command

Parameters:

Name Type Description Default
project DbtProject

A class that represents a dbt project configuration.

required
model str

Name of the model to run.

required
profile Optional[DbtProfile]

A class that represents a dbt profile configuration.

required
dag_options Optional[DbtDagOptions]

A class to add dbt DAG configurations.

required

Returns:

Type Description
str

A string representing the output of the dbt test command.

Source code in prefect_dbt_flow/dbt/cli.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def dbt_test(
    project: DbtProject,
    model: str,
    profile: Optional[DbtProfile],
    dag_options: Optional[DbtDagOptions],
) -> str:
    """
    Function that executes `dbt test` command

    Args:
        project: A class that represents a dbt project configuration.
        model: Name of the model to run.
        profile: A class that represents a dbt profile configuration.
        dag_options: A class to add dbt DAG configurations.

    Returns:
        A string representing the output of the `dbt test` command.
    """
    dbt_test_cmd = [DBT_EXE, "test"]
    dbt_test_cmd.extend(["--project-dir", str(project.project_dir)])
    dbt_test_cmd.extend(["--profiles-dir", str(project.profiles_dir)])
    dbt_test_cmd.extend(["-m", model])

    if profile:
        dbt_test_cmd.extend(["-t", profile.target])

    if dag_options:
        if dag_options.vars:
            dbt_test_cmd.extend(["--vars", f"'{json.dumps(dag_options.vars)}'"])

    return cmd.run(" ".join(dbt_test_cmd))