Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/etengine/scenario_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module ETEngine
module ScenarioMigration
NoScenariosMigrated = Class.new(RuntimeError)

NO_CHANGES_MESSAGE =
'No scenarios were changed. If this database holds no scenarios the migration ' \
'applies to, re-run with SKIP_SCENARIO_CHECK=1 to record it as applied.'

# Public: Yields all migrateable scenarios. If a scenario is changed while
# yielded it will be saved.
#
Expand All @@ -18,7 +22,8 @@ module ScenarioMigration
# Raises an error if no scenarios were migrated. This is useful if you are
# expecting scenarios to be migrated and want to fail the migration if
# none were. This is particularly valuable when deploying automatically
# where this might not be noticed. (default: true)
# where this might not be noticed. Never raises while migrating the test
# database. (default: true)
#
# since: -
# By default, all read-only scenarios and writeable scenarios modified
Expand Down Expand Up @@ -53,9 +58,10 @@ def migrate_scenarios(raise_if_no_changes: true, since: nil)
say("#{total}/#{total} (#{changed} migrated)")

# With continuous deployment, it might go unnoticed if no scenarios are
# migrated. If the developer knows that zero migrated scenarios is an
# error, they may
raise NoScenariosMigrated if changed.zero? && raise_if_no_changes
# migrated.
if raise_if_no_changes && changed.zero? && !skip_no_changes_check?
raise NoScenariosMigrated, NO_CHANGES_MESSAGE
end

nil
end
Expand All @@ -66,6 +72,15 @@ def down

private

def skip_no_changes_check?
test_database? || ENV['SKIP_SCENARIO_CHECK'].present?
end

# True while migrating the test database.
def test_database?
ActiveRecord::Base.connection_db_config.env_name == 'test'
end

def scenarios(since)
since.nil? ? Scenario.migratable : Scenario.migratable_since(since)
end
Expand Down