Ecto.Adapters.Turso.Migration (ex_turso v3.0.4)

Copy Markdown View Source

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

copy_entry()

@type copy_entry() :: sql_identifier() | {sql_identifier(), sql_identifier()}

object_kind()

@type object_kind() :: :indexes | :triggers

rebuild_options()

@type rebuild_options() :: [
  create: (String.t() -> iodata()),
  copy: [copy_entry()],
  recreate: [object_kind()],
  validate: (term() -> :ok | {:error, term()})
]

sql_identifier()

@type sql_identifier() :: atom() | String.t()

Functions

rebuild_table!(repo, table, opts)

@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).