mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 03:43:40 +08:00
6a3c8fe69c
Often we need to amend our schema, it is tempting to use drop_table, rename_column and drop_column to amned schema trouble though is that existing code that is running in production can depend on the existance of previous schema leading to application breaking until new code base is deployed. The commit enforces new rules to ensure we can never drop tables or columns in migrations and instead use Migration::ColumnDropper and Migration::TableDropper to defer drop the db objects
43 lines
944 B
Ruby
43 lines
944 B
Ruby
require 'migration/column_dropper'
|
|
|
|
class CreateUserEmails < ActiveRecord::Migration[4.2]
|
|
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
|
|
t.timestamps null: false
|
|
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
|
|
Migration::ColumnDropper.mark_readonly(:users, :email)
|
|
end
|
|
|
|
def down
|
|
raise ActiveRecord::IrreversibleMigration
|
|
end
|
|
end
|