Localize.Ecto.Migration.Generator (Localize SQL v1.0.0)

Copy Markdown View Source

Writes a migration file into a repository, for the migration-generating mix tasks of localize_sql.

A tagged decimal type is installed by running SQL — CREATE TYPE, the aggregate functions — inside an Ecto migration. Rather than ship the SQL as a file the consumer copies, these tasks generate a migration whose up and down embed the SQL produced by a DDL module, so the statements are baked into the consumer's own timestamped migration and are not affected by a later upgrade of localize_sql.

This module renders and writes that migration. render/3 builds the source and is independent of any repository; create_migration/5 writes it into a repository's migrations directory.

Summary

Functions

Writes a migration into a repository's migrations directory.

Renders a migration module as formatted Elixir source.

Returns a 14-digit UTC timestamp for a migration filename.

Functions

create_migration(repo, name, up_sql, down_sql, timestamp)

@spec create_migration(module(), String.t(), String.t(), String.t(), String.t()) ::
  String.t()

Writes a migration into a repository's migrations directory.

Arguments

  • repo is an Ecto.Repo module.

  • name is the migration name in snake case, such as "add_cldr_unit". It is timestamped and camelized into the module name.

  • up_sql is the body of the up/0 function.

  • down_sql is the body of the down/0 function.

  • timestamp is the 14-digit migration timestamp string, from timestamp/0.

Returns

  • The path of the written file.

render(module, up_sql, down_sql)

@spec render(module(), String.t(), String.t()) :: String.t()

Renders a migration module as formatted Elixir source.

Arguments

  • module is the migration module name.

  • up_sql is the body of the up/0 function — typically the output of a DDL.execute_each/1 call.

  • down_sql is the body of the down/0 function.

Returns

  • The migration source as a string.

Examples

iex> source = Localize.Ecto.Migration.Generator.render(
...>   MyApp.Repo.Migrations.AddCldrUnit,
...>   ~s|execute "CREATE TYPE public.cldr_unit AS (unit varchar, value numeric);"|,
...>   ~s|execute "DROP TYPE public.cldr_unit;"|
...> )
iex> source =~ "def up do"
true

timestamp()

@spec timestamp() :: String.t()

Returns a 14-digit UTC timestamp for a migration filename.

Returns

  • The timestamp as a string, such as "20260731090501".