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

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, options = {})
  before = options.fetch(:before, false)
  after  = options.fetch(:after,  false)

  before = enum_values(name).first if options.key? :prepend
  before = quote(before) unless before == false
  after  = quote(after)  unless after == false

  quote_enum_values(name, values, options).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, **options, &block)
  options[:id] = false if options[:inherits].present? &&
    options[:primary_key].blank? && options[:id].blank?

  super table_name, **options, &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, options = {})
  force = options.fetch(:force, '').upcase
  check = 'IF EXISTS' if options.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