Improve migration
This commit is contained in:
parent
b9f7df0ef5
commit
9a6904829f
1 changed files with 18 additions and 21 deletions
|
@ -2,29 +2,11 @@
|
||||||
|
|
||||||
class InitialMigration < ActiveRecord::Migration[6.0]
|
class InitialMigration < ActiveRecord::Migration[6.0]
|
||||||
def change
|
def change
|
||||||
reversible do |dir|
|
enum :sex, %i[male female]
|
||||||
dir.up do
|
|
||||||
execute <<~SQL
|
|
||||||
CREATE TYPE sex AS ENUM ('male', 'female');
|
|
||||||
|
|
||||||
CREATE TYPE relationship_status AS ENUM (
|
enum :relationship_status, %i[supporter excluded member]
|
||||||
'supporter',
|
|
||||||
'excluded',
|
|
||||||
'member'
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TYPE relationship_role AS ENUM ('manager', 'supervisor');
|
enum :relationship_role, %i[manager supervisor]
|
||||||
SQL
|
|
||||||
end
|
|
||||||
|
|
||||||
dir.down do
|
|
||||||
execute <<~SQL
|
|
||||||
DROP TYPE sex;
|
|
||||||
DROP TYPE relationship_status;
|
|
||||||
DROP TYPE relationship_role;
|
|
||||||
SQL
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
create_table :contacts_lists do |t|
|
create_table :contacts_lists do |t|
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
|
@ -251,6 +233,21 @@ class InitialMigration < ActiveRecord::Migration[6.0]
|
||||||
|
|
||||||
private
|
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)
|
def constraint(table, name, check)
|
||||||
reversible do |dir|
|
reversible do |dir|
|
||||||
dir.up do
|
dir.up do
|
||||||
|
|
Reference in a new issue