module ActiveRecord::PGEnum::SchemaStatements

Public Instance Methods

add_enum_value(type, value, options = {}) click to toggle source

Add a new value to an existing ENUM type. Only one value at a time is supported by PostgreSQL.

Options:

before: add value BEFORE the given value
after:  add value AFTER the given value

Example:

add_enum_value("foo_type", "quux", before: "bar")
# File lib/active_record/pg_enum/schema_statements.rb, line 45
def add_enum_value(type, value, options = {})
  before, after = options.values_at(:before, :after)
  cmd = "ALTER TYPE #{type} ADD VALUE '#{value}'"

  if before && after
    raise ArgumentError, "Cannot have both :before and :after at the same time"
  elsif before
    cmd << " BEFORE '#{before}'"
  elsif after
    cmd << " AFTER '#{after}'"
  end

  execute(cmd).tap { reload_type_map }
end
create_enum(name, values) click to toggle source

Create a new ENUM type, with an arbitrary number of values.

Example:

create_enum("foo_type", "foo", "bar", "baz", "foo bar")
# File lib/active_record/pg_enum/schema_statements.rb, line 14
def create_enum(name, values)
  execute("CREATE TYPE #{name} AS ENUM (#{Array(values).map { |v| "'#{v}'" }.join(", ")})").tap {
    reload_type_map
  }
end
drop_enum(name, values_for_revert = nil) click to toggle source

Drop an ENUM type from the database.

# File lib/active_record/pg_enum/schema_statements.rb, line 21
def drop_enum(name, values_for_revert = nil)
  execute("DROP TYPE #{name}").tap {
    reload_type_map
  }
end
rename_enum(name, options = {}) click to toggle source

Rename an existing ENUM type

# File lib/active_record/pg_enum/schema_statements.rb, line 28
def rename_enum(name, options = {})
  to = options.fetch(:to) { raise ArgumentError, ":to is required" }
  execute("ALTER TYPE #{name} RENAME TO #{to}").tap {
    reload_type_map
  }
end
rename_enum_value(type, options = {}) click to toggle source

Change the label of an existing ENUM value

Options:

from: The original label string
to:   The desired label string

Example:

rename_enum_value "foo_type", from: "quux", to: "Quux"

Note: This feature requires PostgreSQL 10 or later

# File lib/active_record/pg_enum/schema_statements.rb, line 71
def rename_enum_value(type, options = {})
  from = options.fetch(:from) { raise ArgumentError, ":from is required" }
  to   = options.fetch(:to)   { raise ArgumentError, ":to is required" }

  execute("ALTER TYPE #{type} RENAME VALUE '#{from}' TO '#{to}'").tap {
    reload_type_map
  }
end