spawn script run
Usage
spawn script run <script> [options] Runs a script against the configured database.
Scripts are for operational SQL that isn’t a schema change: a backfill, a reindex, a one-off report, a VACUUM. A script is rendered exactly as a migration is — the same includes, variables, filters and secret() values — and its output is streamed back as it runs.
Arguments
Section titled “Arguments”<script>— Required. Path to the script, relative to the scripts folder. For example,maintenance/reindexrunsscripts/maintenance/reindex/script.sql.
Scripts may be nested however suits the project. Create one with spawn script new.
How it differs from a migration
Section titled “How it differs from a migration”migration apply | script run | |
|---|---|---|
| Runs more than once | No, unless --retry | Yes, as often as you like |
| Run pinned version | By default | Automatically, with pinned files if pinned |
| Recorded in | migration_history | script_run |
| Later runs gated on history | Yes | No |
Nothing about a script run is gated on its history. The recorded rows are an audit trail — what was run, against what, when, and whether it worked — not a lock on running it again.
Transactions
Section titled “Transactions”The script owns its own transaction, exactly as a migration’s up.sql does. Wrap the body in BEGIN; / COMMIT; if you want one, and leave it out for statements that cannot run inside a transaction, such as CREATE INDEX CONCURRENTLY, VACUUM, or ALTER TYPE ... ADD VALUE.
Output
Section titled “Output”Results are streamed to stdout as they arrive, so a long report appears as it is produced and stdout stays pipeable.
Diagnostics (NOTICE, ERROR, and the rest) are not streamed. They are collected while the script runs and the last of them is printed to stderr once it finishes, because they are the database’s own output and may echo a secret() value back – see Secrets below. Only a bounded tail is kept, which is the part that explains a failure, so a chatty script will not show every NOTICE it emitted.
If any statement reports an error the command exits non-zero. Spawn sets ON_ERROR_STOP before every script, so the first failing statement stops the run; a script that turns that setting off is reporting its own success and will be recorded as having succeeded.
Pinning
Section titled “Pinning”Pinning is automatic. If the script has been pinned with spawn script pin, its {% include %}s resolve against the recorded tree; if it has not, they resolve against the current components. A pinned script is never run against other components by accident.
Every run says which tree it used, on stderr:
Using the pinned component tree for 'maintenance/reindex' (scripts/maintenance/reindex/config.toml).Using the current component tree for 'maintenance/reindex' (not pinned).Two flags override the choice:
--pinnedasserts that the script must be pinned. If it records no pin the run fails instead of falling back to the current components. Worth setting in CI, where a pin that silently disappeared should be an error rather than a different run.--no-pinignores the pin and uses the current components, for trying a component edit before re-pinning. The stderr line reports(--no-pin)so the choice is on the record.
Passing both is rejected.
Note what a pin does and does not freeze. It fixes the components a script includes. It does not fix the script’s own source: script.sql lives under scripts/, outside the pinned tree, and is always read live. Editing a script changes what the next pinned run executes.
Either way, the pin hash of the tree the run resolved against is recorded in the audit trail. For an unpinned run that hash is recomputed from the current tree at run time, which identifies the tree but does not retain it — only a pinned run keeps its components in the store.
Variables
Section titled “Variables”--variables loads a JSON, TOML or YAML file into the variables context, which templates read as {{ variables.key }}. --var-json key=<json> sets a single key straight from the command line, overriding the same key from the file. It is repeatable, and keys may not contain ., which is reserved for nested overrides later.
The value is a JSON value rather than bare text, so it arrives with its type intact and is escaped accordingly. The JSON quotes are what choose the type — 42 is a number, "42" is a string:
--var-json tenant_id=42 # number → 42--var-json 'tenant_id="42"' # string → '42'--var-json archived=false # boolean → FALSE--var-json 'archived="false"' # string → 'false'--var-json 'name="acme"' # string → 'acme'--var-json 'tags=["a","b"]' # array → ARRAY['a', 'b']--var-json 'limits={"max":10}' # object → reached as variables.limits.maxAn object sets that one key to the whole object, for a template to reach into: {{ variables.limits.max }} renders 10.
It replaces the key rather than merging into it. If the variables file sets limits = {max = 5, min = 1}, then --var-json 'limits={"max":10}' leaves limits.min unset rather than 1, because the whole table was swapped out. Overriding one leaf of a nested table is not possible from the command line today — that is what the reserved . in keys is held back for.
A string therefore carries two layers of quoting: the JSON quotes that make it a string, and the shell quotes that keep them intact. Single quotes around the whole argument are the easiest way — --var-json 'name="acme"' — or escape the inner pair where the surrounding context forces double quotes, as in a Makefile or CI YAML: --var-json "name=\"acme\"". Omitting the JSON quotes is an error rather than a silent fallback to text, so a typo cannot quietly become a string literal.
Because the type survives, a value lands correctly wherever SQL expects it: VARCHAR({{ variables.name_length }}) renders VARCHAR(255) rather than a quoted literal.
The distinction matters most for booleans, where it changes which branch runs rather than just how the value is escaped. archived=false is a real boolean and {% if variables.archived %} is false; archived="false" is the four-character string false, and since any non-empty string is true, the same test is true. If a template branches on a value, pass it unquoted.
- TOML has no null, so
--var-json archived=nullagainst a TOML file is rejected. YAML and JSON accept it. - TOML integers are 64-bit signed, so a larger number is rejected against a TOML file.
Both limits are the file format’s own. With no --variables file at all, overrides become a JSON object, so neither applies.
Declared variables
Section titled “Declared variables”A script can declare the variables it expects in its own config.toml, so the script carries its inputs rather than leaving them to whoever remembers the right --var-json. Each entry names a source, in the same shape a secret uses, in scripts/make-admin/config.toml:
[variables.tier]source = "value"value = 3
[variables.email]source = "prompt"type = "string"description = "Email of the user to promote"
[variables.grant_all]source = "prompt"type = "boolean"default = truesource = "value" is a value written straight into the file, in TOML’s own types — value = 3 is a number and reaches SQL as 3, not '3'.
source = "prompt" is asked for when the script runs. type is string (the default), number, or boolean, and decides both how the answer is parsed and how it is escaped. There is no separate integer type: a number answered 100 stays an integer and 0.25 becomes a decimal, each reaching SQL as written. default is offered as the pre-filled answer, and description replaces the bare name in the prompt. A default whose type contradicts type is an error as soon as the declarations are resolved, before any prompt appears, and even for a variable that a supplied value would have skipped.
The source shape is borrowed from secrets, but a variable is not one: its value is not masked in build output, and it is not redacted out of a failed run’s diagnostics. Anything that needs those protections belongs in [secrets], not here.
Only config.toml declares a variable. Writing {{ variables.tenant }} in script.sql does not: an undeclared name is never asked for, so it has to arrive from --variables or --var-json. A render that never gets one fails with undefined value, naming the offending expression along with the file and line that wanted it.
Precedence
A declaration is the lowest priority source, so it never fights the command line:
| source | beats |
|---|---|
--var-json key=<json> | everything |
--variables <file> | declarations in config.toml |
| a declaration | nothing |
That ordering is what makes a prompt skippable. Supplying email any other way marks it answered, and it is not asked for:
spawn script run make-admin --var-json 'email="me@example.com"'When it asks, and when it refuses
Prompting needs somewhere to ask and permission to do so. It happens only on a terminal, and only when the value is genuinely missing. Otherwise:
- Not a terminal — piped, redirected, or running in CI — nothing is asked.
--yes, on the two commands that have it -spawn script runandspawn migration apply- means unattended, so it suppresses variable prompts as well as the confirmation.
In both of those cases a missing value is an error naming the variable and the flag that would supply it, rather than a hang:
Error: variable 'email' needs a string value and cannot be asked for here:pass --var-json 'email="..."', or --use-defaults to take the declared default--use-defaults takes each declared default instead of asking, which is the unattended form of the same run. A prompt variable with no default is still an error, since there is nothing to fall back to — --use-defaults never invents a value.
Secrets
Section titled “Secrets”run always reveals real secret() values, since it executes the rendered SQL. See the Secrets guide for how secrets are configured, and spawn script build for inspecting a script with them masked.
The diagnostics are the database’s own, and a failing statement can echo a literal back, and with secrets revealed that literal may be one. Spawn replaces every secret value the run resolved with ***REDACTED*** before printing them, which it can only do once the run has finished and every secret() has actually resolved. The same best-effort caveat applies as for a failed apply.
Results on stdout are intentionally not redacted, so that users can write scripts that output to the terminal.
Audit trail
Section titled “Audit trail”Each run appends a row to script_run in spawn’s schema, recording the script’s name, the status, the pin hash it resolved against, how long it took, and a checksum of the script’s raw (unrendered) source.
If the run itself succeeds but the row cannot be written, spawn warns and still reports success: unlike a migration, nothing about a later run depends on that row existing, so a failure here leaves a gap in the audit trail and nothing else.
Examples
Section titled “Examples”Run a script, with a confirmation prompt showing the target:
spawn script run maintenance/reindexRun unattended, as in CI:
spawn script run maintenance/reindex --yesPass a value straight from the command line:
spawn script run maintenance/backfill --var-json tenant_id=42 --yesInsist the script is pinned, failing if it is not:
spawn script run maintenance/backfill --pinned --yesTry a component edit against a pinned script before re-pinning:
spawn script run maintenance/backfill --no-pin --yesOptions
| Option | Description |
|---|---|
--pinned | Require the pinned component tree. Fails if the script has not been pinned. |
--no-pin | Ignore the script's pin and resolve includes against the current components. |
--variables <path> | Path to variables file (JSON, TOML, or YAML). Values are available in templates under {{ variables }}. |
--var-json <KEY=JSON> | Set a single variable from a JSON value, so it keeps its type. May be repeated, and overrides the same key from --variables. Keys may not contain '.', which is reserved for future nested overrides. |
--use-defaults | Take each declared default instead of asking for it, for unattended use. A prompt variable with no default is still an error. |
--yes | Skip confirmation prompt |
-e, --environment <name> | Override the environment for the target config. |
--target <name> | Select which target from spawn.toml to use. |
--config-file <path> | Path to config file. Defaults to spawn.toml. |
-d, --debug | Turn on debug output. |