2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
require 'migration/column_dropper'
|
2017-04-26 14:47:36 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
class CreateUserEmails < ActiveRecord::Migration[4.2]
|
2017-04-26 14:47:36 -04:00
|
|
|
def up
|
|
|
|
create_table :user_emails do |t|
|
|
|
|
t.integer :user_id, null: false
|
|
|
|
t.string :email, limit: 513, null: false
|
|
|
|
t.boolean :primary, default: false, null: false
|
2017-08-07 11:48:36 -04:00
|
|
|
t.timestamps null: false
|
2017-04-26 14:47:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
add_index :user_emails, :user_id
|
|
|
|
add_index :user_emails, [:user_id, :primary], unique: true
|
|
|
|
|
|
|
|
execute "CREATE UNIQUE INDEX index_user_emails_on_email ON user_emails (lower(email));"
|
|
|
|
|
|
|
|
execute <<~SQL
|
|
|
|
INSERT INTO user_emails (
|
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
email,
|
|
|
|
"primary",
|
|
|
|
created_at,
|
|
|
|
updated_at
|
|
|
|
) SELECT
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
'TRUE',
|
|
|
|
created_at,
|
|
|
|
updated_at
|
|
|
|
FROM users
|
|
|
|
SQL
|
|
|
|
|
|
|
|
change_column_null :users, :email, true
|
2018-03-20 03:20:50 -04:00
|
|
|
Migration::ColumnDropper.mark_readonly(:users, :email)
|
2017-04-26 14:47:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|