1
0
Fork 0

Improve migration

This commit is contained in:
Alex Kotov 2019-07-22 09:29:48 +05:00
parent b9f7df0ef5
commit 9a6904829f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -2,29 +2,11 @@
class InitialMigration < ActiveRecord::Migration[6.0]
def change
reversible do |dir|
dir.up do
execute <<~SQL
CREATE TYPE sex AS ENUM ('male', 'female');
enum :sex, %i[male female]
CREATE TYPE relationship_status AS ENUM (
'supporter',
'excluded',
'member'
);
enum :relationship_status, %i[supporter excluded member]
CREATE TYPE relationship_role AS ENUM ('manager', 'supervisor');
SQL
end
dir.down do
execute <<~SQL
DROP TYPE sex;
DROP TYPE relationship_status;
DROP TYPE relationship_role;
SQL
end
end
enum :relationship_role, %i[manager supervisor]
create_table :contacts_lists do |t|
t.timestamps null: false
@ -251,6 +233,21 @@ class InitialMigration < ActiveRecord::Migration[6.0]
private
def enum(name, values)
reversible do |dir|
dir.up do
execute <<~SQL
CREATE TYPE #{name}
AS ENUM (#{values.map { |s| "'#{s}'" }.join(', ')})
SQL
end
dir.down do
execute "DROP TYPE #{name}"
end
end
end
def constraint(table, name, check)
reversible do |dir|
dir.up do