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

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

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

9ec2ceee3f782e64fd67fa7d82b6c1b4?s=128

Hazumi Ichijo

October 29, 2018
Tweet

More Decks by Hazumi Ichijo

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. rake db:test:prepare͕ Ͱ͖ͳ͔ͬͨ࿩ Meguro.rb#20 29.Oct.2018 - rerost/hazumirr

  2. ©2018 Wantedly, Inc. ࣗݾ঺հ Ұᑍ୺੅!IB[VNJSS!SFSPTU 8FC"QQMJDBUJPO&OHJOFFS (P 3BJMT 3FBDUʜ ۴࿏ߴઐஜ೾େֶ8BOUFEMZ

  3. ©2018 Wantedly, Inc. rake db:test:prepare ͕ಈ͔ͳ͍ɾɾɾ

  4. ©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
  5. ©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
  6. ©2018 Wantedly, Inc. ςετ༻DBͷηοτΛ͔͚ͨͬͨͩ͠ͳͷʹɾɾɾ

  7. ©2018 Wantedly, Inc. Wantedly Visitͷ։ൃ؀ڥ ϩʔΧϧDB ڞ༻DB - ΈΜͳ࢖͏ͷͰڞ༻DBͰ͸
 migrationͳͲ͸ېࢭ

    - ςετ࣌͸ϩʔΧϧDBΛ࢖͏
  8. ©2018 Wantedly, Inc. Wantedly Visitͷ։ൃ؀ڥ ϩʔΧϧDB ڞ༻DB - ΈΜͳ࢖͏ͷͰڞ༻DBͰ͸
 migrationͳͲ͸ېࢭ

    - ςετ࣌͸ϩʔΧϧDBΛ࢖͏
  9. ©2018 Wantedly, Inc. ίʔυϦʔσΟϯά͢Δ͔͠ͳ͍ɾɾɾ

  10. ©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)>
  11. ©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
  12. ©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
  13. ©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
  14. ©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
  15. ©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
  16. ©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
  17. ©2018 Wantedly, Inc. 1. db:test:prepare࣌΋RAILS_ENVΛ࢖͍ͬͯΔ => current_environment͕RAILS_ENVʹͳΔ 2. ઀ଓ͍ͯ͠ΔDBͷar_internal_metadataΛݟ͍ͯΔ =>

    ઀ଓ͢ΔDBΛద੾ʹ͢ΔඞཁΞϦ RAILS_ENV=test ͕ඞཁ ݁࿦
  18. ©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