[Ruby] [Rails] How to delete production environment MySQL data after putting it in the development environment

2 minute read

Introduction

When I put MySQL data into the development environment in the production environment, I was able to input it without any error, but when I erased it, an error occurred and I was addicted to it.
It may be a little unusual case, but I decided to summarize it as a reference for other people.

Please refer to the following for an article on how to put MySQL data in the production environment into the development environment in the first place.

  • How to enter data into local MySQL after connecting to MySQL (RDS) from EC2
    • https://qiita.com/syukan3/items/d73d23611c8bfccfa7e4

error

When I executed the command to delete the DB itself in the development environment, …

$ rails db:drop

I got the error ActiveRecord :: ProtectedEnvironmentError. .. ..

The meaning of this error is

I’m trying to operate a DB in a production environment, is that okay?

It’s like a warning, you can continue the operation by entering an environment variable. I’m saying.

It’s a development environment in the first place, but why does it come out as a production 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
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

Conclusion

I will write from the conclusion first,

In Rails5, environment variables are stored in MySQL, and by putting the data of the production environment into the development environment as it is, the variables of the production environment are stored in the environment variables of MySQL, so the above error occurred. It was.

So, the solution is to rewrite the MySQL environment variables from those in the production environment to those in the development environment.

Solution procedure

Connect to MySQL.

$ mysql -h db -u root -p

Check the existing database.

$ show databases;

Specify the target database.

$ use *******;

Display a list of tables in the database.

$ show tables;

I think there is ar_internal_metadata in the table list.
Environment variables are stored in this.

+---------------------------------------------------+
| Tables_in_scm_development                         |
+---------------------------------------------------+
| ar_internal_metadata                              |
| ...                                               |
+---------------------------------------------------+

Looking at the contents of ar_internal_metadata,

$ select * from ar_internal_metadata;

I found the letters production.

+-------------+-------------+---------------------+---------------------+
| key         | value       | created_at          | updated_at          |
+-------------+-------------+---------------------+---------------------+
| environment | production  | 2020-01-10 09:37:29 | 2020-01-10 09:37:29 |
+-------------+-------------+---------------------+---------------------+

Modify this production` `` todevelopment```.

$ update ar_internal_metadata set value='development'

Looking at the contents of ar_internal_metadata again,

$ select * from ar_internal_metadata;

The environment variable could be changed to development.

+-------------+-------------+---------------------+---------------------+
| key         | value       | created_at          | updated_at          |
+-------------+-------------+---------------------+---------------------+
| environment | development | 2020-01-10 09:37:29 | 2020-01-10 09:37:29 |
+-------------+-------------+---------------------+---------------------+

Get out of MySQL

$ exit

Execute the command to delete the DB again. Then I succeeded!

$ rails db:drop

Summary

It may be difficult for some people to understand because I wrote the procedure in a hurry, but I hope it will be useful for someone in need.

reference

  • UnknownMigrationVersionError and ProtectedEnvironmentError (Rails 5) after populating production data into local DB
    • https://ontheneworbit.blogspot.com/2019/07/db-unknownmigrationversionerror.html