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 /activerecord/lib/active_record/migration.rb | |
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.
Diffstat (limited to 'activerecord/lib/active_record/migration.rb')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 16 |
1 files changed, 14 insertions, 2 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? |