As I was working with Rails, I noticed that there were a lot of files and commands that I somehow knew existed, but didn't really understand what they did. One of them is the migration file.
When I make a model, I don't know why the migration file is created, and when I type rails db: migrate
, it is reflected as table data in schema.rb
...
That was the recognition, but I will dig deeper and explain it!
The migration file is the blueprint for creating the database. Also, by executing the migration file, a data table based on the described contents will be generated.
When you create a model by typing the following in the terminal, a migration file will also be generated automatically.
Basic syntax
rails g model model name#Here, the model name is "book"
db/migrate/20201114044025_create_books.rb
class CreateBooks < ActiveRecord::Migration[5.2]
def change
create_table :books do |t|
t.timestamps
end
end
end
You can see that only t.timestamps
is listed by default in the initial state when the migration file is completed.
This will add created_at
, which means the creation date and time, and updated_at
, which means the update date and time, to the column.
Now let's run the migration file we created! I feel that there are not enough columns that I actually need, but I dare to keep it as it is!
The created migration file is read by typing the following command in the terminal and reflected in the database.
command
rails db:migrate
This time, I created only one migration file. I think that you may create many models at the same time (or rather, I think there are more). In such a case, if you want to check how far the migration file you created is executed, type the following command in the terminal.
command
rails db:migrate:status
This will print the current migration file status to the terminal.
Terminal
Status Migration ID Migration Name
--------------------------------------------------
up 20201114044025 Create books
The migration file that is up is already executed, so it will not be read even if you enter the
rails db: migrate
command. So if you edit the migration file that isup
when you create a column with the wrong name, it will not be read and it will be meaningless.
Reference: [Rails] Thorough explanation of migration files!
Well, so what if you want to add a new column to the books table you just created? Let's check the schema file for the time being!
When the migration is executed, a file called schema.rb
will be created in the db folder.
Let's take a look at the contents immediately.
db/schema.rb
ActiveRecord::Schema.define(version: 2020_11_14_044025) do
create_table "books", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
It seems that the table has been created successfully.
The description force :: cascade
allows the schema to be reloaded if the foreign key is correct.
Now let's add a column called title
here!
There are two ways to add columns to an existing table.
rails db: rollback
In this case, enter the following command in the terminal
command
rails g migration Add Column name to add To Table name to add Column name to add:Type
In this case, it looks like this:
Terminal
rails g migration AddTitleToBooks title:string
Then, the following migration file will be created.
Migration file
class AddTitleToBooks < ActiveRecord::Migration[5.2]
def change
add_column :books, :title, :string
end
end
After that, run the migration file and the title
column will be added to the table.
command
rails db:migrate
rails db: rollback
command
rails db:migrate:rollback
Entering this command in the terminal reverts the latest migration file version to what it was before rails db: migrate
.
In this case, the terminal display changes as follows.
Terminal
Status Migration ID Migration Name
--------------------------------------------------
down 20201114044025 Create books #It's going from up to down! !! !! !!
In other words, going from up
to down
will bring the database back to what it was before rails db: migrate
.
Since we have returned to before db: migrate, we can add the title
column to the migration file created first.
db/migrate/20201114044025_create_books.rb
class CreateGenres < ActiveRecord::Migration[5.2]
def change
create_table :books do |t|
t.string :title #Add a title column here! !! !! !!
t.timestamps
end
end
end
After that, if you execute the migration file as in pattern (1), the title
column will be added.
command
rails db:migrate
db/schema.rb
ActiveRecord::Schema.define(version: 2020_11_14_044025) do
create_table "books", force: :cascade do |t|
t.string "title" #A title column has been added! !! !! !!
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
The work of modifying the migration file is often more sensitive than I expected, so I was reminded that I should work on it with a solid understanding of the timing and order. I learned one more thing! !!
Recommended Posts