1
0

More than 1 year has passed since last update.

ActiveRecord::ProtectedEnvironmentError: の対処方法

Posted at

はじめに

Railsチュートリアル第7版の10章の終わりにデータをRenderにデプロイしたところ、タイトルのようなエラーが発生しました。

環境

OS: macOS Monterey Version 12.3
Ruby: 3.1.2
Rails: 7.0.4

発生したエラー

Manual DeployのDeploy latest commitを実行後、Renderのログに以下のエラーが発生しました。

Dec 29 11:35:59 AM  rails aborted!
Dec 29 11:35:59 AM  ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
Dec 29 11:35:59 AM  If you are sure you want to continue, run the same command with the environment variable:
Dec 29 11:35:59 AM  DISABLE_DATABASE_ENVIRONMENT_CHECK=1
Dec 29 11:35:59 AM
Dec 29 11:35:59 AM  Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
Dec 29 11:35:59 AM  (See full trace by running task with --trace)
Dec 29 11:35:59 AM  ==> Build failed 😞

原因

Rails5より実装された、本番環境のDBを誤って削除しないようにするための防止機能が働いているようです。

対処方法

エラーログにあるように、render-build.shにDISABLE_DATABASE_ENVIRONMENT_CHECK=1を追加しました。

render-build.sh
#!/usr/bin/env bash
# exit on error
set -o errexit
bundle install
bundle exec rails assets:precompile
bundle exec rails assets:clean
DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rails db:migrate:reset
bundle exec rails db:seed

参考サイト

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