Upgrade to Pro — share decks privately, control downloads, hide ads and more …

rake db:test:prepare ができなかった話

rake db:test:prepare ができなかった話

Hazumi Ichijo

October 29, 2018
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  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
  2. ©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
  3. ©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)>
  4. ©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
  5. ©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
  6. ©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
  7. ©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
  8. ©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
  9. ©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