Exercise#
Prerequisite#
To proceed with this exercise we first need to have some new or updated records in our database. We have prepared the update.sql file with a batch of fresh data which we now import into our current DB. To do that follow the next steps:
open the terminal and navigate to the
dbt-demo/dbfoldercopy the
update.sqlfile into the container using the following command:docker cp update.sql dbt-demo_database_1:/update.sqlconnect to the Postgres container:
docker exec -it dbt-workshop-db psql -U postgres datawarehouserun the .sql file inside Postgres container:
\i update.sql
Now we should have some fresh and updated records in our DB.
Exercise#
create
dim_customers_snapshot: createdim_customers_snapshot.sqlmodel insnapshotsfolder according to the provided example. Checkallcolumns for the change.create
fact_orders_snapshot: createfact_orders_snapshot.sqlmodel. Check onlystatuscolumn for the change.run
dbt runto load new and updated records into our modelsrun
dbt snapshotto create snapshots
Solution#
dim_customers_snapshot.sql
{% snapshot dim_customers_snapshot %}
    {{
        config(
          unique_key='customer_id',
          strategy='check',
          target_schema='snapshots',
          check_cols='all',
        )
    }}
    select * from {{ ref('dim_customers') }}
{% endsnapshot %}
fact_orders_snapshot.sql
{% snapshot fact_orders_snapshots %}
    {{
        config(
          unique_key='order_id',
          strategy='check',
          target_schema='snapshots',
          check_cols=['status'],
        )
    }}
  select * from {{ ref('fact_orders') }}
{% endsnapshot %}