PhoenixMountable.Migrations.SQLite (PhoenixMountable v0.2.1)

Copy Markdown View Source

Table rebuilds for SQLite, whose ALTER TABLE cannot change a column.

SQLite can add, rename and drop columns, but changing a column's type, nullability or default means creating a new table, copying the rows and swapping the tables. Done naively, that drops whatever the host added to the table. rebuild_table/3 keeps it:

  • every column of the current table is copied, including columns the library does not know about;
  • every index (including unique constraints) and trigger is recreated;
  • foreign keys are kept and checked before committing;
  • a table with CHECK constraints or COLLATE clauses is refused, since they cannot be reproduced reliably, rather than silently lost.

It runs in its own transaction with foreign keys off (dropping a table with them on would cascade deletes), so the version runs outside the migration's transaction; PhoenixMountable.Migration detects this.

Changes

rebuild_table(ctx, :my_lib_items,
  modify: [title: [type: "TEXT", null: false, default: "''"]],
  remove: [:legacy_flag],
  rename: [body: :content]
)
  • :modify — columns to change: :type (SQL type), :null and :default (an SQL expression, as a string). Unmentioned attributes are kept.
  • :remove — columns to drop.
  • :rename — columns to rename, old: :new.

Summary

Functions

Rebuilds a SQLite table with the given changes. See the module documentation.

Functions

rebuild_table(ctx, table, changes)

@spec rebuild_table(
  PhoenixMountable.Migrations.Context.t(),
  atom() | String.t(),
  keyword()
) :: :ok

Rebuilds a SQLite table with the given changes. See the module documentation.