2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Migration::SafeMigrate do
|
2018-03-20 15:20:50 +08:00
|
|
|
before { Migration::SafeMigrate::SafeMigration.disable_safe! }
|
|
|
|
|
|
|
|
after do
|
|
|
|
Migration::SafeMigrate.disable!
|
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
end
|
|
|
|
|
2018-06-05 15:29:17 +08:00
|
|
|
def migrate_up(path)
|
2024-06-20 17:42:17 +08:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(path, ActiveRecord::SchemaMigration).migrations
|
2019-09-12 08:41:50 +08:00
|
|
|
ActiveRecord::Migrator.new(
|
|
|
|
:up,
|
|
|
|
migrations,
|
2024-06-20 17:42:17 +08:00
|
|
|
ActiveRecord::SchemaMigration,
|
2019-09-12 08:41:50 +08:00
|
|
|
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
|
|
|
|
2022-02-26 10:51:39 +08:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 15:20:50 +08:00
|
|
|
|
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
|
|
|
|
2022-02-26 10:51:39 +08:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-22 00:52:05 +08:00
|
|
|
|
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
|
|
|
|
2022-02-26 10:51:39 +08:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 15:20:50 +08:00
|
|
|
|
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
|
|
|
|
2022-02-26 10:51:39 +08:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 15:20:50 +08:00
|
|
|
|
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
|
|
|
|
|
2023-03-22 22:43:32 +08: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) }
|
|
|
|
|
2024-06-20 17:42:17 +08:00
|
|
|
expect(output).to include("change_column_null(:users, :username, true)")
|
2023-03-22 22:43:32 +08:00
|
|
|
end
|
|
|
|
|
2018-03-20 15:20:50 +08:00
|
|
|
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 { migrate_up(path) }
|
|
|
|
|
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
|
2020-05-15 14:23:27 +08:00
|
|
|
it "should not ban unsafe migrations using up" do
|
2018-10-08 15:47:38 +08:00
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
|
2020-05-15 14:23:27 +08: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 15:47:38 +08:00
|
|
|
|
|
|
|
output = capture_stdout { migrate_up(path) }
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-10-08 15:47:38 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-20 15:20:50 +08:00
|
|
|
end
|