2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/rails/migration/migration_generator"
|
2014-01-06 06:01:15 -05:00
|
|
|
|
|
|
|
class CreateMigrationTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
|
|
|
|
class Migrator < Rails::Generators::MigrationGenerator
|
|
|
|
include Rails::Generators::Migration
|
|
|
|
|
|
|
|
def self.next_migration_number(dirname)
|
|
|
|
current_migration_number(dirname) + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
tests Migrator
|
|
|
|
|
|
|
|
def default_destination_path
|
|
|
|
"db/migrate/create_articles.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_migration(destination_path = default_destination_path, config = {}, generator_options = {}, &block)
|
2016-08-06 13:16:09 -04:00
|
|
|
migration_name = File.basename(destination_path, ".rb")
|
2014-01-06 06:01:15 -05:00
|
|
|
generator([migration_name], generator_options)
|
|
|
|
generator.set_migration_assigns!(destination_path)
|
|
|
|
|
|
|
|
dir, base = File.split(destination_path)
|
2016-08-06 13:16:09 -04:00
|
|
|
timestamped_destination_path = File.join(dir, ["%migration_number%", base].join("_"))
|
2014-01-06 06:01:15 -05:00
|
|
|
|
|
|
|
@migration = Rails::Generators::Actions::CreateMigration.new(generator, timestamped_destination_path, block || "contents", config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migration_exists!(*args)
|
|
|
|
@existing_migration = create_migration(*args)
|
|
|
|
invoke!
|
|
|
|
@generator = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def invoke!
|
|
|
|
capture(:stdout) { @migration.invoke! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke!
|
|
|
|
capture(:stdout) { @migration.revoke! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke
|
|
|
|
create_migration
|
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/create db\/migrate\/1_create_articles\.rb\n/, invoke!)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_file @migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_pretended
|
2017-08-12 07:31:46 -04:00
|
|
|
create_migration(default_destination_path, {}, { pretend: true })
|
2014-01-06 06:01:15 -05:00
|
|
|
|
|
|
|
assert_no_file @migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_when_exists
|
|
|
|
migration_exists!
|
|
|
|
create_migration
|
|
|
|
|
|
|
|
assert_equal @existing_migration.destination, @migration.existing_migration
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_when_exists_identical
|
|
|
|
migration_exists!
|
|
|
|
create_migration
|
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/identical db\/migrate\/1_create_articles\.rb\n/, invoke!)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @migration, :identical?
|
2014-01-06 06:01:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_when_exists_not_identical
|
|
|
|
migration_exists!
|
|
|
|
create_migration { "different content" }
|
|
|
|
|
|
|
|
assert_raise(Rails::Generators::Error) { invoke! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_forced_when_exists_not_identical
|
|
|
|
dest = "db/migrate/migration.rb"
|
|
|
|
migration_exists!(dest)
|
|
|
|
create_migration(dest, force: true) { "different content" }
|
|
|
|
|
|
|
|
stdout = invoke!
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/remove db\/migrate\/1_migration\.rb\n/, stdout)
|
|
|
|
assert_match(/create db\/migrate\/2_migration\.rb\n/, stdout)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_file @migration.destination
|
|
|
|
assert_no_file @existing_migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_forced_pretended_when_exists_not_identical
|
|
|
|
migration_exists!
|
2017-08-12 07:31:46 -04:00
|
|
|
create_migration(default_destination_path, { force: true }, { pretend: true }) do
|
2014-01-06 06:01:15 -05:00
|
|
|
"different content"
|
|
|
|
end
|
|
|
|
|
|
|
|
stdout = invoke!
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, stdout)
|
|
|
|
assert_match(/create db\/migrate\/2_create_articles\.rb\n/, stdout)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_no_file @migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_skipped_when_exists_not_identical
|
|
|
|
migration_exists!
|
2017-08-12 07:31:46 -04:00
|
|
|
create_migration(default_destination_path, {}, { skip: true }) { "different content" }
|
2014-01-06 06:01:15 -05:00
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/skip db\/migrate\/2_create_articles\.rb\n/, invoke!)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_no_file @migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_revoke
|
|
|
|
migration_exists!
|
|
|
|
create_migration
|
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_no_file @existing_migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_revoke_pretended
|
|
|
|
migration_exists!
|
2017-08-12 07:31:46 -04:00
|
|
|
create_migration(default_destination_path, {}, { pretend: true })
|
2014-01-06 06:01:15 -05:00
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!)
|
2014-01-06 06:01:15 -05:00
|
|
|
assert_file @existing_migration.destination
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_revoke_when_no_exists
|
|
|
|
create_migration
|
|
|
|
|
2017-05-06 15:08:58 -04:00
|
|
|
assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!)
|
2014-01-06 06:01:15 -05:00
|
|
|
end
|
|
|
|
end
|