Skip to content

spawn script build

Usage

spawn script build <script> [options]

Renders a script to SQL and prints it, without touching the database. The counterpart to spawn script run, for checking what a run would actually execute.

  • <script> — Required. Path to the script, relative to the scripts folder.

--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:

Terminal window
--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.max

An 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=null against 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.

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 = true

source = "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:

sourcebeats
--var-json key=<json>everything
--variables <file>declarations in config.toml
a declarationnothing

That ordering is what makes a prompt skippable. Supplying email any other way marks it answered, and it is not asked for:

Terminal window
spawn script build 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 run and spawn 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.

build masks secret() values by default, since its output is meant for reading rather than executing. The secrets are still resolved, so a misconfigured or unreachable one still fails the build — only the value printed is a placeholder. Pass --reveal-secrets to print real values when debugging locally.

build picks its component tree exactly as spawn script run does, so what it prints is what a run would execute. A pinned script renders against its recorded tree; an unpinned one renders against the current components. Either way the choice is reported on stderr, so it stays out of the SQL on stdout:

Using the pinned component tree for 'maintenance/reindex' (scripts/maintenance/reindex/config.toml).

--pinned requires the script to be pinned, failing if it is not, and --no-pin renders against the current components even when it is — useful for seeing what re-pinning would change.

Inspect what a run would execute:

Terminal window
spawn script build maintenance/reindex

Check how a variable will be escaped before running anything — the JSON quotes decide whether it reaches SQL as 42 or '42':

Terminal window
spawn script build maintenance/backfill --var-json tenant_id=42
spawn script build maintenance/backfill --var-json 'tenant_id="42"'

See what re-pinning would change, by rendering a pinned script against the current components:

Terminal window
spawn script build maintenance/backfill --no-pin

Options

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.
--reveal-secrets Render real secret() values instead of masked placeholders. For local debugging only — build output is not executed.
-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.