Module: Sequel::Postgres::EnumDatabaseMethods
- Defined in:
- lib/sequel/extensions/pg_enum.rb
Overview
Methods enabling Database object integration with enum types.
Class Method Summary collapse
-
.extended(db) ⇒ Object
Parse the available enum values when loading this extension into your database.
Instance Method Summary collapse
-
#add_enum_value(enum, value, opts = OPTS) ⇒ Object
Run the SQL to add the given value to the existing enum type.
-
#create_enum(enum, values) ⇒ Object
Run the SQL to create an enum type with the given name and values.
-
#drop_enum(enum, opts = OPTS) ⇒ Object
Run the SQL to drop the enum type with the given name.
-
#rename_enum(enum, new_name) ⇒ Object
Run the SQL to rename the enum type with the given name to the another given name.
Class Method Details
.extended(db) ⇒ Object
Parse the available enum values when loading this extension into your database.
69 70 71 72 73 74 |
# File 'lib/sequel/extensions/pg_enum.rb', line 69 def self.extended(db) db.instance_exec do @enum_labels = {} parse_enum_labels end end |
Instance Method Details
#add_enum_value(enum, value, opts = OPTS) ⇒ Object
Run the SQL to add the given value to the existing enum type. Options: :after :: Add the new value after this existing value. :before :: Add the new value before this existing value. :if_not_exists :: Do not raise an error if the value already exists in the enum.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sequel/extensions/pg_enum.rb', line 81 def add_enum_value(enum, value, opts=OPTS) sql = String.new sql << "ALTER TYPE #{quote_schema_table(enum)} ADD VALUE#{' IF NOT EXISTS' if opts[:if_not_exists]} #{literal(value.to_s)}" if v = opts[:before] sql << " BEFORE #{literal(v.to_s)}" elsif v = opts[:after] sql << " AFTER #{literal(v.to_s)}" end run sql parse_enum_labels nil end |
#create_enum(enum, values) ⇒ Object
Run the SQL to create an enum type with the given name and values.
95 96 97 98 99 100 |
# File 'lib/sequel/extensions/pg_enum.rb', line 95 def create_enum(enum, values) sql = "CREATE TYPE #{quote_schema_table(enum)} AS ENUM (#{values.map{|v| literal(v.to_s)}.join(', ')})" run sql parse_enum_labels nil end |
#drop_enum(enum, opts = OPTS) ⇒ Object
Run the SQL to drop the enum type with the given name. Options: :if_exists :: Do not raise an error if the enum type does not exist :cascade :: Also drop other objects that depend on the enum type
115 116 117 118 119 120 |
# File 'lib/sequel/extensions/pg_enum.rb', line 115 def drop_enum(enum, opts=OPTS) sql = "DROP TYPE#{' IF EXISTS' if opts[:if_exists]} #{quote_schema_table(enum)}#{' CASCADE' if opts[:cascade]}" run sql parse_enum_labels nil end |
#rename_enum(enum, new_name) ⇒ Object
Run the SQL to rename the enum type with the given name to the another given name.
104 105 106 107 108 109 |
# File 'lib/sequel/extensions/pg_enum.rb', line 104 def rename_enum(enum, new_name) sql = "ALTER TYPE #{quote_schema_table(enum)} RENAME TO #{quote_schema_table(new_name)}" run sql parse_enum_labels nil end |