schema-migration-generator
Converts a schema-diff::ChangeSet into database-specific migration SQL statements (Flyway-style), ready to be applied via schema-installer.
Usage
use SchemaDiffEngine;
use create_generator;
use DatabaseType;
// Diff two schemas
let change_set = diff;
// Generate database-specific migration SQL
let generator = create_generator;
let mut output = Vecnew;
generator.generate?;
let migration_sql = Stringfrom_utf8?;
println!;
CLI Usage
# Diff two schema sources (file paths, or git '<rev>:<path>' references) and print SQL to stdout
# Shortcut: diff a file on disk against its own HEAD version
# Write to a specific path instead of stdout
# Auto-name the output as V{timestamp}.sql alongside the schema file (requires --file)
# Auto-name with a description: V{timestamp}__add_users_table.sql
--auto-generate-name and --description require --file (not --old/--new, since there's no single unambiguous schema-file directory to write into otherwise) and are mutually exclusive with --output-file. The generated filename is Flyway-style and compatible with schema-installer's migration parser, whether or not a description is given.
Typical Pipeline
- Parse two schema versions:
schema-parser+schema-model - Diff them:
schema-diff::SchemaDiffEngine::diff() - Generate migration SQL:
schema_migration_generator::create_generator()+MigrationGenerator::generate() - Apply the SQL:
schema-installer::Migrator::migrate()
Database Support
Supported databases with implementations:
| Database | Status |
|---|---|
| PostgreSQL | ✅ Full |
| SQLite | ✅ Full |
| SQL Server | ✅ Full |
Generated SQL Examples
PostgreSQL
-- Add table
(...);
-- Add column
users ADD COLUMN created_at TIMESTAMP DEFAULT NOW;
-- Modify column
users ALTER COLUMN email SET NOT NULL;
-- Add constraint
orders ADD CONSTRAINT fk_orders_user_id
FOREIGN KEY (user_id) REFERENCES users(id);
-- Drop column (with rename suggestion for ambiguous case)
-- TODO: possible rename? Column status_id disappeared; created_status present.
-- ALTER TABLE orders RENAME COLUMN status_id TO created_status;
orders DROP COLUMN status_id;
Library API
Ambiguous Rename Handling
When a column is dropped and another of the same type is added, the generator emits a commented-out suggestion before the actual DROP COLUMN, helping developers recognize and fix the generated SQL:
-- TODO: possible rename? Column old_name disappeared; new_name present.
-- ALTER TABLE users RENAME COLUMN old_name TO new_name;
users DROP COLUMN old_name;
Part of schema-rs
See the workspace README for an overview of the full schema-rs toolchain.