Skip to content

Commit

Permalink
improve specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed May 1, 2024
1 parent 83a0b54 commit 05d3299
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 137 deletions.
192 changes: 55 additions & 137 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,154 +2,72 @@

require "test_helper"

def app_file(path)
Rails.application.config.root.join(path)
end

def remove_app_dir(name)
FileUtils.rm_rf(app_file(name))
end

def run_migrations
Rake::Task["db:migrate"].invoke
Rake::Task["db:migrate"].reenable
Rake::Task["db:rollback_branches"].reenable
end

def run_sql(sql)
ActiveRecord::Base.connection.execute(sql)
end

def applied_migrations
run_sql("select * from schema_migrations").map do |row|
row["version"]
end
end

def clear_schema
run_sql("delete from schema_migrations")
end
describe "single db" do
let(:utils) { TestUtils.new }

def delete_migrations_files
Dir.glob(app_file("db/migrate/*.rb")).each do |file|
remove_app_dir(file)
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|
define_migration_file(file_name, <<~RUBY)
class #{key.to_s.camelize} < ActiveRecord::Migration[6.0]
def up
TestingState.up << :#{key}
end
def down
TestingState.down << :#{key}
end
end
RUBY
end
end
before { utils.cleanup }

def prepare_phantom_migrations
run_migrations
delete_migrations_files # simulate switching branches
end

def cleanup
delete_migrations_files
if ActiveRecord::SchemaMigration.respond_to?(:create_table)
ActiveRecord::SchemaMigration.create_table
else
ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table
end
run_sql("delete from schema_migrations")
remove_app_dir("tmp/migrated")
define_migrations
Rails.application.load_tasks
TestingState.reset
end

def migrated_files
Dir.glob(app_file("tmp/migrated/*.rb")).map { |f| File.basename(f) }.sort
end

describe "db:rollback_branches" do
before { cleanup }

it "creates the tmp/migrated folder" do
refute File.exist?(app_file("tmp/migrated"))
run_migrations
assert File.exist?(app_file("tmp/migrated"))
end

it "migrates the migrations" do
assert_empty applied_migrations
run_migrations
assert_equal %w[20130906111511 20130906111512], applied_migrations
end

it "keeps migrated migrations in tmp/migrated folder" do
run_migrations
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], migrated_files
end

it "rolls back the migrations in the reversed order" do
prepare_phantom_migrations
assert_empty TestingState.down
run_migrations
assert_equal %i[second first], TestingState.down
end
describe "db:rollback_branches" do
it "creates the tmp/migrated folder" do
refute File.exist?(utils.app_file("tmp/migrated"))
utils.run_migrations
assert File.exist?(utils.app_file("tmp/migrated"))
end

describe "with irreversible migration" do
before do
define_migration_file("20130906111513_irreversible.rb", <<~RUBY)
class Irreversible < ActiveRecord::Migration[6.0]
def up
TestingState.up << :irreversible
end
it "migrates the migrations" do
assert_empty utils.applied_migrations
utils.run_migrations
assert_equal %w[20130906111511 20130906111512], utils.applied_migrations
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
RUBY
it "keeps migrated migrations in tmp/migrated folder" do
utils.run_migrations
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files
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) })
it "rolls back the migrations in the reversed order" do
utils.prepare_phantom_migrations
assert_empty TestingState.down
utils.run_migrations
assert_equal %i[second first], TestingState.down
end
end
end

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

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

it "shows the list of phantom migrations" do
ActualDbSchema::Git.stub(:current_branch, "fix-bug") do
prepare_phantom_migrations
run_task
assert_match(/ Status Migration ID Branch Migration File/, TestingState.output)
assert_match(/---------------------------------------------------/, TestingState.output)
assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output)
assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output)
describe "db:phantom_migrations" do
it "shows the list of phantom migrations" do
ActualDbSchema::Git.stub(:current_branch, "fix-bug") do
utils.prepare_phantom_migrations
Rake::Task["db:phantom_migrations"].invoke
Rake::Task["db:phantom_migrations"].reenable
assert_match(/ Status Migration ID Branch Migration File/, TestingState.output)
assert_match(/---------------------------------------------------/, TestingState.output)
assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output)
assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output)
end
end
end
end
90 changes: 90 additions & 0 deletions test/support/test_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

class TestUtils
attr_accessor :migrations_path, :migrated_path

def initialize(migrations_path: "db/migrate", migrated_path: "tmp/migrated")
@migrations_path = migrations_path
@migrated_path = migrated_path
end

def app_file(path)
Rails.application.config.root.join(path)
end

def remove_app_dir(name)
FileUtils.rm_rf(app_file(name))
end

def run_migrations
Rake::Task["db:migrate"].invoke
Rake::Task["db:migrate"].reenable
Rake::Task["db:rollback_branches"].reenable
end

def run_sql(sql)
ActiveRecord::Base.connection.execute(sql)
end

def applied_migrations
run_sql("select * from schema_migrations").map do |row|
row["version"]
end
end

def clear_schema
run_sql("delete from schema_migrations")
end

def delete_migrations_files
Dir.glob(app_file("#{migrations_path}/**/*.rb")).each do |file|
remove_app_dir(file)
end
end

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

def define_migrations
{
first: "20130906111511_first.rb",
second: "20130906111512_second.rb"
}.each do |key, file_name|
define_migration_file(file_name, <<~RUBY)
class #{key.to_s.camelize} < ActiveRecord::Migration[6.0]
def up
TestingState.up << :#{key}
end
def down
TestingState.down << :#{key}
end
end
RUBY
end
end

def prepare_phantom_migrations
run_migrations
delete_migrations_files # simulate switching branches
end

def cleanup
delete_migrations_files
if ActiveRecord::SchemaMigration.respond_to?(:create_table)
ActiveRecord::SchemaMigration.create_table
else
ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table
end
run_sql("delete from schema_migrations")
remove_app_dir(migrated_path)
define_migrations
Rails.application.load_tasks
TestingState.reset
end

def migrated_files
Dir.glob(app_file("#{migrated_path}/*.rb")).map { |f| File.basename(f) }.sort
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "actual_db_schema"
require "minitest/autorun"
require "debug"
require "support/test_utils"

Rails.env = "test"

Expand Down

0 comments on commit 05d3299

Please sign in to comment.