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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 50

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 <<-SQL.squish
      ALTER TYPE #{quote_type_name(name, options[: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


35
36
37
38
39
40
41
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 35

def create_enum(name, values, options = {})
  drop_type(name, options) if options[:force]
  execute <<-SQL.squish
    CREATE TYPE #{quote_type_name(name, options[:schema])} AS ENUM
    (#{quote_enum_values(name, values, options).join(', ')})
  SQL
end

#create_table(table_name, **options, &block) ⇒ Object

Rewrite the method that creates tables to easily accept extra options



81
82
83
84
85
86
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 81

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 <<-SQL.squish
    DROP TYPE #{check}
    #{quote_type_name(name, options[:schema])} #{force}
  SQL
end

#enum_values(name) ⇒ Object

Returns all values that an enum type can have.



72
73
74
75
76
77
78
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 72

def enum_values(name)
  select_values(<<-SQL.squish, 'SCHEMA')
    SELECT enumlabel FROM pg_enum
    WHERE enumtypid = #{quote(name)}::regtype::oid
    ORDER BY enumsortorder
  SQL
end

#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 <<-SQL.squish
    ALTER TYPE #{quote_type_name(type_name)}
    RENAME TO #{Quoting::Name.new(nil, new_name.to_s).quoted}
  SQL
end