diff options
author | schneems <richard.schneeman@gmail.com> | 2016-01-07 17:40:38 -0600 |
---|---|---|
committer | schneems <richard.schneeman@gmail.com> | 2016-01-08 09:25:50 -0600 |
commit | a76c4233a9ee9ffbf413c4b8353e73e8ffbeb3a5 (patch) | |
tree | 9354c485cc20e06cf9a5874eedfec6f7b53a2150 | |
parent | 350ae6cdc1ea83e21c23abd10e7e99c9a0bbdbd2 (diff) | |
download | rails-a76c4233a9ee9ffbf413c4b8353e73e8ffbeb3a5.tar.gz rails-a76c4233a9ee9ffbf413c4b8353e73e8ffbeb3a5.tar.bz2 rails-a76c4233a9ee9ffbf413c4b8353e73e8ffbeb3a5.zip |
Add EnvironmentMismatchError
Raise an error when a destructive action is made on a database where the current environment is different from the environment stored in the database.
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 16 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 13 |
2 files changed, 25 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index c61491d58c..a7e747a482 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -163,6 +163,15 @@ module ActiveRecord end end + class EnvironmentMismatchError < ActiveRecordError + def initialize(current: current, stored: stored) + msg = "You are attempting to modify a database that was last run in #{ stored } environment.\n" + msg << "You are running in #{ current } environment." + msg << "if you are sure you want to continue, run the same command with the environment variable\n" + msg << "DISABLE_DATABASE_ENVIRONMENT_CHECK=1" + end + end + # = Active Record Migrations # # Migrations can manage the evolution of a schema used by several physical @@ -1224,8 +1233,7 @@ module ActiveRecord else migrated << version ActiveRecord::SchemaMigration.create!(version: version.to_s) - environment = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call - ActiveRecord::InternalMetadata.store(environment: environment) + ActiveRecord::InternalMetadata.store(environment: current_environment) end end @@ -1233,6 +1241,10 @@ module ActiveRecord ActiveRecord::InternalMetadata.value_for(:environment) end + def current_environment + ActiveRecord::ConnectionHandling::DEFAULT_ENV.call + end + def self.protected_environment? return false if current_version == 0 raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists? diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 6318a0725d..92f6f44de9 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -43,8 +43,17 @@ module ActiveRecord LOCAL_HOSTS = ['127.0.0.1', 'localhost'] def check_protected_environments! - if !ENV['DISABLE_DATABASE_internal_metadata'] && ActiveRecord::Migrator.protected_environment? - raise ActiveRecord::ProtectedEnvironmentError.new(ActiveRecord::Migrator.last_stored_environment) + unless ENV['DISABLE_DATABASE_internal_metadata'] + current = ActiveRecord::Migrator.current_environment + stored = ActiveRecord::Migrator.last_stored_environment + + if ActiveRecord::Migrator.protected_environment? + raise ActiveRecord::ProtectedEnvironmentError.new(stored) + end + + if current != stored + raise EnvironmentMismatchError.new(current: current, stored: stored) + end end rescue ActiveRecord::NoDatabaseError end |