Enumex.Static.Migration (Enumex v1.0.0)
View SourceA simple wrapper for the adapter that allows to write a reversible migrations using a single call.
With wrapper
defmodule ExampleMigration do
use Ecto.Migration
alias Enumex.Static.Adapters.Postgres
alias Enumex.Static.Migration
alias Enumex.Value
@first_value Value.new(ExampleEnum, :example, :firstt, 1)
@second_value Value.new(ExampleEnum, :example, :second, 2)
@third_value Value.new(ExampleEnum, :example, :third, 3)
def change do
Migration.create_enum(Postgres, [@first_value, @third_value])
Migration.rename_value(Postgres, @first_value, %{@first_value | id: :first})
Migration.add_value(Postgres, @second_value, @first_value)
Migration.drop_value(Postgres, @third_value, @second_value)
end
end
Without wrapper
defmodule ExampleMigration do
use Ecto.Migration
alias Enumex.Static.Adapters.Postgres
alias Enumex.Value
@first_value Value.new(ExampleEnum, :example, :firstt, 1)
@second_value Value.new(ExampleEnum, :example, :second, 2)
@third_value Value.new(ExampleEnum, :example, :third, 3)
def down do
Postgres.add_value(repo(), @third_value)
Postgres.drop_value(repo(), @second_value, @third_value)
Postgres.rename_value(repo(), %{@first_value | id: :first}, @first_value)
Postgres.drop_enum(repo(), @first_value.enum_name)
end
def up do
Postgres.create_enum(repo(), [@first_value, @third_value])
Postgres.rename_value(repo(), @first_value, %{@first_value | id: :first})
Postgres.add_value(repo(), @second_value)
Postgres.drop_value(repo(), @third_value, @second_value)
end
end
Summary
Functions
Adds the enum value to a database enumerated type.
Adds the enum value to a database enumerated type.
Creates the database enumerated type with a specific values.
Drops the database enumerated type.
Drops the enum value from a database enumerated type.
Renames the database enumerated type.
Renames the enum value in a database enumerated type.
Functions
@spec add_value( Enumex.macro_input(Enumex.Static.Adapter.t()), Enumex.macro_input(Enumex.Value.t()), Enumex.macro_input(Enumex.Static.Adapter.opts() | Enumex.Value.t()) ) :: Macro.output()
Adds the enum value to a database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> new_value = Enumex.Value.new(ExampleEnum, :example, :new, 3)
iex> Enumex.Static.Migration.add_value(adapter, new_value)
iex> Enumex.Static.Migration.add_value(adapter, new_value, log: true)
:ok
Note: use enum value
instead of opts if you want to have a reversible migration.
iex> default_value = Enumex.Value.new(ExampleEnum, :example, :existing, 1)
iex> Enumex.Static.Migration.add_value(adapter, new_value, default_value)
:ok
@spec add_value( Enumex.macro_input(Enumex.Static.Adapter.t()), Enumex.macro_input(Enumex.Value.t()), Enumex.macro_input(Enumex.Value.t()), Enumex.macro_input(Enumex.Static.Adapter.opts()) ) :: Macro.output()
Adds the enum value to a database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> new_value = Enumex.Value.new(ExampleEnum, :example, :new, 3)
iex> default_value = Enumex.Value.new(ExampleEnum, :example, :existing, 1)
iex> Enumex.Static.Migration.add_value(adapter, new_value, default_value, log: true)
:ok
@spec create_enum( Enumex.Static.Adapter.t(), [Enumex.Value.t()], Enumex.Static.Adapter.opts() ) :: :ok
Creates the database enumerated type with a specific values.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> first_value = Enumex.Value.new(ExampleEnum, :example, :first, 1)
iex> second_value = Enumex.Value.new(ExampleEnum, :example, :second, 2)
iex> Enumex.Static.Migration.create_enum(adapter, [first_value, second_value])
:ok
iex> Enumex.Static.Migration.create_enum(adapter, [first_value, second_value], log: true)
:ok
@spec drop_enum( Enumex.Static.Adapter.t(), [Enumex.Value.t()], Enumex.Static.Adapter.opts() ) :: :ok
@spec drop_enum( Enumex.Static.Adapter.t(), Enumex.Value.enum_name(), Enumex.Static.Adapter.opts() ) :: :ok | no_return()
Drops the database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> first_value = Enumex.Value.new(ExampleEnum, :example, :first, 1)
iex> second_value = Enumex.Value.new(ExampleEnum, :example, :second, 2)
iex> Enumex.Static.Migration.create_enum(adapter, first_value.enum_name)
:ok
iex> Enumex.Static.Migration.create_enum(adapter, first_value.enum_name, log: true)
:ok
Note: use enum values
instead of enum name if you want to have a reversible migration.
iex> Enumex.Static.Migration.create_enum(adapter, [first_value, second_value])
:ok
iex> Enumex.Static.Migration.create_enum(adapter, [first_value, second_value], log: true)
:ok
@spec drop_value( Enumex.macro_input(Enumex.Static.Adapter.t()), Enumex.macro_input(Enumex.Value.t()), Enumex.macro_input(Enumex.Value.t()), Enumex.macro_input(Enumex.Static.Adapter.opts()) ) :: Macro.output()
Drops the enum value from a database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> existing_value = Enumex.Value.new(ExampleEnum, :example, :existing, 3)
iex> default_value = Enumex.Value.new(ExampleEnum, :example, :another_existing, 1)
iex> Enumex.Static.Migration.drop_value(adapter, existing_value, default_value)
:ok
iex> Enumex.Static.Migration.drop_value(adapter, existing_value, default_value, log: true)
:ok
@spec rename_enum( Enumex.Static.Adapter.t(), Enumex.Value.enum_name(), Enumex.Value.enum_name(), Enumex.Static.Adapter.opts() ) :: :ok
Renames the database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> Enumex.Static.Migration.rename_enum(adapter, :old_enum_name, :new_enum_name)
:ok
iex> Enumex.Static.Migration.rename_enum(adapter, :old_enum_name, :new_enum_name, log: true)
:ok
@spec rename_value( Enumex.Static.Adapter.t(), Enumex.Value.t(), Enumex.Value.t(), Enumex.Static.Adapter.opts() ) :: :ok
Renames the enum value in a database enumerated type.
iex> adapter = Enumex.Static.Adapters.Postgres
iex> old_value = Enumex.Value.new(ExampleEnum, :example, :old, 1)
iex> new_value = %{old_value | id: :new}
iex> Enumex.Static.Migration.rename_value(adapter, old_value, new_value)
:ok
iex> Enumex.Static.Migration.rename_value(adapter, old_value, new_value, log: true)
:ok