Module: Ec::Pg::MigrationMixin

Defined in:
lib/ec/pg/migration_mixin.rb

Instance Method Summary collapse

Instance Method Details

#add_values_to_type(name, values, instruction = '') ⇒ Object



22
23
24
25
26
27
# File 'lib/ec/pg/migration_mixin.rb', line 22

def add_values_to_type(name, values, instruction = '')
  execute('commit;')   # The Alter Type query cannont happen inside a SQL transaction, so quit it.
  values.each do |value|
    execute "ALTER TYPE #{Ec::Pg::SchemaManager.current_schema}.#{name} ADD VALUE IF NOT EXISTS #{quote(value)} #{instruction}"
  end
end

#create_type(name, allow_blank: true) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/ec/pg/migration_mixin.rb', line 6

def create_type(name, allow_blank: true)
  values = allow_blank ? ['', 'null'] : []
  values += I18n.t('database_types.%s' % name).absolute_deepest_keys.map(&:to_s)
  quoted_values = values.map{|x| quote(x)}.join(',')
  execute <<-SQL
  CREATE TYPE #{Ec::Pg::SchemaManager.current_schema}.#{name} as ENUM (#{quoted_values});
  SQL
end

#drop_type(name) ⇒ Object



15
16
17
18
19
20
# File 'lib/ec/pg/migration_mixin.rb', line 15

def drop_type(name)
  execute <<-SQL
  DROP TYPE IF EXISTS #{Ec::Pg::SchemaManager.current_schema}.#{name};
  SQL

end