After all, what is [rails db: migrate] doing?

I use ** rails db: migrate ** as a matter of course in Rails, but I think there are some people who use it somehow and don't know what they are actually doing.

Until recently, I somehow understood that ** ah migration files are reflected in the database ~ **.

However, as I studied deeply, I wondered, "What are each Rails command doing after all?", So I summarized it in this article.

migrate is the act of reflecting the contents of the migration file in the DB

From the conclusion ** migrate is the act of reflecting the contents of the migration file in the DB ** Will be.

However, this alone is weak, so I will dig a little deeper.

About MVC model

First of all, the basic recognition part, the framework called Ruby on Rails uses a model called MVC.

Untitled Diagram.jpg

I will omit the details here, but

--Execute action in response to request from server ** Controller ** --Exchange with database (DB) ** Model ** rails g model --** View ** responsible for browser display

It consists of three parts.

rails g model

Before understanding migration, let's start with the model creation command.

When you execute the rails g model command, two types of files, a model file and a migration file, are created (strictly speaking, a test file etc. are also created automatically).

Each of these files

--migration file → Contents to be changed to DB --model file → Connect DB and Rails application

There is such a role.

Untitled Diagram.3png.png

In particular, all the models created here inherit the class called ApplicationRecord.

models/user.rb


class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
end

In addition, the parent class of this ApplicationRecord class inherits from ActiveRecord :: Base.

models/application_record.rb


class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

This ActiveRecord class has the ability to translate the SQL syntax needed to interact with the DB.

Therefore, ** you can easily access the DB and tamper with the data without writing SQL syntax each time **.

You may not be familiar with it when you enter from Rails, but in order to access the DB and manipulate the data, you usually have to skip the instructions using another language called SQL **.

SQL statement example


--Create table
CREATE TABLE USERS (
    ID    INT NOT NULL PRIMARY KEY,
    NAME  VARCHAR NOT NULL 
    AGE INT NOT NULL
);

--Create data(table in rails.create(value1, value2…))
INSERT INTO USERS VALUES (1,'Ichihara','');

--Select all data(In rails model.all)
SELECT * FROM USERS

However, Rails can use this ** ActiveRecord ** to automatically translate it into SQL and operate the DB ** with a more intuitive and simple syntax.

Also, since getters and setters are automatically defined in this ActiveRecord :: Base class, you can refer to the instance value ** without intentionally defining ** attr_accessor etc.

I see… It's certainly convenient, but what made it difficult to understand getters and setters ...

>> What are setters and getters in the first place?

rails db:migrate

Make changes to the DB based on the migration file created by running rails db: migrate. In this case, a new table is created on the DB for migrate when creating the model.

xxxxxxxx_create_users.rb


class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end

The table created by ActiveRecord has the following characteristics.

--Table name is plural of model (Post → posts) --id, created_at are automatically created

By the way, if you create a migration file with ** rails g migration **, the change method is automatically defined and you can make changes to the already created table.

xxxxxxxx_oooo.rb


class PasswordDigestToUsers < ActiveRecord::Migration[5.1]
  def change
    add_column :users, :password_digest, :string
  end
end

the end

That's all about migration.

I'm not very confident, so I'd appreciate it if you could point out any mistakes in Lee's commentary!

Recommended Posts

After all, what is [rails db: migrate] doing?
What is Rails Active Record?
After all, what is [rails db: migrate] doing?
What to do when Address already in use is displayed after executing rails s
[Rails] Modify migrate file (rails db: rollback)
[heroku] run rails db: migrate doesn't work
[Rails] What is a dot (.) Or a colon (:)?
[heroku] run rails db: migrate doesn't work
What is object-oriented after all?
What to do when Address already in use is displayed after executing rails s
What is RubyGem?
What is Byte?
rails db: 〇〇 Summary
What is Tomcat
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate