Build with Daana

Chapter 10: Going to production

In chapter 7, you applied reporting documents to the bookstore data warehouse. This is the last chapter that goes further after the tutorial. Moving the bookstore project into production means selecting a target, checking each change, and testing mappings against the production database's source tables and SQL dialect.

You will deploy to another target locally, prepare continuous integration (CI) checks, and generate a Dataform preview. Afterward, you will be able to verify the selected target's data and explain what remains untested.

Start state: The main project bookstore has completed daana execute, and its dar/ holds the reporting documents of chapter 7. The terminal is in bookstore, and DuckDB is closed. To start at this chapter, follow Start at any chapter.

What ran where

The local steps cover setup, steps 1 through 3, the offline and DuckDB checks in step 4, and the preview in step 6. Read the CI example, step 5, and BigQuery generation in step 6 as configuration only. The CI workflow, the operations on the production database, and the Dataform run were not run for this tutorial.

StepWhere it ran
Connection profiles and daana check connectionsLocally
Project declarations per target and daana check --projectLocally, including the BigQuery declaration (offline)
Deploy to a second targetLocally, on a second set of schemas in bookstore.duckdb
Connected mapping checkLocally, on the duckdb profile
daana dar check and daana dar generate --previewLocally
CI workflow fileNot run. Example configuration. Its offline checks ran locally, its connected check ran locally on duckdb only, and its deploy command did not run.
install, deploy and execute on PostgreSQL, BigQuery or SnowflakeNot run. Configuration only
daana dar generate --connection bookstore_bq and the Dataform runNot run. Configuration only

Set up the project

A side project keeps chapter 7's documents and database intact. The setup replaces chapter 7's finished dar/order_line.yaml with the file from checkpoint ch9. This is the daily revenue declaration of daana.dev's bookstore example, with chapter 8's fix. Its axis is order_line_purchase_ts, the time the order was placed, so daily revenue is grouped by that day, as its description promises.

Chapter 7's finished declaration has a checks entry. Daana refuses to generate the preview in step 6 from that declaration because output checks are not implemented for Dataform.

Removing dar/customer.yaml and dar/order.yaml limits the preview to four SQLX files. Replacing only order_line.yaml would generate nine.

Stay in bookstore-production after setup. The next steps printed by daana init do not apply because ch10-files is only a source for the declaration. Copy the project and prepare its reporting documents, DAR (Data according to Requirements), for the preview:

Run in terminal

cp -R ../bookstore ../bookstore-production
cd ../bookstore-production
rm dar/customer.yaml dar/order.yaml
daana init ../ch10-files --example bookstore --checkpoint ch9
cp ../ch10-files/dar/order_line.yaml dar/

The commands leave one DAR document, dar/order_line.yaml, alongside README.md. The copied file stays as checkpoint ch9 ships it, with its axis already set to order_line_purchase_ts.

What changes between environments

The model, workflow, and DAR documents keep their meaning in each environment. Connection profiles change to select the database. Mapping expressions may need changes to run in that database's SQL dialect.

In production, the ingestion tool loads the stage.* source tables before deployment. Daana reads them through the selected profile. Adding a profile does not move local data to the production database. See Cross-project BigQuery for sources in another project.

Step 1: Add connection profiles

Connection profiles give each target its database settings. Step 4 uses bookstore_bq for its checks. The local profile follows later in this section. Add these three profiles for the production databases below duckdb in connections.yaml:

  bookstore_pg:
    type: postgresql
    host: "${BOOKSTORE_PG_HOST}"
    port: 5432
    user: "${BOOKSTORE_PG_USER}"
    password: "${BOOKSTORE_PG_PASSWORD}"
    database: bookstore
    sslmode: require
    target_schema: daana_dw
    stage_schema: daana_stage
    metadata_schema: daana_metadata

  bookstore_bq:
    type: bigquery
    project_id: "${BOOKSTORE_GCP_PROJECT}"
    dataset: daana_dw
    location: EU
    credentials_file: "${BOOKSTORE_BQ_CREDENTIALS_FILE}"

  bookstore_sf:
    type: snowflake
    account: xy12345.eu-west-1.aws
    user: "${BOOKSTORE_SF_USER}"
    password: "${BOOKSTORE_SF_PASSWORD}"
    warehouse: BOOKSTORE_WH
    database: BOOKSTORE
    schema: DAANA_DW

The ${...} values come from environment variables. Step 3 deploys to a second target in the same DuckDB file, with its own target, stage, and metadata schemas. The stage_schema setting names a schema of the target. It is not the stage schema that holds the source tables.

Add the local profile with its separate schema names:

  duckdb_release:
    type: duckdb
    database: bookstore.duckdb
    target_schema: release_dw
    stage_schema: release_stage
    metadata_schema: release_metadata

daana install refuses schemas that already hold tables or views. daana deploy refuses schemas that differ from the installation's. Changing those schemas needs a new installation.

The profile check reads the configuration and does not connect to a database. The --format json flag selects JSON output. Validate the profiles and compare the valid and invalid counts in the result:

Run in terminal

daana check connections --format json

Expected output

{
  "success": true,
  "command": "connections",
  "duration_ms": 2,
  "file": "connections.yaml",
  "summary": {
    "files_checked": 1,
    "errors": 0,
    "warnings": 1,
    "info": 0
  },
  "details": {
    "profile_count": 5,
    "valid_count": 5,
    "invalid_count": 0
  }
}

All five profiles are valid. The warning comes from an empty BigQuery credentials path when BOOKSTORE_BQ_CREDENTIALS_FILE is unset. The value of duration_ms is the run time of the check in milliseconds, so it differs between runs. The Connections reference lists database options.

The profiles are now checked, but the project still selects duckdb. The next step selects the release profile through a project declaration.

Step 2: Select a target per environment

daana.yaml selects the duckdb profile for daana install, daana deploy, and daana execute. The --connection flag cannot redirect these commands to another profile. Try selecting the release profile with that flag and inspect the error:

Run in terminal

daana deploy --connection duckdb_release

Expected output

Error: --connection conflicts with selected project

The command exits with status 2 because the flag conflicts with the project. A separate project declaration can select the release profile while using the same workflow. Create daana.release.yaml beside daana.yaml with duckdb_release as its profile:

version: 1
domains:
  dab:
    workflow: "dab/workflow.yaml"
connections:
  file: connections.yaml
  profile: duckdb_release

The workflow still has connection: duckdb near the end of dab/workflow.yaml.

Check the release declaration with --project:

Run in terminal

daana check --project daana.release.yaml

Expected output

Project: <project>/daana.release.yaml
Mode: offline
DAB: not_run
Target profile: duckdb_release (validated_offline)
DAS: disabled
DAR: disabled
Source schema: not requested
error [project] : workflow.connection conflicts with the selected project profile
Error: project selection is not usable: workflow.connection conflicts with the selected project profile

Daana rejects the conflict, so neither connection wins. The output shows DAB: not_run, because Daana checks DAB (Data according to Business) only when the two selections agree.

Before you continue

The error names the workflow's connection. Every mapping also has connection: duckdb for its source tables. Predict whether the mappings must change too before the release declaration passes the check, and write down the reason.

Delete connection: duckdb from the workflow so that it uses the project's profile. Before you change anything else, check the declaration again and compare the result with your prediction:

Run in terminal

daana check --project daana.release.yaml

Expected output

Project: <project>/daana.release.yaml
Mode: offline
DAB: passed
Target profile: duckdb_release (validated_offline)
DAS: disabled
DAR: disabled
Source schema: not requested

DAB: passed and Target profile: duckdb_release show that the declaration is usable offline. Keep the mappings' connection: duckdb entries, because a project run reads their source tables through the selected profile.

The mapping entries look like the workflow's, so the conflict seems to cover them too. The second check passes with them unchanged. The release target is ready for installation.

Step 3: Deploy to the second target

The release target is new, so it needs daana install first, as chapter 1 explained. Run these commands in sequence:

Run in terminal

daana install --project daana.release.yaml
daana deploy --project daana.release.yaml
daana execute --project daana.release.yaml

Expected output, summary line of daana execute

Pipelines: 50 ran

Each command exits with status 0. Together they create release_dw, release_stage, and release_metadata in bookstore.duckdb. The summary shows 50 pipelines, as in the main project. Open DuckDB:

Run in terminal

duckdb -readonly bookstore.duckdb

Exercise

Decide what result would show that the three commands wrote to the wrong target. Write a query that shows which target received this load before opening a correct answer.

Hint: Use schema-qualified view_order_line references, UNION ALL, counts, and sums of amount and tax. Label each result with its target schema. Each metadata schema's batch_history records each pipeline run and its write time, popln_tmstp.

Show a correct answer

A correct answer

Compare the order-line counts and revenue totals in both target schemas:

Run in DuckDB

SELECT 'daana_dw' AS target_schema, count(*) AS order_lines,
       round(sum(order_line_amount_amount), 2) AS revenue_incl_tax,
       round(sum(order_line_amount_amount - order_line_tax_amount), 2) AS revenue_excl_tax
FROM daana_dw.view_order_line
UNION ALL
SELECT 'release_dw', count(*),
       round(sum(order_line_amount_amount), 2),
       round(sum(order_line_amount_amount - order_line_tax_amount), 2)
FROM release_dw.view_order_line
ORDER BY target_schema;

Expected output

target_schemaorder_linesrevenue_incl_taxrevenue_excl_tax
daana_dw31762.88700.86
release_dw31762.88700.86

Both targets have 31 order lines, revenue of 762.88 including tax, and revenue of 700.86 excluding tax, matching chapter 5. The rows in release_dw show that the commands loaded the release target. Equal totals cannot show whether this load also wrote to daana_dw. Compare the runs that each target recorded:

Run in DuckDB

SELECT 'daana_metadata' AS metadata_schema, count(*) AS runs,
       count(*) FILTER (WHERE popln_tmstp >= (SELECT min(popln_tmstp) FROM release_metadata.batch_history)) AS runs_since_release_load
FROM daana_metadata.batch_history
UNION ALL
SELECT 'release_metadata', count(*),
       count(*) FILTER (WHERE popln_tmstp >= (SELECT min(popln_tmstp) FROM release_metadata.batch_history))
FROM release_metadata.batch_history
ORDER BY metadata_schema;

Expected output

metadata_schemarunsruns_since_release_load
daana_metadata500
release_metadata5050

daana_metadata holds 50 runs copied from chapter 1. None was written during the release load. All 50 release_metadata runs were written by this step. The commands wrote their runs only to the release target.

Compare the original and release targets' order-line counts and revenue totals:

Run in DuckDB

SELECT 'daana_dw' AS target_schema, count(*) AS order_lines,
       round(sum(order_line_amount_amount), 2) AS revenue_incl_tax,
       round(sum(order_line_amount_amount - order_line_tax_amount), 2) AS revenue_excl_tax
FROM daana_dw.view_order_line
UNION ALL
SELECT 'release_dw', count(*),
       round(sum(order_line_amount_amount), 2),
       round(sum(order_line_amount_amount - order_line_tax_amount), 2)
FROM release_dw.view_order_line
ORDER BY target_schema;

Expected output

target_schemaorder_linesrevenue_incl_taxrevenue_excl_tax
daana_dw31762.88700.86
release_dw31762.88700.86

Both targets hold 31 order lines and the same totals, so the release target holds its own loaded copy of the data.

What Daana did

  • Daana created view_order_line in the selected release schema. The comparison above reads it alongside the original view.
  • Daana loaded the same mapped amounts into that target. The counts and revenue totals in that comparison agree with the original target.

Quit DuckDB. Steps 1 to 3 selected and loaded a second target locally. Steps 4 to 6 prepare the CI checks, the deployment to the production database, and the files for Dataform.

Step 4: Check the project in CI

CI checks changes before deployment. A CI job runs the offline and connected checks of chapter 2.

Create daana.bq.yaml with the bookstore_bq profile:

version: 1
domains:
  dab:
    workflow: "dab/workflow.yaml"
connections:
  file: connections.yaml
  profile: bookstore_bq

daana check validates the default project, --project selects the BigQuery declaration for the second check, and daana dar check dar/ checks the reporting documents. Check all three offline, without credentials:

Run in terminal

daana check
daana check --project daana.bq.yaml
daana dar check dar/

Expected output of daana check --project daana.bq.yaml

Project: <project>/daana.bq.yaml
Mode: offline
DAB: passed
Target profile: bookstore_bq (validated_offline)
DAS: disabled
DAR: disabled
Source schema: not requested
warning [project-profile] <project>/connections.yaml: [bookstore_bq] root: credentials_file '' does not exist

The BigQuery project passes offline despite the credentials warning. This check does not test database access or BigQuery SQL. The default project's daana check reports DAB: passed on the duckdb profile. The daana dar check dar/ command reports one document with three outputs.

daana check mapping is the same command as daana dab check mapping, which chapters 2 and 5 used. For the connected check, --model names the model, --connections names the profile file, and --connection selects the source database. Check the local mappings against DuckDB and inspect the valid and invalid counts:

Run in terminal

daana check mapping dab/mappings/ --model dab/model.yaml --connections connections.yaml --connection duckdb

Expected output

Checking 6 mapping(s) in directory: mappings

→ author-mapping.yaml ✓
→ book-mapping.yaml ✓
→ customer-mapping.yaml ✓
→ order-line-mapping.yaml ✓
→ order-mapping.yaml ✓
→ publisher-mapping.yaml ✓

Summary:
  Total: 6
  Valid: 6
  Invalid: 0

All 6 mappings of the project pass. On the full model of chapter 6, the same command reports 43 valid mappings. A mapping that passes on DuckDB can still fail on BigQuery, because its expressions must use BigQuery's SQL dialect. Some mappings cast to VARCHAR, while BigQuery uses STRING.

The check with --connection bookstore_bq was not run for this tutorial. With credentials, it would test the expressions in BigQuery's SQL dialect.

This GitHub Actions workflow is an example configuration. A successful run would pass the checks on pull requests. On a push to main, the workflow would deploy only after the checks pass. The ci/install-daana.sh file stands for the team's way of installing the CLI.

Read how the workflow sets up credentials and how its deploy job depends on check:

Configuration

name: daana

on:
  pull_request:
  push:
    branches: [main]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install daana
        run: ./ci/install-daana.sh # your team's way of fetching the daana binary
      - name: Offline checks
        run: |
          daana check
          daana check --project daana.bq.yaml
          daana dar check dar/
      - name: Connected mapping check
        env:
          BOOKSTORE_GCP_PROJECT: ${{ vars.BOOKSTORE_GCP_PROJECT }}
          BOOKSTORE_BQ_CREDENTIALS_FILE: ${{ runner.temp }}/bq.json
          BOOKSTORE_BQ_KEY: ${{ secrets.BOOKSTORE_BQ_KEY }}
        run: |
          printf '%s' "$BOOKSTORE_BQ_KEY" > "$BOOKSTORE_BQ_CREDENTIALS_FILE"
          daana check mapping dab/mappings/ --model dab/model.yaml \
            --connections connections.yaml --connection bookstore_bq

  deploy:
    if: github.event_name == 'push'
    needs: check
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - name: Install daana
        run: ./ci/install-daana.sh
      - name: Deploy to BigQuery
        env:
          BOOKSTORE_GCP_PROJECT: ${{ vars.BOOKSTORE_GCP_PROJECT }}
          BOOKSTORE_BQ_CREDENTIALS_FILE: ${{ runner.temp }}/bq.json
          BOOKSTORE_BQ_KEY: ${{ secrets.BOOKSTORE_BQ_KEY }}
        run: |
          printf '%s' "$BOOKSTORE_BQ_KEY" > "$BOOKSTORE_BQ_CREDENTIALS_FILE"
          daana deploy --project daana.bq.yaml --no-tui

The workflow reads the key from CI secrets and writes it to the credentials file. The --no-tui flag disables the logo and progress bar for clean log output.

daana deploy also runs the project's checks. A finding about a source table is a warning, so it does not stop deployment. The daana deploy --strict command stops on a finding. In this workflow, a finding of the separate connected check would fail the job.

The deploy job needs an initial daana install on its target, as step 5 describes.

Step 5: Deploy to the production database

The production database needs source tables, credentials, and mapping SQL that works in its SQL dialect. This tutorial does not load the stage source tables into the production database. The file data/seed.sql is written for DuckDB, and in production an ingestion tool loads the source tables, as What changes between environments describes. These commands were not run for this tutorial. In a successful run, daana install, daana deploy, and daana execute each exit with status 0. A query on the target then returns the expected rows.

Read how the BigQuery configuration sets the environment variables and selects the project declaration:

On the production database

export BOOKSTORE_GCP_PROJECT=...
export BOOKSTORE_BQ_CREDENTIALS_FILE=/path/to/service-account.json
daana install --project daana.bq.yaml
daana deploy --project daana.bq.yaml
daana execute --project daana.bq.yaml

PostgreSQL uses a declaration with profile: bookstore_pg, and Snowflake uses one with profile: bookstore_sf. Each uses the environment variables of its profile.

daana install runs once per target, and daana deploy runs after changes to the model, mappings, or workflow. A scheduler runs daana execute for each new batch of source data.

Step 6: Generate DAR for Dataform on BigQuery

Dataform creates and refreshes reporting tables and views from SQLX files Daana generates. Chapter 7's daana dar apply creates outputs directly on DuckDB. This step generates files for Dataform to run.

--preview reads the local model, and --out chooses the output directory. Generate the preview and check how many SQLX files it writes:

Run in terminal

daana dar generate dar/ --preview --out definitions/dar

Expected output

Preview uses local Daana DAB model.yaml; its attribute keys and columns may differ from the deployed DAB.
Generated 4 SQLX file(s) in definitions/dar.
Run the generated actions with Dataform in your project.

The preview writes four SQLX files for the dimension, latest view, line fact, and daily revenue. Daana generated them from the local model.yaml, not from the deployed DAB. The preview therefore cannot show whether they match the DAB in the production database. Inspect the start of definitions/dar/fct__order_line_daily_revenue.sqlx for the output table's name and partition and clustering columns:

config {
  type: "table",
  schema: "daana_dar",
  name: "fct__order_line_daily_revenue",
  bigquery: {
    partitionBy: "order_date",
    clusterBy: ["order_date", "order_line_amount_currency"]
  }
}

The configuration names the output table, partitions it by order_date, and clusters it by order_date and order_line_amount_currency.

The next command and the Dataform run were not run for this tutorial. A successful run means that the generated files compile in Dataform and that Dataform creates the outputs. Read the production command, which replaces --preview with the BigQuery profile so that Daana reads the deployed DAB:

On the production database

daana dar generate dar/ --connection bookstore_bq

In production, the generated files are reviewed and committed to the Dataform repository. Its workflow runs them. Changes to DAR documents or the deployed model require new files. The local preview does not test those operations.

What you can do now

You can select another target, verify its loaded data, and generate a Dataform preview. Daana created the release objects from the model and loaded their mapped values. It also generated the reporting SQLX files. Verify the data on the selected target, because passing configuration checks does not show where data was loaded.

The mapping SQL on the production database, the CI workflow, and the Dataform run still need testing in the production environment. The local results do not establish that those steps work there.

Continue with Check DAB configurations, Generate DAR for Dataform, and Daana next to an existing stack to apply that work in an existing platform.

Return to bookstore:

Run in terminal

cd ../bookstore

The terminal is in bookstore, and DuckDB is closed.