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.
Rename a Postgres Type.
Rename a value of a Postgres Type.
Link to this section Functions
Specs
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)
Specs
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")
Specs
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")
Specs
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")
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")