Module: ActiveRecord::PGEnum::SchemaStatements

Defined in:
lib/active_record/pg_enum/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_enum_value(type, value, before: nil, after: nil) ⇒ Object

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")


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_record/pg_enum/schema_statements.rb', line 28

def add_enum_value(type, value, before: nil, after: nil)
  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
end

#create_enum(name, *values) ⇒ Object

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

Example:

create_enum("foo_type", "foo", "bar", "baz")


9
10
11
# File 'lib/active_record/pg_enum/schema_statements.rb', line 9

def create_enum(name, *values)
  execute "CREATE TYPE #{name} AS ENUM (#{values.map { |v| "'#{v}'" }.join(", ")})"
end

#drop_enum(name, *values_for_revert) ⇒ Object

Drop an ENUM type from the database.



14
15
16
# File 'lib/active_record/pg_enum/schema_statements.rb', line 14

def drop_enum(name, *values_for_revert)
  execute "DROP TYPE #{name}"
end