2019-04-30 10:27:42 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 05:27:38 +03:00
|
|
|
RSpec.describe Migration::SafeMigrate do
|
2018-03-20 18:20:50 +11:00
|
|
|
before { Migration::SafeMigrate::SafeMigration.disable_safe! }
|
|
|
|
|
|
|
|
after do
|
|
|
|
Migration::SafeMigrate.disable!
|
|
|
|
Migration::SafeMigrate::SafeMigration.enable_safe!
|
|
|
|
end
|
|
|
|
|
2018-06-05 17:29:17 +10:00
|
|
|
def migrate_up(path)
|
2024-06-20 10:33:01 +02:00
|
|
|
migrations = ActiveRecord::MigrationContext.new(path).migrations
|
2019-09-12 10:41:50 +10:00
|
|
|
ActiveRecord::Migrator.new(
|
|
|
|
:up,
|
|
|
|
migrations,
|
2024-06-20 10:33:01 +02:00
|
|
|
ActiveRecord::Base.connection.schema_migration,
|
|
|
|
ActiveRecord::Base.connection.internal_metadata,
|
2019-09-12 10:41:50 +10:00
|
|
|
migrations.first.version,
|
|
|
|
).run
|
2018-06-05 17:29:17 +10:00
|
|
|
end
|
|
|
|
|
2018-03-20 18:20:50 +11: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 18:20:50 +11:00
|
|
|
|
2022-02-26 03:51:39 +01:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 18:20:50 +11:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 18:20:50 +11:00
|
|
|
|
2018-03-21 18:06:44 +01:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-20 18:20:50 +11:00
|
|
|
expect(User.first).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2018-03-21 17:52:05 +01: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-21 17:52:05 +01:00
|
|
|
|
2022-02-26 03:51:39 +01:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-21 17:52:05 +01:00
|
|
|
|
2018-03-21 18:06:44 +01:00
|
|
|
expect { User.first }.not_to raise_error
|
2018-03-21 17:52:05 +01:00
|
|
|
expect(User.first).not_to eq(nil)
|
2018-06-05 17:29:17 +10:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-21 17:52:05 +01:00
|
|
|
end
|
|
|
|
|
2018-03-20 18:20:50 +11: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 18:20:50 +11:00
|
|
|
|
2022-02-26 03:51:39 +01:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 18:20:50 +11:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 18:20:50 +11:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-21 18:06:44 +01:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 18:20:50 +11: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 18:20:50 +11:00
|
|
|
|
2022-02-26 03:51:39 +01:00
|
|
|
output = capture_stdout { expect do migrate_up(path) end.to raise_error(StandardError) }
|
2018-03-20 18:20:50 +11:00
|
|
|
|
2018-10-08 15:47:38 +08:00
|
|
|
expect(output).to include("rails g post_migration")
|
2018-03-20 18:20:50 +11:00
|
|
|
|
|
|
|
expect(User.first).not_to eq(nil)
|
2018-03-21 18:06:44 +01:00
|
|
|
expect { User.first.username }.not_to raise_error
|
2018-03-20 18:20:50 +11:00
|
|
|
end
|
|
|
|
|
2023-03-22 14:43:32 +00: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 10:33:01 +02:00
|
|
|
expect(output).to include("change_column_null(:users, :username, true, nil)")
|
2023-03-22 14:43:32 +00:00
|
|
|
end
|
|
|
|
|
2018-03-20 18:20:50 +11: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 18:20:50 +11:00
|
|
|
|
|
|
|
output = capture_stdout { migrate_up(path) }
|
|
|
|
|
2018-11-19 14:50:00 +01:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-03-20 18:20:50 +11: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 14:50:00 +01:00
|
|
|
expect(output).to include("drop_table(:email_logs)")
|
2018-10-08 15:47:38 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-20 18:20:50 +11:00
|
|
|
end
|