---
title: "Land raw data unchanged and build tables · Agent Wranglers"
url: https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/raw-then-models
id: DEN-2
level: Recommended
updated: 2026-10-22
tokens: 963
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Land raw data unchanged and build tables in layers

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: DEN-2
- Level: Recommended
- Chapter: [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md)
- Rule: Load source data into a raw layer exactly as it arrived, then build cleaned and business tables from it in code. Only the loader writes to the raw layer.
- Check: A script reads every model and fails when a `marts` model reads `raw` directly. Only the loader's account can write to `raw`. Rebuilding every model from raw on staging gives the same row counts as production.
- How: This order is called ELT, for extract, load and then transform inside the warehouse. A raw copy lets you rebuild every later table when a transformation was wrong. Use three layers, such as `raw`, `staging` for renamed and typed columns, and `marts` for the tables dashboards read. Each layer reads only from the one before it. Add the load time and the source name to every raw row. Raw personal data stays only as long as [Expire warehouse data by partition](https://agentwranglers.se/playbook/data-engineering/personal-data-in-the-warehouse/warehouse-retention.md) allows.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
import re
from pathlib import Path

ALLOWED_SOURCES = {"staging": {"raw"}, "marts": {"staging", "marts"}}

def test_layers_read_only_the_layer_before():
    for model in Path("models").glob("*/*.sql"):
        layer = model.parent.name
        read = set(re.findall(r"\b(raw|staging|marts)\.", model.read_text()))
        assert read <= ALLOWED_SOURCES[layer], f"{model} reads {read - ALLOWED_SOURCES[layer]}"
```

### JavaScript example

```js
import { readdirSync, readFileSync } from 'node:fs';

const ALLOWED_SOURCES = { staging: ['raw'], marts: ['staging', 'marts'] };

for (const layer of Object.keys(ALLOWED_SOURCES)) {
  for (const file of readdirSync(`models/${layer}`)) {
    const read = [...readFileSync(`models/${layer}/${file}`, 'utf8').matchAll(/\b(raw|staging|marts)\./g)].map(match => match[1]);
    const wrong = read.filter(source => !ALLOWED_SOURCES[layer].includes(source));
    if (wrong.length) throw new Error(`${layer}/${file} reads ${wrong.join(', ')}`);
  }
}
```

## Sources

- [dbt How we structure our dbt projects](https://docs.getdbt.com/best-practices/how-we-structure/1-guide-overview)

## Related rules

- DEN-26 [Expire warehouse data by partition](https://agentwranglers.se/playbook/data-engineering/personal-data-in-the-warehouse/warehouse-retention.md) in Data engineering

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/raw-then-models">Agent Wranglers data engineering playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Keep analytics queries off the production database](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/separate-warehouse.md)
- Next: [Build every warehouse table from reviewed code](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/transformations-as-code.md)
- [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md)
