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


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

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


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

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



75
76
77
78
79
80
81
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 75

def create_table(table_name, **options, &block)
  td = create_table_definition(table_name, **options)
  options[:id] = false if td.inherited_id?
  options[:temporary] = td

  super table_name, **options, &block
end

#drop_type(name, options = {}) ⇒ Object

Drops a type.



9
10
11
12
13
14
15
16
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 9

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.



70
71
72
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 70

def enum_values(name)
  select_values("SELECT unnest(enum_range(NULL::#{name}))", 'SCHEMA')
end

#rename_type(type_name, new_name) ⇒ Object

Renames a type.



19
20
21
22
23
24
# File 'lib/torque/postgresql/adapter/schema_statements.rb', line 19

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