LoginSignup
1
0

More than 1 year has passed since last update.

Heroku上のRailsアプリのDB(MySQL)をリセットする

Posted at

はじめに

Railsなどを中心に勉強中のエンジニア初心者が他の記事を参考にしたり、実際に実装してみたりして、アウトプットの一環としてまとめたものです。
間違っていることもあると思われるので、その際は指摘いただけると幸いです。

DBのリセット(エラー発生)

DBに挿入したテストデータが原因でアプリケーションがうまく動作していない場合など、DBをリセットする場合がある。

Heroku上で稼働させているRailsアプリのDB(MySQL)をリセットする方法を記載する。

実行コマンド

データベースをリセットするだけでなく、マイグレーションまで実行する。

heroku run rails db:migrate:reset

実行結果

本番環境として稼働させているアプリケーションの場合、単純にDBリセットコマンドを実行すると、Railsアプリ側から確認が入る。

% heroku run rails db:migrate:reset
Running rails db:migrate:reset on ⬢ sekkey777... up, run.1383 (Basic)
DEPRECATION WARNING: Spree::Order state machine defined in Spree::Order::Checkout is deprecated. Future versions of Solidus will use Spree::Core::StateMachines::Order} (called from <main> at /app/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.raise_with_invalid_currency set to true is deprecated. Please note that by switching this value, Spree::LineItem::CurrencyMismatch will not be raised anymore. (called from <main> at /app/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.consider_actionless_promotion_active set to true is deprecated. Please note that by switching this value, promotions with no actions will be considered active. (called from <main> at /app/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.run_order_validations_on_order_updater set to false is deprecated and will not be possibile in Solidus 3.0. Please switch this value to true and check that everything works as expected. (called from <main> at /app/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.use_legacy_address_state_validator set to true has been deprecated and will be removed in Solidus 3.0. The Spree::Address state validation has been extracted into a configurable external class. Switch Spree::Config.use_legacy_address_state_validator to false to start using the external validation class. (called from <main> at /app/config/environment.rb:5)
D, [2023-01-27T10:27:34.329961 #4] DEBUG -- :    (9.9ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2023-01-27T10:27:34.342716 #4] DEBUG -- :    (3.4ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
D, [2023-01-27T10:27:34.343973 #4] DEBUG -- :    (0.7ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2023-01-27T10:27:34.345217 #4] DEBUG -- :    (0.6ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
D, [2023-01-27T10:27:34.346263 #4] DEBUG -- :    (0.6ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
D, [2023-01-27T10:27:34.347435 #4] DEBUG -- :    (0.5ms)  SELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'environment'
rails aborted!
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
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/tasks/database_tasks.rb:63:in `check_protected_environments!'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/railties/databases.rake:18:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `block in execute'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `execute'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:241:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:241:in `invoke_prerequisites'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:241:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:241:in `invoke_prerequisites'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:188:in `invoke'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/rake_module.rb:59:in `with_application'
/app/vendor/bundle/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/app/vendor/bundle/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/command.rb:52:in `invoke'
/app/vendor/bundle/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/app/bin/rails:4:in `<main>'
Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

下記のエラーは、本番環境でDBリセットを実行して大丈夫か?と確認されている。

問題なければ「DISABLE_DATABASE_ENVIRONMENT_CHECK=1」をつけた状態で実行しろと言われている。

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

DBのリセット(エラーなし)

上記で出たエラーをもとに下記のコマンドを実行する。

実行コマンド

heroku run rails db:migrate:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1

実行結果

マイグレーション実行結果が出力される。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0