2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Migration::SafeMigrate do
|
|
|
|
before do
|
|
|
|
Migration::SafeMigrate::SafeMigration.disable_safe!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Migration::SafeMigrate.disable!
|
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
end
|
|
|
|
|
2018-06-05 15:29:17 +08:00
|
|
|
def migrate_up(path)
|
2019-09-12 08:41:50 +08:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(path, ActiveRecord::SchemaMigration).migrations
|
|
|
|
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration, migrations.first.version).run
|
2018-06-05 15:29:17 +08:00
|
|
|
end
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
it "bans all table removal" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_table"
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 15:29:17 +08:00
|
|
|
migrate_up(path)
|
2018-03-20 15:20:50 +08:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 15:20:50 +08:00
|
|
|
|
2018-03-22 01:06:44 +08:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-20 15:20:50 +08:00
|
|
|
expect(User.first).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2018-03-22 00:52:05 +08:00
|
|
|
it "bans all table renames" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/rename_table"
|
2018-03-22 00:52:05 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 15:29:17 +08:00
|
|
|
migrate_up(path)
|
2018-03-22 00:52:05 +08:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-03-22 01:06:44 +08:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-22 00:52:05 +08:00
|
|
|
expect(User.first).not_to eq(nil)
|
2018-06-05 15:29:17 +08:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-22 00:52:05 +08:00
|
|
|
end
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
it "bans all column removal" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/remove_column"
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 15:29:17 +08:00
|
|
|
migrate_up(path)
|
2018-03-20 15:20:50 +08:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-22 01:06:44 +08:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "bans all column renames" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/rename_column"
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 15:29:17 +08:00
|
|
|
migrate_up(path)
|
2018-03-20 15:20:50 +08:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-22 01:06:44 +08:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports being disabled" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
Migration::SafeMigrate.disable!
|
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_table"
|
2018-03-20 15:20:50 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
2018-06-05 15:29:17 +08:00
|
|
|
migrate_up(path)
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|
2018-10-08 15:47:38 +08:00
|
|
|
|
|
|
|
describe 'for a post deployment migration' do
|
|
|
|
it 'should not ban unsafe migrations' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate"
|
2018-10-08 15:47:38 +08:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
migrate_up(path)
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(user.reload).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|