Skip to content

Refactor

Column migrations

Keep a column's history when you rename it in an incremental or snapshot model.

Rename a column with sqb rename fact_orders.amount revenue. On the next build, SQLBuild renames the column in the warehouse table, so every row keeps its value under the new name and nothing is replayed.

sqb rename fact_orders.amount revenue
sqb plan
Column migrations (1)
└── fact_orders migrate columns
└── amount -> revenue rename in place

Column migrations apply to incremental models, including microbatch models, and snapshot models. Tables and views are rebuilt on every build, so they don’t need them.

Without a column migration, on_schema_change compares columns by name and sees one column removed and another added. With the default append_new_columns, the new column starts empty and older rows keep their values under the old name. With sync_all_columns, the old column and its data are dropped.

sqb rename <model>.<column> renames the column in the model and in every query that reads it, and writes a migrate_from declaration on the renamed column. You can also write the declaration yourself:

MODEL (
description "One row per order",
materialized incremental,
incremental_strategy delete_insert,
unique_key order_id,
cursor order_date,
columns (
revenue (migrate_from amount),
),
);
SELECT order_id, order_date, amount AS revenue
FROM __source("raw_orders")

On the next build, SQLBuild renames amount to revenue in the existing table, records the rename, and then builds the model incrementally, so the new delta lands in revenue.

Once the rename is recorded, migrate_from has no further effect, and SQLBuild reports (M108) that it can be removed. Keep it until every target that needs the rename has built. On a target where the table doesn’t exist yet, the declaration does nothing and the table is created with the new name.

A column entry can hold migrate_from on its own or together with type, nullable, description and audits. A column inherited from a reusable schema can add migrate_from without repeating its metadata.

Compilation rejects a declaration when:

  • migrate_from names the column itself, or a qualified name instead of one column.
  • Two columns declare the same migrate_from.
  • A column migrates from another column that declares its own migrate_from. Chains and swaps can’t be renamed in place safely. Rename through a name no other column uses, or rebuild with --full-refresh.
  • The model isn’t incremental or snapshot, unless the model itself declares migrate_from (see below).

On a table or view model that declares its own model-level migrate_from, a column’s migrate_from only names the old column in the old-name view.

SQLBuild detects a rename by itself when renames are the only change to the query: each removed column has exactly one new column with the same expression. For example, amount becomes amount AS revenue. If you changed anything else in the query, declare migrate_from, or use sqb rename.

Detection never guesses. If more than one removed or new column has the same expression, nothing is renamed automatically. Two columns that swap names keep their names. Automatically detected renames are marked (automatic) in the plan, and they never stop a build: when one can’t be applied, the columns follow on_schema_change.

A near match, where the expression changed, is not a rename. It follows on_schema_change, and sqb plan suggests the declaration until the change has been built:

├── schema diff:
+ revenue (added)
similar to amount; if this is a rename, add revenue (migrate_from amount)
- amount (removed)

A declared rename with other query changes still renames the column in place. The other changes are a query change, so the model’s replay_on_change decides how much history is rebuilt, as it would without the rename.

sqb plan and sqb build list column migrations before the models they change. The decision is based on the table’s current columns first, then on recorded renames:

Decision Meaning
rename in place The old column exists and the new one doesn’t. Rename it.
already renamed, record The new column exists, the old one doesn’t, and no rename is recorded. An interrupted rename leaves this. Record it without renaming again.
done The rename is recorded and the new column exists. Nothing to do.
conflict Both columns exist. The build stops (M110). Drop one of them, or remove migrate_from and let on_schema_change handle the columns.
source missing Neither column exists and no rename is recorded. The plan warns (M109) and the columns follow on_schema_change. A target can require confirmation or stop instead with missing_migration_origin.
source still produced The query still selects the old column. The build stops (M111).
unsupported The warehouse can’t rename this table’s columns (see Adapter support). The build stops (M112).
Adapter Statement
Snowflake, PostgreSQL, DuckDB, MotherDuck, BigQuery ALTER TABLE ... RENAME COLUMN ... TO ...
Databricks ALTER TABLE ... RENAME COLUMN ... TO ..., which needs Delta column mapping
SQL Server EXEC sp_rename '<schema>.<table>.<column>', '<new name>', 'COLUMN'

On Databricks, a Delta table column can be renamed only when the table uses name-based column mapping. SQLBuild checks this during planning and doesn’t change the table for you. A declared rename on a table without column mapping stops the build (M112) with the statement that enables it:

ALTER TABLE <table> SET TBLPROPERTIES (
'delta.columnMapping.mode' = 'name',
'delta.minReaderVersion' = '2',
'delta.minWriterVersion' = '5'
);

Enabling column mapping upgrades the table’s Delta protocol, which older readers may not support. An automatically detected rename on such a table is skipped with a warning (M112).

  • Expressions are compared ignoring formatting, comments, and the case of keywords and unquoted identifiers. Detection compares the query the table was last built from with the current query, so it applies to the build that makes the change.
  • “The only change” means the same CTEs, FROM, joins, filters, grouping and other columns. Output columns may be reordered, unless the query groups, orders or uses DISTINCT ON by position.
  • A new name used in ORDER BY or QUALIFY counts as the old name only when SQLBuild can prove it refers to the output column: every input relation’s columns are known, the new name isn’t an input column, and the old name either isn’t one or passes through unchanged.
  • A rename that is the whole change is not a query change, whether declared or detected. It doesn’t trigger replay_on_change and doesn’t replay downstream models. Downstream models you edit to use the new name follow their own replay_on_change.
  • If the renamed column is the model’s cursor, the next delta starts from the watermark stored under the old name.
  • Renames run after model migrations and before any model builds, one model at a time. SQLBuild reads the table’s columns again first, so a column is never renamed twice. On PostgreSQL, SQL Server, DuckDB and MotherDuck, a model’s renames and their records commit together; elsewhere an interrupted run recovers through already renamed, record.
  • After the rename, other differences follow on_schema_change: append_new_columns adds new columns, sync_all_columns also drops removed ones and changes types, and fail fails only when a difference other than the rename remains.
  • Renames are recorded in a _sqlbuild_column_migrations table in the model’s schema. Janitor never archives it.
  • On PostgreSQL, views that select the renamed column keep working with their own output names, and the new name is created in lower case. On SQL Server, sp_rename warns that renaming can break scripts and stored procedures that use the old name.