module Sequel::Postgres::EnumDatabaseMethods
Methods enabling Database
object integration with enum types.
Public Class Methods
Parse the available enum values when loading this extension into your database.
# File lib/sequel/extensions/pg_enum.rb 75 def self.extended(db) 76 db.instance_exec do 77 @enum_labels = {} 78 parse_enum_labels 79 end 80 end
Public Instance Methods
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 87 def add_enum_value(enum, value, opts=OPTS) 88 sql = String.new 89 sql << "ALTER TYPE #{quote_schema_table(enum)} ADD VALUE#{' IF NOT EXISTS' if opts[:if_not_exists]} #{literal(value.to_s)}" 90 if v = opts[:before] 91 sql << " BEFORE #{literal(v.to_s)}" 92 elsif v = opts[:after] 93 sql << " AFTER #{literal(v.to_s)}" 94 end 95 _process_enum_change_sql(sql) 96 end
Run the SQL
to create an enum type with the given name and values.
# File lib/sequel/extensions/pg_enum.rb 99 def create_enum(enum, values) 100 _process_enum_change_sql("CREATE TYPE #{quote_schema_table(enum)} AS ENUM (#{values.map{|v| literal(v.to_s)}.join(', ')})") 101 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 119 def drop_enum(enum, opts=OPTS) 120 _process_enum_change_sql("DROP TYPE#{' IF EXISTS' if opts[:if_exists]} #{quote_schema_table(enum)}#{' CASCADE' if opts[:cascade]}") 121 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 105 def rename_enum(enum, new_name) 106 _process_enum_change_sql("ALTER TYPE #{quote_schema_table(enum)} RENAME TO #{quote_schema_table(new_name)}") 107 end
Run the SQL
to rename the enum value with the given name to the another given name.
# File lib/sequel/extensions/pg_enum.rb 111 def rename_enum_value(enum, old_name, new_name) 112 _process_enum_change_sql("ALTER TYPE #{quote_schema_table(enum)} RENAME VALUE #{literal(old_name.to_s)} TO #{literal(new_name.to_s)}") 113 end
Private Instance Methods
Run the SQL
on the database, reparsing the enum labels after it is run.
# File lib/sequel/extensions/pg_enum.rb 126 def _process_enum_change_sql(sql) 127 run(sql) 128 parse_enum_labels 129 nil 130 end
Parse the pg_enum table to get enum values, and the pg_type table to get names and array oids for enums.
# File lib/sequel/extensions/pg_enum.rb 135 def parse_enum_labels 136 enum_labels = metadata_dataset.from(:pg_enum). 137 order(:enumtypid, :enumsortorder). 138 select_hash_groups(Sequel.cast(:enumtypid, Integer).as(:v), :enumlabel).freeze 139 enum_labels.each_value(&:freeze) 140 141 if respond_to?(:register_array_type) 142 array_types = metadata_dataset. 143 from(:pg_type). 144 where(:oid=>enum_labels.keys). 145 exclude(:typarray=>0). 146 select_map([:typname, Sequel.cast(:typarray, Integer).as(:v)]) 147 148 existing_oids = conversion_procs.keys 149 array_types.each do |name, oid| 150 next if existing_oids.include?(oid) 151 register_array_type(name, :oid=>oid) 152 end 153 end 154 155 Sequel.synchronize{@enum_labels.replace(enum_labels)} 156 end
For schema entries that are enums, set the type to :enum and add a :enum_values entry with the enum values.
# File lib/sequel/extensions/pg_enum.rb 160 def schema_post_process(_) 161 super.each do |_, s| 162 oid = s[:oid] 163 if values = Sequel.synchronize{@enum_labels[oid]} 164 s[:type] = :enum 165 s[:enum_values] = values 166 end 167 end 168 end
Typecast the given value to a string.
# File lib/sequel/extensions/pg_enum.rb 171 def typecast_value_enum(value) 172 value.to_s 173 end