order
Reorder SQL dump statements in topological order based on foreign key dependencies for safe imports.
Alias: ord (e.g., sql-splitter ord dump.sql -o ordered.sql)
When to Use This
Section titled “When to Use This”- Safe database restore - Ensure parent tables are created before child tables to avoid FK constraint errors
- Stable diffs - Normalize statement order before comparing dumps with
diffto eliminate ordering noise - CI/CD pipelines - Produce deterministic output from dumps that may have non-deterministic ordering
- DROP script generation - Use
--reverseto generate safe table drop order (children before parents) - Cycle detection - Use
--checkto find circular FK dependencies before migration
Use graph if you want to visualize the dependencies rather than reorder the dump.
How It Works
Section titled “How It Works”The command performs a two-pass operation:
-
First pass - Parse the dump to extract:
- CREATE TABLE statements (with FK relationships)
- ALTER TABLE statements (for deferred FK definitions)
- CREATE INDEX statements
- INSERT/COPY statements (data)
-
Second pass - Rebuild the dump with statements grouped by table in topological order:
- Header statements (SET, comments) come first
- Tables ordered so parents come before children
- For each table: CREATE TABLE → ALTER TABLE/CREATE INDEX → INSERT statements
What gets reordered:
- CREATE TABLE statements
- INSERT/COPY statements (grouped with their table)
- ALTER TABLE statements (grouped with their table)
- CREATE INDEX statements (grouped with their table)
What stays at the top (unchanged order):
- SET statements, session configuration
- Comments, headers
- Statements that don’t reference a specific table
sql-splitter order <INPUT> [OPTIONS]Examples
Section titled “Examples”Basic Reordering
Section titled “Basic Reordering”# Reorder for safe importsql-splitter order dump.sql -o ordered.sql
# Preview the order without writingsql-splitter order dump.sql --dry-runValidating Order (CI/CD)
Section titled “Validating Order (CI/CD)”Use --check to verify ordering is possible and see the result without modifying anything:
sql-splitter order dump.sql --checkExit codes:
0- All tables can be ordered (no cycles)1- Circular dependencies detected (cannot fully order)
Generating DROP Scripts
Section titled “Generating DROP Scripts”Reverse the order so child tables come first (safe for DROP operations):
sql-splitter order dump.sql --reverse -o drop-order.sqlThis ensures you can run DROP TABLE statements without FK constraint violations.
Normalizing for Diff
Section titled “Normalizing for Diff”When comparing two dumps, ordering differences cause noise. Normalize both first:
sql-splitter order old.sql -o old-ordered.sqlsql-splitter order new.sql -o new-ordered.sqlsql-splitter diff old-ordered.sql new-ordered.sqlOptions
Section titled “Options”| Flag | Short | Description | Default |
|---|---|---|---|
--output | -o | Output SQL file | stdout |
--dialect | -d | SQL dialect | auto-detect |
--check | Verify ordering is possible, report order, exit (no output written) | false | |
--dry-run | Show topological order to stderr (no output written) | false | |
--reverse | Reverse order (children before parents, for DROP) | false |
Circular Dependencies
Section titled “Circular Dependencies”When tables have circular FK relationships (e.g., orders → payments → invoices → orders), a perfect topological order is impossible.
What happens with cycles
Section titled “What happens with cycles”- Detection - Cycles are detected using Kahn’s algorithm during topological sort
- Partial ordering - Non-cyclic tables are ordered correctly
- Cyclic tables appended - Tables in cycles are appended at the end of the output
- Warning printed - You’ll see which tables are involved
Example output:
Warning: Circular dependencies detected!The following tables are part of cycles: - orders - payments - invoices
Processed 15 tables in topological order.Handling cycles during import
Section titled “Handling cycles during import”Cyclic tables at the end of the output may cause FK errors during import. Options:
Option 1: Disable FK checks during import
# MySQLmysql -e "SET FOREIGN_KEY_CHECKS=0; SOURCE ordered.sql; SET FOREIGN_KEY_CHECKS=1;"
# PostgreSQL (must be in same transaction)psql -c "BEGIN; SET CONSTRAINTS ALL DEFERRED;" -f ordered.sql -c "COMMIT;"Option 2: Analyze and refactor
# See exactly which tables form cyclessql-splitter graph dump.sql --cycles-only
# Visualize the cyclesql-splitter graph dump.sql --cycles-only -o cycles.htmlOption 3: Split the dump
Use split to extract cyclic tables, import them separately with FK checks disabled.
Exit code behavior with —check
Section titled “Exit code behavior with —check”When using --check:
- No cycles → exit 0, prints suggested order
- Cycles found → exit 1, prints cyclic tables and suggests using
graph --cycles-only
Output Structure
Section titled “Output Structure”The ordered dump follows this structure:
-- 1. Header statements (SET, comments, session config)SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS=0;
-- 2. Tables in topological order (parents first)-- For each table: CREATE → ALTER/INDEX → INSERT
CREATE TABLE users (...);INSERT INTO users VALUES (...);
CREATE TABLE orders (...); -- References usersALTER TABLE orders ADD INDEX idx_user (user_id);INSERT INTO orders VALUES (...);
-- 3. Cyclic tables (if any) appended at endCREATE TABLE payments (...); -- Part of cycleINSERT INTO payments VALUES (...);Troubleshooting
Section titled “Troubleshooting””No tables found in the file”
Section titled “”No tables found in the file””The dump may not contain CREATE TABLE statements, or dialect detection failed:
# Specify dialect explicitlysql-splitter order dump.sql --dialect postgres -o ordered.sqlDifferent order on each run
Section titled “Different order on each run”The order should be deterministic for the same input. If you see different orders:
- Check if the input file itself is changing
- Tables with no dependencies between them may appear in different relative order (this is correct - they’re interchangeable)
Some INSERT statements missing
Section titled “Some INSERT statements missing”INSERT statements are grouped with their table. If a table has INSERTs but no CREATE TABLE in the dump:
# Check if the table exists in the dumpsql-splitter analyze dump.sql | grep table_nameData-only dumps (no DDL) can’t be reordered since there’s no schema to analyze.
Cycle detection differs from graph command
Section titled “Cycle detection differs from graph command”Both use the same algorithm, but order --check lists tables in cycles, while graph --cycles-only shows the actual cycle paths (A → B → C → A). Use graph for detailed analysis.