prefect_dbt.cli.commands
Module containing tasks and flows for interacting with dbt CLI
Classes
DbtCoreOperation
Bases: ShellOperation
A block representing a dbt operation, containing multiple dbt and shell commands.
For long-lasting operations, use the trigger method and utilize the block as a context manager for automatic closure of processes when context is exited. If not, manually call the close method to close processes.
For short-lasting operations, use the run method. Context is automatically managed with this method.
Attributes:
Name | Type | Description |
---|---|---|
commands |
A list of commands to execute sequentially. |
|
stream_output |
Whether to stream output. |
|
env |
A dictionary of environment variables to set for the shell operation. |
|
working_directory |
The working directory context the commands will be executed within. |
|
shell |
The shell to use to execute the commands. |
|
extension |
The extension to use for the temporary file.
if unset defaults to |
|
profiles_dir |
Optional[Path]
|
The directory to search for the profiles.yml file.
Setting this appends the |
project_dir |
Optional[Path]
|
The directory to search for the dbt_project.yml file. Default is the current working directory and its parents. |
overwrite_profiles |
bool
|
Whether the existing profiles.yml file under profiles_dir should be overwritten with a new profile. |
dbt_cli_profile |
Optional[DbtCliProfile]
|
Profiles class containing the profile written to profiles.yml. Note! This is optional and will raise an error if profiles.yml already exists under profile_dir and overwrite_profiles is set to False. |
Examples:
Load a configured block.
from prefect_dbt import DbtCoreOperation
dbt_op = DbtCoreOperation.load("BLOCK_NAME")
Execute short-lasting dbt debug and list with a custom DbtCliProfile.
from prefect_dbt import DbtCoreOperation, DbtCliProfile
from prefect_dbt.cli.configs import SnowflakeTargetConfigs
from prefect_snowflake import SnowflakeConnector
snowflake_connector = await SnowflakeConnector.load("snowflake-connector")
target_configs = SnowflakeTargetConfigs(connector=snowflake_connector)
dbt_cli_profile = DbtCliProfile(
name="jaffle_shop",
target="dev",
target_configs=target_configs,
)
dbt_init = DbtCoreOperation(
commands=["dbt debug", "dbt list"],
dbt_cli_profile=dbt_cli_profile,
overwrite_profiles=True
)
dbt_init.run()
Execute a longer-lasting dbt run as a context manager.
with DbtCoreOperation(commands=["dbt run"]) as dbt_run:
dbt_process = dbt_run.trigger()
# do other things
dbt_process.wait_for_completion()
dbt_output = dbt_process.fetch_result()
Source code in prefect_dbt/cli/commands.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
Functions
trigger_dbt_cli_command
async
Task for running dbt commands.
If no profiles.yml file is found or if overwrite_profiles flag is set to True, this will first generate a profiles.yml file in the profiles_dir directory. Then run the dbt CLI shell command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
str
|
The dbt command to be executed. |
required |
profiles_dir |
Optional[Union[Path, str]]
|
The directory to search for the profiles.yml file. Setting this
appends the |
None
|
project_dir |
Optional[Union[Path, str]]
|
The directory to search for the dbt_project.yml file. Default is the current working directory and its parents. |
None
|
overwrite_profiles |
bool
|
Whether the existing profiles.yml file under profiles_dir should be overwritten with a new profile. |
False
|
dbt_cli_profile |
Optional[DbtCliProfile]
|
Profiles class containing the profile written to profiles.yml. Note! This is optional and will raise an error if profiles.yml already exists under profile_dir and overwrite_profiles is set to False. |
None
|
**shell_run_command_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to shell_run_command. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
last_line_cli_output |
str
|
The last line of the CLI output will be returned
if |
full_cli_output |
List[str]
|
Full CLI output will be returned if |
Examples:
Execute dbt debug
with a pre-populated profiles.yml.
from prefect import flow
from prefect_dbt.cli.commands import trigger_dbt_cli_command
@flow
def trigger_dbt_cli_command_flow():
result = trigger_dbt_cli_command("dbt debug")
return result
trigger_dbt_cli_command_flow()
Execute dbt debug
without a pre-populated profiles.yml.
from prefect import flow
from prefect_dbt.cli.credentials import DbtCliProfile
from prefect_dbt.cli.commands import trigger_dbt_cli_command
from prefect_dbt.cli.configs import SnowflakeTargetConfigs
from prefect_snowflake.credentials import SnowflakeCredentials
@flow
def trigger_dbt_cli_command_flow():
credentials = SnowflakeCredentials(
user="user",
password="password",
account="account.region.aws",
role="role",
)
connector = SnowflakeConnector(
schema="public",
database="database",
warehouse="warehouse",
credentials=credentials,
)
target_configs = SnowflakeTargetConfigs(
connector=connector
)
dbt_cli_profile = DbtCliProfile(
name="jaffle_shop",
target="dev",
target_configs=target_configs,
)
result = trigger_dbt_cli_command(
"dbt debug",
overwrite_profiles=True,
dbt_cli_profile=dbt_cli_profile
)
return result
trigger_dbt_cli_command_flow()
Source code in prefect_dbt/cli/commands.py
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 47 48 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 80 81 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 113 114 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|