Skip to main content

Canonical Staging Schema & Adapter Interface

Foundation for all multi-ERP work. This design defines the canonical staging interface every ERP adapter must produce.

Status (2026-11-05)

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_sources defaults to empty (konsolidat#207): no adapter is built, the trial-balance upload is the canonical source, and each canonical model compiles to empty_relation(...) with its full typed column set.
  • Columns are typed, not all String as specified below (record_id Nullable(Int64), amount Nullable(Decimal(38, 9)), fiscal_year Int64, _loaded_at DateTime64(3), ...). The empty_relation([...]) block at the top of each canonical model is the authoritative column list.
  • is_credit was retired (konsolidat C1 / assert_is_credit_retired): silver derives debit and credit from the sign of amount.
  • partner_data_area_id was added to stg_gl_entries and stg_trial_balance (konsol#159); every adapter emits it (NULL today).
  • Exchange rates leave the adapter as TRUE rates: stg_d365_fo__exchange_rates divides by the ConversionFactor enum (#138).
  • Dimension harmonisation runs centrally in the canonical models, keyed per row on erp_source and entity (konsol#111), against epm_staging.dimension_mappings, not a seed.
  • The adapter contract is documented in models/staging/README.md in the product repo; test_canonical_gl_journals_balance fails 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:

  1. Adding a new ERP (SAP, ERPNext, D365 BC) requires modifying every bronze model
  2. There is no contract defining what a staging adapter must produce
  3. 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​

ModelPurposeKey Columns
stg_gl_entriesGL line itemserp_source, record_id, entity_id, posting_date, main_account, amount, partner_data_area_id
stg_trial_balancePeriod-end TB snapshotserp_source, entity_id, main_account, fiscal_year, opening/closing balance
stg_accountsChart of accountserp_source, account_id, account_name, account_type
stg_legal_entitiesCompany/entity mastererp_source, entity_id, entity_name, accounting_currency
stg_exchange_ratesFX rateserp_source, from_currency, to_currency, valid_from, exchange_rate
stg_budget_entriesBudget lineserp_source, entity_id, posting_date, main_account, amount
stg_fiscal_periodsFiscal calendarerp_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:

ValueERPAdapter exists
d365_foDynamics 365 Finance & Operationsyes
erpnextERPNextyes
d365_bcDynamics 365 Business Centralno
sap_s4SAP S/4HANAno
sap_eccSAP ECC 6.0no
sap_b1SAP Business Oneno

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:

  1. Write the adapter models in models/staging/<erp>/, following models/staging/README.md
  2. Add the ERP key to erp_sources
  3. 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
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 ColumnCanonical Column
RecId / SourceKeyrecord_id
dataAreaIdentity_id
AccountingDateposting_date
MainAccountmain_account
AccountingCurrencyAmountamount
TransactionCurrencyCodetransaction_currency
Rate ÷ ConversionFactorexchange_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_unique
  • tests/: assert_d365_gl_vouchers_balance, assert_conversion_factor_known, assert_is_credit_retired
  • Regression: the full dbt build suite (Testing Guide) passes unchanged

Success Criteria​

  1. All bronze models consume from canonical staging (no stg_d365__ refs): met
  2. dbt build passes with zero regressions: met
  3. Gold model outputs are byte-identical before and after: met at the time
  4. Adding a new ERP requires only adapter models and an erp_sources entry: met, with the caveat in Adding Dimensions about the literal dimension names in the canonical unioned CTE