Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

©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

Slide 5

Slide 5 text

©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

Slide 6

Slide 6 text

©2018 Wantedly, Inc. ςετ༻DBͷηοτΛ͔͚ͨͬͨͩ͠ͳͷʹɾɾɾ

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

©2018 Wantedly, Inc. ίʔυϦʔσΟϯά͢Δ͔͠ͳ͍ɾɾɾ

Slide 10

Slide 10 text

©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

Slide 11

Slide 11 text

©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

Slide 12

Slide 12 text

©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

Slide 13

Slide 13 text

©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

Slide 14

Slide 14 text

©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

Slide 15

Slide 15 text

©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

Slide 16

Slide 16 text

©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

Slide 17

Slide 17 text

©2018 Wantedly, Inc. 1. db:test:prepare࣌΋RAILS_ENVΛ࢖͍ͬͯΔ => current_environment͕RAILS_ENVʹͳΔ 2. ઀ଓ͍ͯ͠ΔDBͷar_internal_metadataΛݟ͍ͯΔ => ઀ଓ͢ΔDBΛద੾ʹ͢ΔඞཁΞϦ RAILS_ENV=test ͕ඞཁ ݁࿦

Slide 18

Slide 18 text

©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