Skip to content

Commit

Permalink
check irreversible migration (#27)
Browse files Browse the repository at this point in the history
* check irreversible migration

* delete unused method
  • Loading branch information
ka8725 authored Jul 20, 2023
1 parent 02ddc95 commit 0ff50ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion test/dummy_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2013_09_06_111512) do
ActiveRecord::Schema[7.0].define(version: 2013_09_06_111513) do
end
45 changes: 36 additions & 9 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ def run_migrations
Rake::Task["db:rollback_branches"].reenable
end

def dump_schema
Rake::Task["db:schema:dump"].invoke
Rake::Task["db:schema:dump"].reenable
end

def run_sql(sql)
ActiveRecord::Base.connection.execute(sql)
end
Expand All @@ -54,12 +49,16 @@ def delete_migrations_files
end
end

def define_migration_file(filename, content)
File.write(app_file("db/migrate/#{filename}"), content, mode: "w")
end

def define_migrations
{
first: "20130906111511_first.rb",
second: "20130906111512_second.rb"
}.each do |key, file_name|
File.write(app_file("db/migrate/#{file_name}"), %(
define_migration_file(file_name, <<~RUBY)
class #{key.to_s.camelize} < ActiveRecord::Migration[7.0]
def up
TestingState.up << :#{key}
Expand All @@ -69,10 +68,15 @@ def down
TestingState.down << :#{key}
end
end
), mode: "w")
RUBY
end
end

def prepare_phantom_migrations
run_migrations
delete_migrations_files # simulate switching branches
end

describe "db:rollback_branches" do
let(:migrated_files) do
Dir.glob(app_file("tmp/migrated/*.rb")).map { |f| File.basename(f) }.sort
Expand Down Expand Up @@ -106,10 +110,33 @@ def down
end

it "rolls back the migrations in the reversed order" do
run_migrations
prepare_phantom_migrations
assert_empty TestingState.down
delete_migrations_files # simulate switching branches
run_migrations
assert_equal %i[second first], TestingState.down
end

describe "with irreversible migration" do
before do
define_migration_file("20130906111513_irreversible.rb", <<~RUBY)
class Irreversible < ActiveRecord::Migration[7.0]
def up
TestingState.up << :irreversible
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
RUBY
end

it "keeps track of the irreversible migrations" do
prepare_phantom_migrations
assert_equal %i[first second irreversible], TestingState.up
assert_empty ActualDbSchema.failed
run_migrations
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
end
end
end

0 comments on commit 0ff50ef

Please sign in to comment.