glimr/db/gen/migrate/sql

Migration SQL Generation

Handles schema diffing and SQL generation for migrations. Compares old and new snapshots to detect changes, then generates driver-specific SQL statements.

Types

A single schema change that needs to be migrated. Variants cover table creation/deletion and column add/drop/alter/rename.

pub type Change {
  CreateTable(table: schema_parser.Table)
  DropTable(name: String)
  AddColumn(table: String, column: schema_parser.Column)
  DropColumn(table: String, column: String)
  AlterColumn(
    table: String,
    column: schema_parser.Column,
    old: snapshot.ColumnSnapshot,
  )
  RenameColumn(table: String, old_name: String, new_name: String)
}

Constructors

Database driver for SQL generation. Determines syntax differences between PostgreSQL and SQLite.

pub type Driver {
  Postgres
  Sqlite
}

Constructors

  • Postgres
  • Sqlite

The computed difference between old and new schema snapshots. Contains a list of changes that need to be migrated.

pub type SchemaDiff {
  SchemaDiff(changes: List(Change))
}

Constructors

  • SchemaDiff(changes: List(Change))

Values

pub fn compute_diff(
  old: snapshot.Snapshot,
  new: snapshot.Snapshot,
  tables: List(schema_parser.Table),
  is_filtered: Bool,
) -> SchemaDiff

Compute the diff between old and new snapshots. Detects new tables, dropped tables, and column changes. When is_filtered is true, skips drop detection to avoid false positives.

pub fn describe_change(change: Change) -> String

Human-readable description of a Change for CLI output.

pub fn generate_sql(diff: SchemaDiff, driver: Driver) -> String

Generate SQL for all changes in a diff. CreateTable changes are sorted by dependency order so tables referencing other tables are created after their dependencies.

Search Document