CLI reference¶
sql2sqlx INPUT [-o DIR] [options]
The CLI is a thin veneer over convert_file and
convert_directory. It can also be run as a module:
python -m sql2sqlx ....
Input and output modes¶
INPUT is either a single .sql file or a directory of .sql files.
What happens next depends on the input kind and whether --output is given:
Input |
|
Behavior |
|---|---|---|
file |
omitted |
The generated SQLX is printed to stdout; the run summary goes to
stderr, so |
file |
given |
The generated file(s) are written beneath the output directory. |
directory |
given |
The converted tree is written beneath the output directory. |
directory |
omitted |
Error, unless |
When a single file produces multiple actions and is printed to stdout, the files are separated by a header line so the stream stays parseable:
-- ===== active.sqlx =====
config { ... }
...
-- ===== customers.sqlx =====
config { ... }
...
Options¶
Flag |
Default |
Meaning |
|---|---|---|
|
Where to write |
|
|
Write the full JSON conversion report to |
|
|
Project assumed for unqualified table paths (reference resolution only). |
|
|
Dataset assumed for unqualified paths; also resolves single-part references to qualified producers. |
|
|
|
Location written into a scaffolded |
|
|
Mirror the input directory tree, or flatten all output into the root. |
|
|
How |
|
|
How |
|
|
How |
|
|
How guarded |
|
off |
Emit |
|
off |
Do not mark converted incrementals |
|
off |
Omit the |
|
Comma-separated Dataform tags added to every generated action. |
|
|
|
Filename glob for directory scans (must stay within the input directory). |
|
|
Encoding used to read input files (must be a text codec). |
|
|
Parser worker processes; |
|
off |
Convert and report, but write no |
|
off |
Allow writing into an output directory that already contains |
|
off |
Also scaffold a |
|
Suppress the summary. |
|
|
Also list every warning and generated file. |
|
|
Print the version and exit. |
Strategy flags in depth¶
The four strategy flags each default to the semantics-preserving option and let you opt into more idiomatic Dataform structure where you accept the trade-off. Their full rules, proof obligations and the warnings they emit are documented in conversion rules:
--insert-strategy incrementalproducestype: "incremental"fromINSERT ... SELECT, marks itprotected: true(unless--no-protected), and reportsINSERT_INCREMENTAL.--merge-strategy incremental-when-safeconverts only MERGEs whose shape is provably equivalent to an incremental upsert; everything else staysoperationswith aMERGE_FALLBACKreason.--plain-create declarationturns bodilessCREATE TABLE (schema)DDL into adeclaration(drops the DDL; use for externally managed tables).--if-not-exists tableconverts guarded... IF NOT EXISTS ... ASstatements to typed actions and reports the lost create-if-absent guard.
The --init-project scaffold¶
--init-project writes a Dataform-core 3.x workflow_settings.yaml. If the
output directory is named definitions, the file is placed in its parent
(the project root); otherwise it is placed inside the output directory. It is
never silently overwritten - pass --overwrite to replace an existing one.
defaultProject: "my-gcp-project" # from --default-project, else "your-gcp-project"
defaultLocation: "US" # from --default-location
defaultDataset: "analytics" # from --default-dataset, else "dataform"
defaultAssertionDataset: "dataform_assertions"
dataformCoreVersion: "3.0.61"
The overwrite guard¶
When writing to a directory, if the output path already contains any .sqlx
files and --overwrite is not set, sql2sqlx stops with exit code 2 rather
than risk clobbering hand-written actions. Because conversion is deterministic,
re-running with --overwrite in CI produces the same tree and a clean diff.
Exit codes¶
Code |
Meaning |
|---|---|
|
Converted successfully (there may still be warnings). |
|
One or more input files failed to parse (see |
|
Usage error: bad arguments, a directory input without |
Examples¶
# Inspect a single file without writing anything
sql2sqlx model.sql | less
# Full migration with shape-checked MERGE conversion and source declarations
sql2sqlx ./sql -o ./definitions \
--merge-strategy incremental-when-safe \
--declare-external --tags migrated --report report.json
# CI-style dry run that only produces the report
sql2sqlx ./sql --dry-run --report report.json -q
# Regenerate an existing project deterministically, verbosely
sql2sqlx ./sql -o ./definitions --overwrite -v
# Convert only staging models, single-process
sql2sqlx ./sql -o ./definitions --include "stg_*.sql" -j 1