Help us understand the problem. What is going on with this article?

【rails】rails db:resetをする手順

はじめに

複数人で共同開発をしていて、テーブルのカラムの追加や型の変更などをしていたところ、
スキーマファイルでコンフリクトが起きてしまったのでリセットしてしまおうという流れです。

備忘録として手順を残しておきます。

1.マイグレーションファイルの中身を整理する

removeやaddしたマイグレーションファイルをcreateにまとめる。

dbresetスクショ.png
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
dbresetスクショ2.png

2.rails db:reset

開発環境ターミナル
$ rails db:reset

テーブル内容が正しく変更されていることを確認。

3.リモートへpush、merge

4.本番環境でrails db:reset

本番環境ターミナル
[ec2-user@ip-172-31-43-73 ~]$ cd /var/www/freemarket_sample_57b
[ec2-user@ip-172-31-43-73 freemarket_sample_57b]$ git pull origin master

[ec2-user@ip-172-31-43-73 freemarket_sample_57b]$ RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake db:drop
Dropped database 'freemarket_sample_57b_production'
[ec2-user@ip-172-31-43-73 freemarket_sample_57b]$ rake db:create RAILS_ENV=production
Created database 'freemarket_sample_57b_production'
[ec2-user@ip-172-31-43-73 freemarket_sample_57b]$ rake db:migrate RAILS_ENV=production

次に、mysqlへログインして正しくテーブルが作られているかを確認します。
※SQLは最後に;(セミコロン)を忘れないように注意。

本番環境
[ec2-user@ip-172-31-43-73 freemarket_sample_57b]$ mysql -u root -p
Enter password:
mysql> show databases;
+----------------------------------+
| Database                         |
+----------------------------------+
| information_schema               |
| freemarket_sample_57b_production |
| mysql                            |
| performance_schema               |
+----------------------------------+
4 rows in set (0.00 sec)
mysql> use freemarket_sample_57b_production;

mysql> show tables;
+--------------------------------------------+
| Tables_in_freemarket_sample_57b_production |
+--------------------------------------------+
| addresses                                  |
| ar_internal_metadata                       |
| cards                                      |
| categories                                 |
| items                                      |
| schema_migrations                          |
| users                                      |
+--------------------------------------------+
7 rows in set (0.00 sec)

mysql> show columns from users;
+------------------------+--------------+------+-----+---------+----------------+
| Field                  | Type         | Null | Key | Default | Extra          |
+------------------------+--------------+------+-----+---------+----------------+
| id                     | int(11)      | NO   | PRI | NULL    | auto_increment |
| nickname               | varchar(255) | NO   |     | NULL    |                |
| email                  | varchar(255) | NO   | UNI | NULL    |                |
| image                  | varchar(255) | YES  |     | NULL    |                |
| last_name              | varchar(255) | NO   |     | NULL    |                |
| first_name             | varchar(255) | NO   |     | NULL    |                |
| last_name_kana         | varchar(255) | NO   |     | NULL    |                |
| first_name_kana        | varchar(255) | NO   |     | NULL    |                |
| birthdate_year         | int(11)      | NO   |     | NULL    |                |
| birthdate_month        | int(11)      | NO   |     | NULL    |                |
| birthdate_day          | int(11)      | NO   |     | NULL    |                |
| phone_number           | varchar(255) | NO   |     | NULL    |                |
| introduce              | text         | YES  |     | NULL    |                |
| encrypted_password     | varchar(255) | NO   |     |         |                |
| reset_password_token   | varchar(255) | YES  | UNI | NULL    |                |
| reset_password_sent_at | datetime     | YES  |     | NULL    |                |
| remember_created_at    | datetime     | YES  |     | NULL    |                |
| created_at             | datetime     | NO   |     | NULL    |                |
| updated_at             | datetime     | NO   |     | NULL    |                |
+------------------------+--------------+------+-----+---------+----------------+
19 rows in set (0.01 sec)

正しいことを確認。

5.各メンバーにpullしてもらう

6.各メンバーもrails db:migrate:reset

kawaihironorinoMacBook-puro:freemarket_sample_57b kawaihironori$ rails db:migrate:reset

参考にさせて頂いた記事

Rails5以降の本番環境でデータベースをresetする方法!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away