Develop
sqb rename and sqb mv
Rename or move a model, or rename a column, and update every reference in one verified step.
sqb rename and sqb mv change a name everywhere the compiler knows it is used, instead of leaving you to find every __ref, fixture, and header by hand. Every edit comes from compiled facts, not text search: a column rename follows the columns each query actually binds, so a same-named column of another table is left alone.
Nothing is written until the edited project compiles. The command applies its edits to a scratch copy of the project, compiles it the way sqb compile does, and only then writes the files. If anything cannot be rewritten safely, the command lists those locations and writes nothing.
sqb rename <model> <new_name> [<model> <new_name> ...] [flags]sqb rename --from-file <renames.tsv> [flags]sqb mv <model> <path/to/new_file.sql | folder/> [<model> <destination> ...] [flags]sqb mv --from-file <moves.tsv> [flags]sqb rename <model>.<column> <new_name> [--cascade] [flags]sqb rename stg_orders stg_order_linessqb mv fact_orders models/reporting/ # keeps the namesqb mv fact_orders models/reporting/order_facts.sql # moves and renamessqb rename stg_orders.amount revenuesqb rename stg_orders.amount revenue --cascadeTargets are written the way sqb lineage reads them: a bare name is a model, and <model>.<column> is one of its columns. The model: and column: prefixes are optional, so sqb rename model:stg_orders stg_order_lines and sqb rename column:stg_orders.amount revenue still work. A target that names a source, seed, or function is refused, because those are not renamed by these commands.
Rename or move
Section titled “Rename or move”sqb rename changes names and never moves files: a model rename keeps the file in its folder and renames it to <new_name>.sql. sqb mv changes where a model lives and always takes a path: a new .sql file path, which also sets the model name, or a folder ending in /, which keeps it. Neither command picks a folder for you, because a folder can change the schema, materialization, tags, and declaration visibility the model gets from path defaults.
# Name only: the file stays in models/orders/intermediate/clean/sqb rename orders__int_clean__order_lines orders__int_clean__order_items
# Name and a specific folder, in one verified stepsqb mv orders__int_clean__order_lines models/orders/intermediate/enriched/orders__int_enriched__order_lines.sql
# Folder only: the name stayssqb mv orders__int_clean__order_lines models/orders/intermediate/clean/web/Move several models at once
Section titled “Move several models at once”sqb mv takes several <model> <destination> pairs, or --from-file with one pair per line, separated by a tab or spaces. Blank lines and lines starting with # are ignored.
sqb mv stg_order_cents models/finance/ stg_order_refunds models/finance/sqb mv --from-file moves.tsv --dry-runstg_order_cents models/finance/stg_order_refunds models/finance/The moves are checked together, as one change: nothing is written unless the project compiles after all of them. Declarations and unit tests go where the final state needs them, so a macro whose users all move goes straight to their new folder.
A batch is refused when it moves the same model twice, moves two models to the same file, gives two models the same new name, or gives a model the name another model of the batch gives up: the old name keeps working through a compatibility view after the rename, so it must stay free. With --dry-run, the output lists the combined edits of every move.
Rename several models at once
Section titled “Rename several models at once”sqb rename takes several <model> <new_name> pairs the same way, on the command line or with --from-file:
sqb rename stg_orders stg_order_lines fact_orders fact_order_linessqb rename --from-file renames.tsv --dry-runThe renames are checked together, and each renamed model gets its own migrate_from. The same refusals as for batch moves apply: a model renamed twice, two models given one name, or a model taking a name another model of the batch gives up. Column renames and --cascade take one <model>.<column> <new_name> per command; a batch that names a column is refused.
What changes
Section titled “What changes”Renaming or moving a model rewrites:
- every
__ref("<name>")in models, tests, scenarios, audits, hooks, and SQL functions; __ref__<name>and__expected__<name>fixture CTEs in tests and scenarios;cursor_inputskeys,relationships (to ...)audits, and__ref("<name>")inside quoted SQL inMODELheaders and reusableSCHEMAdeclarations;relationshipsaudits (to:andfield:) and__ref("<name>")in source and seed YAML.- the model’s name and folder in job and job group
selectandexcludeselectors; see Rename models in jobs.
The model’s own unit tests, the test files whose every TEST block targets only that model, follow it:
- a file named
test_<name>.sqlortest_<name>__<behavior>.sqlis renamed to the new name; TEST (name "<name>__<behavior>")takes the new name as its subject;- a test file in the folder that mirrors the model,
tests/unit/<folders>/formodels/<folders>/, moves to the folder that mirrors the destination. Test files kept elsewhere stay where they are. - a test file stays in its folder, still renamed, when a macro, enum, or constant it uses would resolve to a different declaration, or to none, from the destination folder, such as a test macro in
tests/unit/<folders>/_sqlbuild/_macros/. The output’s notes name the file and the declaration.
Tests that also target other models keep their file and name; only their fixtures and references change.
Moving a model to another folder also moves the macros, enums, constants, reusable schemas, generic audits, and named hooks it uses when their placement must change, following the same rules sqb compile enforces:
- a declaration only the moved model uses moves with it, into the destination folder’s matching role;
- a declaration other resources still use moves to the lowest folder they share, for example
models/staging/_sqlbuild/macros/; - when that folder would be an authored root such as
models/, or the users span resource trees such as models and tests, it moves to the top-level role (macros/,enums/,constants/,schemas/, …).
A moved declaration file takes the shape of the role folder it lands in. In an empty role it keeps its group folder, such as money/ in _macros/money/cents.py, or stays direct if it had none. In a role whose files all sit in group folders it keeps its group, and a file without a group is refused: move it into a group folder of its current role first. In a role whose files sit directly in it, the group folder is dropped.
When a Python file moves, such as a carried macro module, every project Python file that imports it is updated: macros, Python nodes, custom rules, and helpers. from <module> import ..., grouped and parenthesized imports, import <module> as <name>, and from <package> import <module> point at the new module path. Relative imports keep their style when the new target is close, such as from ..text.cents import to_cents, and become absolute when they would climb further. The move is refused, naming the file and line, when an import cannot be rewritten safely:
import <module>withoutas, because code reads the dotted name itself;from <package> import *from the moved module’s package;importlib.import_module("<module>")or__import__("<module>")naming the moved module;from <package> import <module>, <other>where the other name does not move with it;from <package> import <module>when the package’s__init__.pyalso binds that name, or when the move renames the module;- a project Python file that cannot be read as UTF-8, because its imports cannot be checked.
Declarations are moved, never copied. Folders the move leaves empty are removed.
Renaming a column rewrites the model that produces it and every query that reads it:
- the producing projection, as
amount AS revenue, or the existing alias; - every downstream reference, including filters, joins, grouping, and ordering;
- column declarations, including the model’s own
MODEL (columns (...))entries with or without commas between them, and column-valued config such asunique_key,cursor, andpartition_by, andcursor_inputsandrelationships (field ...)entries that point at the column; - string arguments of audits attached in
MODELheaders, such aswhere_condition "is_flagged = TRUE"orexpression "amount > 0", when the generic audit’s SQL definition splices the argument in as SQL with@where_condition. These arguments are parsed as SQL expressions, so only references to the audited model’s column change: string literals stay as written, and a subquery reading another model, such ascustomer_id IN (SELECT customer_id FROM __ref("vip_customers")), keeps that model’s column. You are asked to edit an argument by hand when it cannot be parsed, when it reads a relation whose columns SQLBuild does not know, when the audit quotes it as a string with@'prefix', when it joins on the column withUSINGorNATURAL, or when no SQL definition of the audit is found. A lambda parameter named like the column, as inlist_filter(xs, lambda customer_id: customer_id > 0), keeps its name; - fixture columns of
__ref__<model>and__expected__<model>CTEs.
By default a column rename is one step: downstream models keep their output names, so SELECT o.amount becomes SELECT o.revenue AS amount. With --cascade, a downstream model that passes the column through unchanged, as a bare column, a same-named alias, or SELECT *, renames its output too, and the rename continues through its own consumers.
History
Section titled “History”A renamed model keeps its warehouse history through a model migration. Whenever the model’s relation moves and its materialization keeps warehouse data (table, view, incremental, or snapshot), the command adds migrate_from <old_name> to the model header, so the move does not depend on automatic discovery finding exactly one match. The next sqb build migrates the relation and leaves a compatibility view at the old name. A move that keeps the relation, such as a folder change in the same schema, adds nothing.
On a target that never built the old name, the origin is missing. By default the plan warns and the model builds fresh; set missing_migration_origin on a target to require confirmation or stop the build instead.
A renamed column of an incremental or snapshot model gets <new> (migrate_from <old>) in its columns block, so the next build renames it in place as a column migration. Table and view models are rebuilt with the new column anyway; they get the declaration only when the model itself declares migrate_from.
The command refuses a rename while the model, or the column, still declares migrate_from from an earlier rename. Build it on every target and remove the declaration first.
Existing rule failures
Section titled “Existing rule failures”A project that already fails some project rules, such as a folder holding more models than a layout rule allows, can still be refactored, so that a move can repair it. Only rule failures are tolerated: a project with syntax, binding, or reference errors is refused before planning, as is any project sqb compile cannot load.
A rule failure that the project already had, at the same place and with the same message, doesn’t block the change. Neither does one whose numbers only went down, such as a folder that now holds 41 models instead of 44. A new or worse failure blocks the write. Rules stay enabled throughout.
The output, including --dry-run, lists them under Existing rule failures, with how many the change resolved. Some rules report a failure at one sample file of a group, so moving part of the group can block the move even when it helps. Move the whole group in one batch instead: moving the direct models of a folder into a sub-folder at once, so that it turns from a leaf into a branch, is sqb mv a models/entry/records/ b models/entry/records/ c models/entry/records/.
Refusals
Section titled “Refusals”The command refuses, and writes nothing, when:
- the project does not compile before the change, other than existing rule failures;
- the new name already belongs to another model or column, or the destination file exists;
- a moved declaration file is not in a group folder, but the role folder it must move to keeps its files in group folders;
- a declaration a moved model uses must change folder, but its file also holds declarations that must stay, or it is not an authored project file, or a file with its name already exists at the new folder;
- a move would leave no model in the old schema, so the old relation could not be found;
- a reference is produced by a macro, or a column is read inside SQL that a macro generates;
- a column reaches a model through
SELECT *in a subquery, aUSINGorNATURALjoin, or an unqualified name that could belong to several tables; - a downstream model’s
SELECT *would silently rename its output, without--cascade; - Python code or Python node SQL strings name a model being renamed, or a column being renamed. A move that keeps the model’s name leaves Python references to it as they are;
- a Python import of a moved Python file cannot be rewritten safely.
A rename whose new name belongs to another layer’s folder, under the layer-folder rules SQBRPROJECT102 and SQBRPROJECT105, fails verification because sqb rename keeps the file where it is. When those rules are the only failures, the output says where the model belongs and prints the one-step sqb mv command, with that layer’s folder and the current sub-folders filled in:
orders__int_enriched__order_lines__web belongs under models/orders/intermediate/enriched/ Rename and move in one step: sqb mv orders__int_clean__order_lines__web models/orders/intermediate/enriched/web/orders__int_enriched__order_lines__web.sql (change the folder if you want it somewhere else)When the current path has no folder for the model’s current layer to swap, or the new layer has no canonical folder, the destination is models/<folder>/<new_name>.sql, with a <folder> placeholder for you to fill in.
Each location is listed with its file, line, and column, with the fix. Edit those locations, or pass the model or column to the macro as an argument, and run the command again. The command never applies part of a rename.
References produced by a macro are only possible when a project turns off explicit references ([references] enforce_explicit = false); such projects get the refusal list for them.
| Flag | Description |
|---|---|
--cascade |
For column renames, rename the output of every downstream model that passes the column through. |
--dry-run |
Print every edit and verify the edited project compiles, without writing files. |
--from-file <file> |
Read <model> <destination> pairs for sqb mv, or <model> <new_name> pairs for sqb rename, one per line, and apply them together. |
--json |
Print the result as JSON on stdout. Progress goes to stderr. |
Output
Section titled “Output”Rename model stg_orders -> stg_order_lines├── models/marts/fact_orders.sql│ └── 24:13 reference __ref("stg_orders") -> __ref("stg_order_lines")├── models/staging/stg_order_lines.sql (moved from models/staging/stg_orders.sql)│ └── 1:8 migration + migrate_from stg_orders└── tests/unit/test_fact_orders.sql └── 4:1 fixture __ref__stg_orders -> __ref__stg_order_linesMigrations└── stg_order_lines migrate_from stg_orders keeps the relation's history and its old name workingCompiled: ok, 3 files changedWith --json, the result has operation, target, new_name, destination, status (applied, dry_run, refused, or compile_failed), files with every edit, migrations, renamed_columns, manual, blocking, notes (files deliberately kept in place, with the reason), compile.errors, and rule_baseline with the project’s rule failures before the change, those remaining, and those resolved. A batch move also has moves, every target and destination in order, and a batch rename has renames, every target, new_name, and destination; target, new_name, and destination then describe the first one.
When the request is invalid or the project does not compile, --json prints an error document instead: {"error": {"code", "message", "help"}}, and for a project that does not compile also "status": "project_failed" and compile.errors.
Exit codes
Section titled “Exit codes”| Code | Meaning |
|---|---|
0 |
Applied, or a dry run that compiles. |
1 |
Refused, the edited project did not compile, or the request was invalid. No files were changed. |
Limitations
Section titled “Limitations”- Descriptions and comments are not rewritten.
- Queries outside the project that read the old relation keep working through the old-name compatibility view until it expires.