2019-09-03 19:26:12 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Partynest
|
|
|
|
module Migration
|
2019-09-29 10:00:55 -04:00
|
|
|
def add_func(name, sql)
|
2019-09-03 19:26:12 -04:00
|
|
|
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-30 18:34:35 -04:00
|
|
|
def remove_func(name, sql)
|
2019-09-21 14:18:36 -04:00
|
|
|
reversible do |dir|
|
|
|
|
dir.up { func_deletion(name).call }
|
|
|
|
dir.down { func_creation(name, sql).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-29 10:00:55 -04:00
|
|
|
def add_enum(name, values)
|
2019-09-03 19:26:12 -04:00
|
|
|
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-30 18:34:35 -04:00
|
|
|
def remove_enum(name, values)
|
2019-09-21 14:18:36 -04:00
|
|
|
reversible do |dir|
|
|
|
|
dir.up { enum_deletion(name).call }
|
|
|
|
dir.down { enum_creation(name, values).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-29 10:00:55 -04:00
|
|
|
def add_constraint(table, name, check)
|
2019-09-03 19:26:12 -04:00
|
|
|
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-30 18:34:35 -04:00
|
|
|
def remove_constraint(table, name, check)
|
2019-09-21 14:18:36 -04:00
|
|
|
reversible do |dir|
|
|
|
|
dir.up { constraint_deletion(table, name).call }
|
|
|
|
dir.down { constraint_creation(table, name, check).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 21:31:12 -04:00
|
|
|
def add_trigger(table, name, events, call)
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up { trigger_creation(table, name, events, call).call }
|
|
|
|
dir.down { trigger_deletion(table, name).call }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_trigger(table, name, events, call)
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up { trigger_deletion(table, name).call }
|
|
|
|
dir.down { trigger_creation(table, name, events, call).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
|
|
|
|
|
2019-10-01 21:31:12 -04:00
|
|
|
def trigger_creation(table, name, events, call)
|
|
|
|
lambda do
|
|
|
|
execute <<~SQL
|
|
|
|
CREATE TRIGGER #{name}
|
|
|
|
#{events}
|
|
|
|
ON #{table}
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE #{call};
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-21 14:17:54 -04:00
|
|
|
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
|
2019-10-01 21:31:12 -04:00
|
|
|
|
|
|
|
def trigger_deletion(table, name)
|
|
|
|
lambda do
|
|
|
|
execute "DROP TRIGGER #{name} ON #{table}"
|
|
|
|
end
|
|
|
|
end
|
2019-09-03 19:26:12 -04:00
|
|
|
end
|
|
|
|
end
|