2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Migration::SafeMigrate do
|
2018-03-20 03:20:50 -04:00
|
|
|
before { Migration::SafeMigrate::SafeMigration.disable_safe! }
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 03:20:50 -04:00
|
|
|
|
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
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-21 12:52:05 -04:00
|
|
|
|
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
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 03:20:50 -04:00
|
|
|
|
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
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 03:20:50 -04:00
|
|
|
|
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
|
|
|
|
|
2023-03-22 10:43:32 -04:00
|
|
|
it "allows dropping NOT NULL" do
|
|
|
|
Migration::SafeMigrate.enable!
|
|
|
|
|
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_not_null"
|
|
|
|
|
|
|
|
output = capture_stdout { migrate_up(path) }
|
|
|
|
|
|
|
|
expect(output).to include("change_column_null(:users, :username, true)")
|
|
|
|
end
|
|
|
|
|
2018-03-20 03:20:50 -04:00
|
|
|
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 { migrate_up(path) }
|
|
|
|
|
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
|
2020-05-15 02:23:27 -04:00
|
|
|
it "should not ban unsafe migrations using up" do
|
2018-10-08 03:47:38 -04:00
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
|
2020-05-15 02:23:27 -04:00
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate/drop_table"
|
|
|
|
|
|
|
|
output = capture_stdout { migrate_up(path) }
|
|
|
|
|
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not ban unsafe migrations using change" do
|
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
|
|
|
|
path = File.expand_path "#{Rails.root}/spec/fixtures/db/post_migrate/change"
|
2018-10-08 03:47:38 -04:00
|
|
|
|
|
|
|
output = capture_stdout { migrate_up(path) }
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-10-08 03:47:38 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-20 03:20:50 -04:00
|
|
|
end
|