If dbt is your source of truth for database schemas and you use Metabase as your analytics tool, dbt-metabase can propagate table relationships, model and column descriptions and semantic types (e.g. currency, category, URL) to your Metabase data model, and extract questions and dashboards as exposures in your dbt project.
All commands require authentication against the Metabase API using one of these methods:
API key (--metabase-api-key)
Strongly recommended for automation, see documentation (Metabase 49 or later).
Username and password (--metabase-username / --metabase-password)
Fallback for older versions of Metabase and smaller instances.
Exporting Models
Let's start by defining a short sample schema.yml as below.
models:
- name: stg_users
description: User records.
columns:
- name: id
description: Primary key.
data_tests:
- not_null
- unique
- name: email
description: User's email address.
- name: group_id
description: Foreign key to user group.
data_tests:
- not_null
- relationships:
arguments:
to: ref('groups')
field: id
- name: stg_groups
description: User groups.
columns:
- name: id
description: Primary key.
data_tests:
- not_null
- unique
- name: name
description: Group name.
This is already enough to propagate the primary keys, foreign keys and descriptions to Metabase:
dbt-metabase models \
--manifest-path target/manifest.json \
--metabase-url https://metabase.example.com \
--metabase-api-key mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX= \
--metabase-database business \
--include-schemas public
Open Metabase and go to Settings > Admin Settings > Table Metadata, you will notice that id column in stg_users is now marked as "Entity Key" and group_id is a "Foreign Key" pointing to id in stg_groups.
Try running dbt-metabase models --help to see all the options available for fine tuning.
Foreign Keys
Native relationship tests and column-level constraints are the recommended ways of defining foreign keys, however you can override them with fk_target_table and fk_target_field meta fields. If both are set for a column, meta fields take precedence.
- name: country_id
description: FK to User's country in the dim_countries table.
config:
meta:
metabase.fk_target_table: analytics_dims.dim_countries
metabase.fk_target_field: id
You can provide fk_target_table as schema_name.table_name or just table_name to use the current schema. If your model has an alias, provide that alias rather than the original name.
Semantic Types
Now that we have foreign keys configured, let's tell Metabase that email column contains email addresses:
By default, dbt-metabase waits for tables and columns to be synchronized between your dbt project and Metabase database, otherwise the export fails when the sync timeout expires.
If you have known discrepancies between dbt and Metabase and wish to proceed without synchronization, set the sync timeout to zero (e.g. --sync-timeout 0). This is discouraged, because you will still encounter errors if you have a table or column in your dbt project that is missing from Metabase and dbt-metabase attempts to export it.
Exposure Extraction
dbt-metabase allows you to extract questions and dashboards from Metabase as dbt exposures in your project:
Once the execution completes, check your output path for exposures files containing descriptions, creator details and links for Metabase questions and dashboards:
exposures:
- name: number_of_orders_over_time
description: '
### Visualization: Line
A line chart depicting how order volume changes over time
#### Metadata
Metabase Id: __8__
Created On: __2021-07-21T08:01:38.016244Z__'
type: analysis
url: https://metabase.example.com/card/8
maturity: medium
owner:
name: Indiana Jones
email: indiana@example.com
depends_on:
- ref('orders')
Native query questions will have SQL code blocks inside the descriptions, formatted to look nice in dbt docs. These YAML files can be committed to source control to understand how exposures change over time.
Try running dbt-metabase exposures --help to see all the options available for fine tuning.
Configuration
There are 3 levels of configuration in decreasing order of precedence:
CLI arguments, e.g. --manifest-path target/manifest.json
Environment variables, e.g. MANIFEST_PATH=target/manifest.json
Configuration file, e.g. manifest_path: target/manifest.json
Try running --help for any command to see the full list of CLI arguments and environment variables.
A configuration file can be created in ~/.dbt-metabase/config.yml for dbt-metabase to pick it up automatically or anywhere else by specifying dbt-metabase --config-path path/to/config.yml (must come before the command). Here is an example YAML file:
config:
manifest_path: target/manifest.json
metabase_url: https://metabase.example.com
metabase_api_key: mb_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=
# Configuration specific to models command
models:
metabase_database: business
# Configuration specific to exposures command
exposures:
output_path: models
Note that common configurations are in the outer block and command-specific ones are in separate blocks.
Programmatic API
Alternatively, you can invoke dbt-metabase programmatically. Below is the equivalent of CLI examples: