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