Module: ActiveRecord::PGEnum::SchemaStatements
- Defined in:
- lib/active_record/pg_enum/schema_statements.rb
Instance Method Summary collapse
-
#add_enum_value(type, value, options = {}) ⇒ Object
Add a new value to an existing ENUM type.
-
#create_enum(name, values) ⇒ Object
Create a new ENUM type, with an arbitrary number of values.
-
#drop_enum(name, values_for_revert = nil) ⇒ Object
Drop an ENUM type from the database.
-
#rename_enum(name, options = {}) ⇒ Object
Rename an existing ENUM type.
-
#rename_enum_value(type, options = {}) ⇒ Object
Change the label of an existing ENUM value.
Instance Method Details
#add_enum_value(type, value, options = {}) ⇒ 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")
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/pg_enum/schema_statements.rb', line 45 def add_enum_value(type, value, = {}) before, after = .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) ⇒ Object
Create a new ENUM type, with an arbitrary number of values.
Example:
create_enum("foo_type", "foo", "bar", "baz", "foo bar")
14 15 16 17 18 |
# 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) ⇒ Object
Drop an ENUM type from the database.
21 22 23 24 25 |
# 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 = {}) ⇒ Object
Rename an existing ENUM type
28 29 30 31 32 33 |
# File 'lib/active_record/pg_enum/schema_statements.rb', line 28 def rename_enum(name, = {}) to = .fetch(:to) { raise ArgumentError, ":to is required" } execute("ALTER TYPE #{name} RENAME TO #{to}").tap { reload_type_map } end |
#rename_enum_value(type, options = {}) ⇒ Object
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
71 72 73 74 75 76 77 78 |
# File 'lib/active_record/pg_enum/schema_statements.rb', line 71 def rename_enum_value(type, = {}) from = .fetch(:from) { raise ArgumentError, ":from is required" } to = .fetch(:to) { raise ArgumentError, ":to is required" } execute("ALTER TYPE #{type} RENAME VALUE '#{from}' TO '#{to}'").tap { reload_type_map } end |