Share
Embed
rake db:test:prepare ができなかった話
Hazumi Ichijo
October 29, 2018
Transcript
18 slides
-
©2018 Wantedly, Inc. $ bin/rake db:test:prepare ActiveRecord::ProtectedEnvironmentError: You are attempting
to run a destructive action against your 'production' database. If you are sure you want to continue, run the same command with the environment variable: DISABLE_DATABASE_ENVIRONMENT_CHECK=1 -
©2018 Wantedly, Inc. $ bin/rake db:test:prepare ActiveRecord::ProtectedEnvironmentError: You are attempting
to run a destructive action against your 'production' database. If you are sure you want to continue, run the same command with the environment variable: DISABLE_DATABASE_ENVIRONMENT_CHECK=1 -
©2018 Wantedly, Inc. $ bundle exec rake -W “db:test:prepare" …
rake db:test:prepare …/vendor/bundle/ruby/2.5.0/gems/activerecord-5.0.4/lib/ active_record/railties/databases.rake:361:in `block (2 levels) in <top (required)> -
©2018 Wantedly, Inc. https://github.com/rails/rails/blob/8688fc4e404e687fe66c7ac214c33ea82710f57e/activerecord/lib/active_record/railties/ databases.rake#L361-L365 task :prepare => %w(environment load_config)
do unless ActiveRecord::Base.configurations.blank? db_namespace['test:load'].invoke end end -
©2018 Wantedly, Inc. db:test:prepare => db:test:load => db:test:load_schema => db:test:purge
=> db:check_protected_environments ͜͜ͰνΣοΫ͕Δ task :check_protected_environments => [:environment, :load_config] do ActiveRecord::Tasks::DatabaseTasks.check_protected_environments! end -
©2018 Wantedly, Inc. def check_protected_environments! unless ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] current = ActiveRecord::Migrator.current_environment
stored = ActiveRecord::Migrator.last_stored_environment if ActiveRecord::Migrator.protected_environment? raise ActiveRecord::ProtectedEnvironmentError.new(stored) end if stored && stored != current raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored) end end rescue ActiveRecord::NoDatabaseError end -
©2018 Wantedly, Inc. def check_protected_environments! unless ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] current = ActiveRecord::Migrator.current_environment
stored = ActiveRecord::Migrator.last_stored_environment if ActiveRecord::Migrator.protected_environment? raise ActiveRecord::ProtectedEnvironmentError.new(stored) end if stored && stored != current raise ActiveRecord::EnvironmentMismatchError.new(current: current, stored: stored) end end rescue ActiveRecord::NoDatabaseError end -
©2018 Wantedly, Inc. def self.current_environment ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end DEFAULT_ENV = ->
{ RAILS_ENV.call || "default_env" } https://github.com/rails/rails/blob/8688fc4e404e687fe66c7ac214c33ea82710f57e/activerecord/lib/active_record/ connection_handling.rb#L4 https://github.com/rails/rails/blob/8688fc4e404e687fe66c7ac214c33ea82710f57e/activerecord/lib/active_record/ migration.rb#L1281-L1283 -
©2018 Wantedly, Inc. https://github.com/rails/rails/blob/8688fc4e404e687fe66c7ac214c33ea82710f57e/activerecord/lib/active_record/ migration.rb#L1272-L1279 def self.last_stored_environment return nil if
current_version == 0 raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists? environment = ActiveRecord::InternalMetadata[:environment] raise NoEnvironmentInSchemaError unless environment environment end https://github.com/rails/rails/blob/8688fc4e404e687fe66c7ac214c33ea82710f57e/activerecord/lib/active_record/ migration.rb#L1272-L1279 -
©2018 Wantedly, Inc. 1. db:test:prepare࣌RAILS_ENVΛ͍ͬͯΔ => current_environment͕RAILS_ENVʹͳΔ 2. ଓ͍ͯ͠ΔDBͷar_internal_metadataΛݟ͍ͯΔ =>
ଓ͢ΔDBΛదʹ͢ΔඞཁΞϦ RAILS_ENV=test ͕ඞཁ ݁ -
©2018 Wantedly, Inc. ͪͳΈʹɺ࠷৽ͷRais 5.2.1ͰNG https://github.com/rerost/test-db-prepare $ bin/rake db:schema:load $
bin/rails r 'ActiveRecord::InternalMetadata[:environment] = "production"' $ bin/rake db:test:prepare