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

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.

Related Links

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.

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` to `` `development```.

$ 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

Recommended Posts

[Rails] How to delete production environment MySQL data after putting it in the development environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails] How to execute "rails db: create" etc. in production environment EC2
How to install Pry after building Rails development environment with Docker
How to delete large amounts of data in Rails and concerns
How to use MySQL in Rails tutorial
[Rails] Reset the database in the production environment
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
How to make a unique combination of data in the rails intermediate table
How to set the display time to Japan time in Rails
I get Mysql2 :: Error :: ConnectionError in the production environment
[Rails] How to display an image in the view
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
How to check the WEB application created in the PC development environment on your smartphone
[Rails] How to display information stored in the database in view
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails / Routing] How to refer to the controller in the directory you created
I tried to sort the data in descending order, ascending order / Rails
[Programming beginner] What to do when rails s becomes an error in the local development environment
[Rails] Various ways to delete data
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to install the language used in Ubuntu and how to build the environment
After learning Progate, I tried to make an SNS application using Rails in the local environment
How to introduce jQuery in Rails 6
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to add the delete function
How to install Swiper in Rails
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
How to execute with commands of normal development language in Docker development environment
[Rails] About the error that the image is not displayed in the production environment
How to change the maximum and maximum number of POST data in Spark
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Rails] How to get the user information currently logged in with devise
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Android development, how to check null in the value of JSON object
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to solve the time lag of created_at after save method
How to implement search functionality in Rails
How to change app name in rails
How to insert a video in Rails
[Rails] How to use the map method
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
How to delete data with foreign key
How to use environment variables in RubyOnRails
How to overwrite Firebase data in Swift
How to use credentials.yml.enc introduced in Rails 5.2
How to build Rails 6 environment with Docker
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
[Rails] How to register multiple records in the intermediate table with many-to-many association