1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/lib/partynest/migration.rb
2019-10-02 08:29:47 +05:00

122 lines
2.9 KiB
Ruby

# frozen_string_literal: true
module Partynest
module Migration
def add_func(name, sql)
reversible do |dir|
dir.up { func_creation(name, sql).call }
dir.down { func_deletion(name).call }
end
end
def remove_func(name, sql)
reversible do |dir|
dir.up { func_deletion(name).call }
dir.down { func_creation(name, sql).call }
end
end
def add_enum(name, values)
reversible do |dir|
dir.up { enum_creation(name, values).call }
dir.down { enum_deletion(name).call }
end
end
def remove_enum(name, values)
reversible do |dir|
dir.up { enum_deletion(name).call }
dir.down { enum_creation(name, values).call }
end
end
def add_constraint(table, name, check)
reversible do |dir|
dir.up { constraint_creation(table, name, check).call }
dir.down { constraint_deletion(table, name).call }
end
end
def remove_constraint(table, name, check)
reversible do |dir|
dir.up { constraint_deletion(table, name).call }
dir.down { constraint_creation(table, name, check).call }
end
end
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
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 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
def func_deletion(name)
lambda do
execute "DROP FUNCTION #{name}"
end
end
def enum_deletion(name)
lambda do
execute "DROP TYPE #{name}"
end
end
def constraint_deletion(table, name)
lambda do
execute "ALTER TABLE #{table} DROP CONSTRAINT #{name}"
end
end
def trigger_deletion(table, name)
lambda do
execute "DROP TRIGGER #{name} ON #{table}"
end
end
end
end