Canonical Staging Schema & Adapter Interface
Foundation for all multi-ERP work. This design defines the canonical staging interface every ERP adapter must produce.
Implemented. models/staging/ holds 7 canonical models, a 16-model D365 F&O adapter and a 7-model ERPNext adapter; see Staging for the current inventory. Where the code moved on from this design:
erp_sourcesdefaults to empty (konsolidat#207): no adapter is built, the trial-balance upload is the canonical source, and each canonical model compiles toempty_relation(...)with its full typed column set.- Columns are typed, not all
Stringas specified below (record_idNullable(Int64),amountNullable(Decimal(38, 9)),fiscal_yearInt64,_loaded_atDateTime64(3), ...). Theempty_relation([...])block at the top of each canonical model is the authoritative column list. is_creditwas retired (konsolidat C1 /assert_is_credit_retired): silver derives debit and credit from the sign ofamount.partner_data_area_idwas added tostg_gl_entriesandstg_trial_balance(konsol#159); every adapter emits it (NULL today).- Exchange rates leave the adapter as TRUE rates:
stg_d365_fo__exchange_ratesdivides by theConversionFactorenum (#138). - Dimension harmonisation runs centrally in the canonical models, keyed per row on
erp_sourceand entity (konsol#111), againstepm_staging.dimension_mappings, not a seed. - The adapter contract is documented in
models/staging/README.mdin the product repo;test_canonical_gl_journals_balancefails the build when a voucher does not net to zero. - konsolidat#221 proposes removing ERP staging wholesale in favour of the upload.
Problem (as written)
Konsolidat's 15 staging models (then named stg_d365__*) were hardcoded to D365 F&O OData entity names and column conventions. The bronze layer referenced them directly. This meant:
- Adding a new ERP (SAP, ERPNext, D365 BC) requires modifying every bronze model
- There is no contract defining what a staging adapter must produce
- Column names leak D365 conventions (
dataAreaId,RecId) into layers that should be ERP-agnostic
The silver and gold layers are already ERP-agnostic. The gap is between raw ERP data and bronze.
Solution
Introduce a canonical staging interface: a set of 7 staging models with standardised column names that every ERP adapter must produce. Each canonical model UNION ALLs the per-ERP adapters listed in var('erp_sources').
Architecture
Raw ERP Data (Airbyte, epm_raw)
↓
Per-ERP Adapters (e.g., stg_d365_fo__gl_entries)
↓
Canonical Staging (stg_gl_entries — UNION ALL from adapters)
↓
Bronze (type casting, ClickHouse materialisation)
↓
Silver → Gold (unchanged)
Canonical Models
| Model | Purpose | Key Columns |
|---|---|---|
stg_gl_entries | GL line items | erp_source, record_id, entity_id, posting_date, main_account, amount, partner_data_area_id |
stg_trial_balance | Period-end TB snapshots | erp_source, entity_id, main_account, fiscal_year, opening/closing balance |
stg_accounts | Chart of accounts | erp_source, account_id, account_name, account_type |
stg_legal_entities | Company/entity master | erp_source, entity_id, entity_name, accounting_currency |
stg_exchange_rates | FX rates | erp_source, from_currency, to_currency, valid_from, exchange_rate |
stg_budget_entries | Budget lines | erp_source, entity_id, posting_date, main_account, amount |
stg_fiscal_periods | Fiscal calendar | erp_source, calendar_id, fiscal_year, start_date, end_date |
erp_source Column
Every canonical model includes erp_source (String) identifying the source ERP. test_erp_source_valid allows:
| Value | ERP | Adapter exists |
|---|---|---|
d365_fo | Dynamics 365 Finance & Operations | yes |
erpnext | ERPNext | yes |
d365_bc | Dynamics 365 Business Central | no |
sap_s4 | SAP S/4HANA | no |
sap_ecc | SAP ECC 6.0 | no |
sap_b1 | SAP Business One | no |
erp_sources Variable
vars:
erp_sources: [] # default: trial-balance upload only
# erp_sources: [d365_fo] # or at run time: dbt build --vars '{erp_sources: [d365_fo]}'
Each adapter model carries {{ config(enabled = '<erp>' in var('erp_sources', [])) }} in its body: a +enabled config in dbt_project.yml cannot see the project's own vars: block. Adding a new ERP is:
- Write the adapter models in
models/staging/<erp>/, followingmodels/staging/README.md - Add the ERP key to
erp_sources dbt build: the canonical models union the new adapter
Canonical Column Specifications (original design)
The lists below are the design's; types in the code are stricter (see Status). Names are current except where noted.
stg_gl_entries
erp_source, record_id, entity_id, posting_date, fiscal_year, fiscal_period,
main_account, account_name, amount (signed: positive debit, negative credit),
transaction_currency_amount, transaction_currency, description, journal_number,
posting_type, ledger_account, partner_data_area_id,
<one column per declared dimension>, _loaded_at, _raw_id
Dropped since the design: debit_amount, credit_amount, currency_amount, is_credit.
stg_trial_balance
erp_source, entity_id, main_account, account_name, fiscal_year,
opening_balance, debit_amount, credit_amount, closing_balance,
currency_code, account_type, partner_data_area_id, _loaded_at, _raw_id
stg_accounts
erp_source, account_id, account_name, account_type, account_category,
debit_credit_default, chart_of_accounts, is_suspended, _loaded_at, _raw_id
stg_legal_entities
erp_source, entity_id, entity_name, accounting_currency, reporting_currency,
party_number, country_region, _loaded_at, _raw_id
stg_exchange_rates
erp_source, from_currency, to_currency, valid_from, valid_to,
exchange_rate (TRUE rate), rate_type, _loaded_at, _raw_id
stg_budget_entries
erp_source, record_id, entity_id, posting_date, main_account, amount,
transaction_amount, transaction_currency, budget_model, budget_status,
<one column per budget dimension>, _loaded_at, _raw_id
stg_fiscal_periods
erp_source, calendar_id, calendar_name, fiscal_year, start_date, end_date,
_loaded_at, _raw_id
D365 F&O Adapter Mapping
| D365 F&O Column | Canonical Column |
|---|---|
RecId / SourceKey | record_id |
dataAreaId | entity_id |
AccountingDate | posting_date |
MainAccount | main_account |
AccountingCurrencyAmount | amount |
TransactionCurrencyCode | transaction_currency |
Rate ÷ ConversionFactor | exchange_rate |
_airbyte_extracted_at | _loaded_at |
_airbyte_raw_id | _raw_id |
File Structure (current)
models/staging/
├── _staging__sources.yml # epm_gold and epm_staging sources (konsol-owned tables)
├── d365_fo/ # 16 models: 7 canonical feeders + 9 D365-only helpers bronze reads
│ ├── _d365_fo__sources.yml # d365_raw (database epm_raw)
│ ├── _d365_fo__models.yml
│ └── (16 adapter models, prefix stg_d365_fo__)
├── erpnext/ # 7 models, one per canonical target
│ ├── _erpnext__sources.yml # erpnext_raw (database epm_raw)
│ ├── _erpnext__models.yml
│ └── (7 adapter models, prefix stg_erpnext__)
└── canonical/ # 7 canonical models
├── _canonical__models.yml
└── stg_gl_entries.sql, stg_trial_balance.sql, stg_accounts.sql, stg_legal_entities.sql,
stg_exchange_rates.sql, stg_budget_entries.sql, stg_fiscal_periods.sql
Testing
- Schema tests in
_canonical__models.yml tests/staging/:test_canonical_gl_entries_schema,test_canonical_gl_entries_not_null,test_canonical_gl_journals_balance,test_erp_source_valid,test_dimension_mappings_uniquetests/:assert_d365_gl_vouchers_balance,assert_conversion_factor_known,assert_is_credit_retired- Regression: the full
dbt buildsuite (Testing Guide) passes unchanged
Success Criteria
- All bronze models consume from canonical staging (no
stg_d365__refs): met dbt buildpasses with zero regressions: met- Gold model outputs are byte-identical before and after: met at the time
- Adding a new ERP requires only adapter models and an
erp_sourcesentry: met, with the caveat in Adding Dimensions about the literal dimension names in the canonicalunionedCTE