提交 83215b86 编写于 作者: R Richard Schneeman

Merge pull request #23015 from schneems/schneems/manual-environment-set

Allow manually setting environment value
...@@ -145,7 +145,7 @@ def initialize(message = DEFAULT_MESSAGE) ...@@ -145,7 +145,7 @@ def initialize(message = DEFAULT_MESSAGE)
class NoEnvironmentInSchemaError < MigrationError #:nodoc: class NoEnvironmentInSchemaError < MigrationError #:nodoc:
def initialize def initialize
msg = "Environment data not found in the schema. To resolve this issue, run: \n\n\tbin/rake db:migrate" msg = "Environment data not found in the schema. To resolve this issue, run: \n\n\tbin/rake db:environment:set"
if defined?(Rails.env) if defined?(Rails.env)
super("#{msg} RAILS_ENV=#{::Rails.env}") super("#{msg} RAILS_ENV=#{::Rails.env}")
else else
...@@ -157,7 +157,7 @@ def initialize ...@@ -157,7 +157,7 @@ def initialize
class ProtectedEnvironmentError < ActiveRecordError #:nodoc: class ProtectedEnvironmentError < ActiveRecordError #:nodoc:
def initialize(env = "production") def initialize(env = "production")
msg = "You are attempting to run a destructive action against your '#{env}' database\n" msg = "You are attempting to run a destructive action against your '#{env}' database\n"
msg << "if you are sure you want to continue, run the same command with the environment variable\n" msg << "If you are sure you want to continue, run the same command with the environment variable\n"
msg << "DISABLE_DATABASE_ENVIRONMENT_CHECK=1" msg << "DISABLE_DATABASE_ENVIRONMENT_CHECK=1"
super(msg) super(msg)
end end
...@@ -165,10 +165,15 @@ def initialize(env = "production") ...@@ -165,10 +165,15 @@ def initialize(env = "production")
class EnvironmentMismatchError < ActiveRecordError class EnvironmentMismatchError < ActiveRecordError
def initialize(current: nil, stored: nil) def initialize(current: nil, stored: nil)
msg = "You are attempting to modify a database that was last run in #{ stored } environment.\n" msg = "You are attempting to modify a database that was last run in `#{ stored }` environment.\n"
msg << "You are running in #{ current } environment." 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 << "If you are sure you want to continue, first set the environment using:\n\n"
msg << "DISABLE_DATABASE_ENVIRONMENT_CHECK=1" msg << "\tbin/rake db:environment:set"
if defined?(Rails.env)
super("#{msg} RAILS_ENV=#{::Rails.env}")
else
super(msg)
end
end end
end end
......
require 'active_record' require 'active_record'
db_namespace = namespace :db do db_namespace = namespace :db do
desc "Set the environment value for the database"
task "environment:set" => [:environment, :load_config] do
ActiveRecord::InternalMetadata.create_table
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
end
task :check_protected_environments => [:environment, :load_config] do task :check_protected_environments => [:environment, :load_config] do
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments! ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册