<@ULVA73B9P> write me the latest prefect_dbt code ...
# ask-marvin
a
@Marvin write me the latest prefect_dbt code for run commands in sequence
m
While that example doesn't directly address Prefect combined with dbt, I've found a better approach considering the latest Prefect versions. Here's a snippet demonstrating how to run dbt commands in sequence within a Prefect flow:
Copy code
python
from prefect import flow, task
from prefect_dbt import DbtCoreOperation

@task
def dbt_run():
    return DbtCoreOperation(
        commands=["dbt run"],
        project_dir="/path/to/your/dbt/project",
        profiles_dir="/path/to/your/dbt/profiles"
    ).run()

@task
def dbt_test():
    return DbtCoreOperation(
        commands=["dbt test"],
        project_dir="/path/to/your/dbt/project",
        profiles_dir="/path/to/your/dbt/profiles"
    ).run()

@task
def dbt_docs():
    return DbtCoreOperation(
        commands=["dbt docs generate"],
        project_dir="/path/to/your/dbt/project",
        profiles_dir="/path/to/your/dbt/profiles"
    ).run()

@flow
def dbt_flow():
    dbt_run()
    dbt_test()
    dbt_docs()

if __name__ == "__main__":
    dbt_flow()
This script defines three tasks (
dbt_run
,
dbt_test
,
dbt_docs
) to run
dbt
commands sequentially within the
dbt_flow
. Adjust the paths for your
dbt
project and profiles as necessary. Feel free to reach out for more details: https://docs.prefect.io/latest/