2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 03:20:50 -04: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 03:29:17 -04:00
|
|
|
def migrate_up(path)
|
2019-09-11 20:41:50 -04:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(path, ActiveRecord::SchemaMigration).migrations
|
|
|
|
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration, migrations.first.version).run
|
2018-06-05 03:29:17 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
it "bans all table removal" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_table"
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 03:29:17 -04:00
|
|
|
migrate_up(path)
|
2018-03-20 03:20:50 -04:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 03:20:50 -04:00
|
|
|
|
2018-03-21 13:06:44 -04:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-20 03:20:50 -04:00
|
|
|
expect(User.first).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2018-03-21 12:52:05 -04:00
|
|
|
it "bans all table renames" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/rename_table"
|
2018-03-21 12:52:05 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 03:29:17 -04:00
|
|
|
migrate_up(path)
|
2018-03-21 12:52:05 -04:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-03-21 13:06:44 -04:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-21 12:52:05 -04:00
|
|
|
expect(User.first).not_to eq(nil)
|
2018-06-05 03:29:17 -04:00
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-21 12:52:05 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
it "bans all column removal" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/remove_column"
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 03:29:17 -04:00
|
|
|
migrate_up(path)
|
2018-03-20 03:20:50 -04:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-21 13:06:44 -04:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "bans all column renames" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/rename_column"
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
expect(lambda do
|
2018-06-05 03:29:17 -04:00
|
|
|
migrate_up(path)
|
2018-03-20 03:20:50 -04:00
|
|
|
end).to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-21 13:06:44 -04:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports being disabled" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
Migration::SafeMigrate.disable!
|
|
|
|
|
2018-10-08 03:47:38 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_table"
|
2018-03-20 03:20:50 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
2018-06-05 03:29:17 -04:00
|
|
|
migrate_up(path)
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|
2018-10-08 03:47:38 -04: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 08:50:00 -05:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate"
|
2018-10-08 03:47:38 -04:00
|
|
|
|
|
|
|
output = capture_stdout do
|
|
|
|
migrate_up(path)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-10-08 03:47:38 -04:00
|
|
|
expect(user.reload).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|