LoginSignup
0

More than 1 year has passed since last update.

[Rails6]Production環境でrails db:reset ,db:dropができない時

Posted at

今回は下記を参考にさせていただきました。

はじめに

Railsアプリの本番環境でrails db:resetを実行した所以下のエラーが出ました。

$ rails db:migrate:reset RAILS_ENV=production
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
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

原因

Rails5からはproduction環境で破壊的処理(内容は以下に記述)をすることができなくなっている。

db:drop
db:drop:all
db:reset
db:purge
db:purge:all
db:purge:test
db:schema:load

解決方法

ヒントはエラーメッセージに記載されている。

$ rails db:migrate:reset RAILS_ENV=production
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 ←ここ

環境変数としてDISABLE_DATABASE_ENVIRONMENT_CHECK=1を指定することでコマンドを実行することができます。

$ rails db:migrate:reset RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1
Dropped database 'hogehoge_production'
Created database 'hogehoge_production'
== 20210311185651 DeviseCreateUsers: migrating ================================

以上です。

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
0