2019-09-03 19:26:12 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Partynest
|
|
|
|
module Migration
|
|
|
|
def func(name, sql)
|
|
|
|
reversible do |dir|
|
2019-09-21 14:17:54 -04:00
|
|
|
dir.up { func_creation(name, sql).call }
|
|
|
|
dir.down { func_deletion(name).call }
|
2019-09-03 19:26:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-21 14:18:36 -04:00
|
|
|
def drop_func(name, sql)
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up { func_deletion(name).call }
|
|
|
|
dir.down { func_creation(name, sql).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-03 19:26:12 -04:00
|
|
|
def enum(name, values)
|
|
|
|
reversible do |dir|
|
2019-09-21 14:17:54 -04:00
|
|
|
dir.up { enum_creation(name, values).call }
|
|
|
|
dir.down { enum_deletion(name).call }
|
2019-09-03 19:26:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-21 14:18:36 -04:00
|
|
|
def drop_enum(name, values)
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up { enum_deletion(name).call }
|
|
|
|
dir.down { enum_creation(name, values).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-03 19:26:12 -04:00
|
|
|
def constraint(table, name, check)
|
|
|
|
reversible do |dir|
|
2019-09-21 14:17:54 -04:00
|
|
|
dir.up { constraint_creation(table, name, check).call }
|
|
|
|
dir.down { constraint_deletion(table, name).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-21 14:18:36 -04:00
|
|
|
def drop_constraint(table, name, check)
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up { constraint_deletion(table, name).call }
|
|
|
|
dir.down { constraint_creation(table, name, check).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-21 14:17:54 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def func_creation(name, sql)
|
|
|
|
lambda do
|
|
|
|
execute "CREATE FUNCTION #{name} #{sql}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enum_creation(name, values)
|
|
|
|
lambda do
|
|
|
|
execute <<~SQL
|
|
|
|
CREATE TYPE #{name}
|
|
|
|
AS ENUM (#{values.map { |s| "'#{s}'" }.join(', ')})
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def constraint_creation(table, name, check)
|
|
|
|
lambda do
|
|
|
|
execute <<~SQL
|
|
|
|
ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{check})
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def func_deletion(name)
|
|
|
|
lambda do
|
|
|
|
execute "DROP FUNCTION #{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enum_deletion(name)
|
|
|
|
lambda do
|
|
|
|
execute "DROP TYPE #{name}"
|
|
|
|
end
|
|
|
|
end
|
2019-09-03 19:26:12 -04:00
|
|
|
|
2019-09-21 14:17:54 -04:00
|
|
|
def constraint_deletion(table, name)
|
|
|
|
lambda do
|
|
|
|
execute "ALTER TABLE #{table} DROP CONSTRAINT #{name}"
|
2019-09-03 19:26:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|