Module: Torque::PostgreSQL::Adapter::SchemaStatements
- Included in:
- Torque::PostgreSQL::Adapter
- Defined in:
- lib/torque/postgresql/adapter/schema_statements.rb
Constant Summary collapse
- TableDefinition =
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition
Instance Method Summary collapse
-
#add_enum_values(name, values, options = {}) ⇒ Object
Changes the enumerator by adding new values.
-
#create_table(table_name, **options, &block) ⇒ Object
Rewrite the method that creates tables to easily accept extra options.
-
#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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 35 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 " ALTER TYPE \#{quote_type_name(name, options[:schema])}\n ADD VALUE \#{value} \#{reference}\n SQL\n\n before = false\n after = value\n end\nend\n".squish |
#create_table(table_name, **options, &block) ⇒ Object
Rewrite the method that creates tables to easily accept extra options
66 67 68 69 70 71 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 66 def create_table(table_name, **, &block) [:id] = false if [:inherits].present? && [:primary_key].blank? && [:id].blank? super table_name, **, &block end |
#drop_type(name, options = {}) ⇒ Object
Drops a type.
11 12 13 14 15 16 17 18 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 11 def drop_type(name, = {}) force = .fetch(:force, '').upcase check = 'IF EXISTS' if .fetch(:check, true) execute " DROP TYPE \#{check}\n \#{quote_type_name(name, options[:schema])} \#{force}\n SQL\nend\n".squish |
#enum_values(name) ⇒ Object
Returns all values that an enum type can have.
57 58 59 60 61 62 63 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 57 def enum_values(name) select_values(" SELECT enumlabel FROM pg_enum\n WHERE enumtypid = \#{quote(name)}::regtype::oid\n ORDER BY enumsortorder\n SQL\nend\n".squish, 'SCHEMA') |
#rename_type(type_name, new_name) ⇒ Object
Renames a type.
21 22 23 24 25 26 |
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 21 def rename_type(type_name, new_name) execute " ALTER TYPE \#{quote_type_name(type_name)}\n RENAME TO \#{Quoting::Name.new(nil, new_name.to_s).quoted}\n SQL\nend\n".squish |