EctoEnumMigration v0.3.4 EctoEnumMigration View Source

Provides a DSL to easily handle Postgres Enum Types in Ecto database migrations.

Link to this section Summary

Functions

Add a value to a existing Postgres type.

Create a Postgres Enum Type.

Drop a Postgres Enum Type.

Link to this section Functions

Link to this function

add_value_to_type(name, value, opts \\ [])

View Source

Specs

add_value_to_type(name :: atom(), value :: atom(), opts :: Keyword.t()) ::
  :ok | no_return()

Add a value to a existing Postgres type.

This operation is not reversible, existing values cannot be removed from an enum type. Checkout Enumerated Types for more information.

Also it cannot be used inside a transaction block, we need to set @disable_ddl_transaction true in the migration.

Examples

defmodule MyApp.Repo.Migrations.AddValueToTypeMigration do
  use Ecto.Migration
  import EctoEnumMigration
  @disable_ddl_transaction true

  def up do
    add_value_to_type(:status, :finished)
  end
  
  def down do
  end
end

By default the type will be created in the public schema. To change the schema of the type pass the schema option.

add_value_to_type(:status, :finished, schema: "custom_schema")

If the new value's place in the enum's ordering is not specified, then the new item is placed at the end of the list of values.

But we specify the the place in the ordering for the new value with the :before and :after options.

add_value_to_type(:status, :finished, before: :started)
add_value_to_type(:status, :finished, after: :started)
Link to this function

create_type(name, values, opts \\ [])

View Source

Specs

create_type(name :: atom(), values :: [atom()], opts :: Keyword.t()) ::
  :ok | no_return()

Create a Postgres Enum Type.

Examples

defmodule MyApp.Repo.Migrations.CreateTypeMigration do
  use Ecto.Migration
  import EctoEnumMigration

  def change do
    create_type(:status, [:registered, :active, :inactive, :archived])
  end
end

By default the type will be created in the public schema. To change the schema of the type pass the schema option.

create_type(:status, [:registered, :active, :inactive, :archived], schema: "custom_schema")
Link to this function

drop_type(name, opts \\ [])

View Source

Specs

drop_type(name :: atom(), opts :: Keyword.t()) :: :ok | no_return()

Drop a Postgres Enum Type.

This command is not reversible, so make sure to include a down/0 step in the migration.

Examples

defmodule MyApp.Repo.Migrations.DropTypeMigration do
  use Ecto.Migration
  import EctoEnumMigration

  def up do
    drop_type(:status)
  end

  def down do
    create_type(:status, [:registered, :active, :inactive, :archived])
  end
end

By default the type will be created in the public schema. To change the schema of the type pass the schema option.

drop_type(:status, schema: "custom_schema")
Link to this function

rename_type(before_name, after_name, opts \\ [])

View Source

Specs

rename_type(before_name :: atom(), after_name :: atom(), opts :: Keyword.t()) ::
  :ok | no_return()

Rename a Postgres Type.

Examples

defmodule MyApp.Repo.Migrations.RenameTypeMigration do
  use Ecto.Migration
  import EctoEnumMigration

  def change do
    rename_type(:status, :status_renamed)
  end
end

By default the type will be created in the public schema. To change the schema of the type pass the schema option.

rename_type(:status, :status_renamed, schema: "custom_schema")
Link to this function

rename_value(type_name, before_value, after_value, opts \\ [])

View Source

Specs

rename_value(
  type_name :: atom(),
  before_value :: atom(),
  after_value :: atom(),
  opts :: Keyword.t()
) :: :ok | no_return()

Rename a value of a Postgres Type.

Only compatible with Postgres version 10+

Examples

defmodule MyApp.Repo.Migrations.RenameTypeMigration do
  use Ecto.Migration
  import EctoEnumMigration

  def change do
    rename_value(:status, :finished, :done)
  end
end

By default the type will be created in the public schema. To change the schema of the type pass the schema option.

rename_value(:status, :finished, :done, schema: "custom_schema")