Helpers for Turso schema changes that require rebuilding a table.
rebuild_table!/3 performs the safe create-copy-drop-rename sequence on
one checked-out connection. The caller must provide the complete target
CREATE TABLE statement and an explicit identifier-only copy mapping.
Call it from execute(fn -> ... end) in explicit up/0 and down/0
migrations with @disable_ddl_transaction true.
Summary
Functions
Atomically replaces a table using a complete target definition.
Types
@type copy_entry() :: sql_identifier() | {sql_identifier(), sql_identifier()}
@type object_kind() :: :indexes | :triggers
@type rebuild_options() :: [ create: (String.t() -> iodata()), copy: [copy_entry()], recreate: [object_kind()], validate: (term() -> :ok | {:error, term()}) ]
Functions
@spec rebuild_table!( Ecto.Repo.t(), sql_identifier(), rebuild_options() ) :: :ok
Atomically replaces a table using a complete target definition.
The required :create function receives an already quoted temporary table
identifier and must return one complete CREATE TABLE statement. The
required :copy list accepts same-name columns or {target, source} pairs;
raw expressions and backfills are intentionally not supported.
Explicit indexes and attached triggers are recreated by default; pass a
subset through :recreate to opt out deliberately. An optional :validate
callback receives the checked-out transaction connection and must return
:ok or {:error, reason}. Built-in foreign-key and integrity checks
always run.
The helper owns its checkout and transaction. It raises when called inside
an existing transaction or checkout, so migrations must use explicit
up/0 and down/0, set @disable_ddl_transaction true, and invoke it from
execute(fn -> ... end).