Module: Torque::PostgreSQL::Adapter::SchemaStatements
- Included in:
- Torque::PostgreSQL::Adapter
- Defined in:
- lib/torque/postgresql/adapter/schema_statements.rb
Instance Method Summary collapse
-
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values.
-
#create_enum(name, values, options = {}) ⇒ Object
Creates a new PostgreSQL enumerator type.
-
#drop_type(name, options = {}) ⇒ Object
Drops a type.
-
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
-
#rename_type(type_name, new_name) ⇒ Object
Renames a type.
Instance Method Details
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values
Example:
add_enum_values 'status', ['baz']
add_enum_values 'status', ['baz'], before: 'bar'
add_enum_values 'status', ['baz'], after: 'foo'
add_enum_values 'status', ['baz'], prepend: true
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 46 def add_enum_values(name, values, = {}) before = .fetch(:before, false) after = .fetch(:after, false) before = enum_values(name).first if .key? :prepend before = quote(before) unless before == false after = quote(after) unless after == false quote_enum_values(name, values, ).each do |value| reference = "BEFORE #{before}" unless before == false reference = "AFTER #{after}" unless after == false execute <<-SQL ALTER TYPE #{quote_type_name(name, [:schema])} ADD VALUE #{value} #{reference} SQL before = false after = value end end |
#create_enum(name, values, options = {}) ⇒ Object
Creates a new PostgreSQL enumerator type
Example:
create_enum 'status', ['foo', 'bar']
create_enum 'status', ['foo', 'bar'], prefix: true
create_enum 'status', ['foo', 'bar'], suffix: 'test'
create_enum 'status', ['foo', 'bar'], force: true
31 32 33 34 35 36 37 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 31 def create_enum(name, values, = {}) drop_type(name, ) if [:force] execute <<-SQL CREATE TYPE #{quote_type_name(name, [:schema])} AS ENUM (#{quote_enum_values(name, values, ).join(', ')}) SQL end |
#drop_type(name, options = {}) ⇒ Object
Drops a type.
7 8 9 10 11 12 13 14 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 7 def drop_type(name, = {}) force = .fetch(:force, '').upcase check = 'IF EXISTS' if .fetch(:check, true) execute <<-SQL DROP TYPE #{check} #{quote_type_name(name, [:schema])} #{force} SQL end |
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
68 69 70 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 68 def enum_values(name) select_values("SELECT unnest(enum_range(NULL::#{name}))") end |
#rename_type(type_name, new_name) ⇒ Object
Renames a type.
17 18 19 20 21 22 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 17 def rename_type(type_name, new_name) execute <<-SQL ALTER TYPE #{quote_type_name(type_name)} RENAME TO #{Quoting::Name.new(nil, new_name.to_s).quoted} SQL end |