[RUBY] [Rails] About migration files

What is a migration file?

A migration file is a file that describes the changes you make to the database.

Basic commands


$rails g model model name

#Migration file creation command
$rails g migration class name

The migration file is created when the model is created. It is also possible to specify the class name and create the migration file alone. It should be easy to understand that the class name is created by "action + table name"! This will create a file called /db/migrate/timestamp_classname.rb. Here are the changes to the schema!

Creating a table


$rails g model model name column name: data type

Specify the content to be described in the migration file when creating the model in "Column name: Data type".

The data types that can be specified are as follows.

Reflect the migration file in the DB


#Executing the migration file
$ rails db:migrate

When this command is executed, the contents of the migration file will be reflected in the DB.

Rollback of migration file


#Roll back the migration file
$ rails db:rollback

Roll back the executed migration file.

Check migration file


#Check the executed migration file
$ rake db:migrate:status

You can check the contents of the migration file executed by this command.

Recommended Posts

[Rails] About migration files
Rails migration
About Rails 6
About Rails routing
[Rails] About ActiveJob ,!
About Rails controller
About RSpec (Rails)
[Rails 6] About main gems
[Rails] About active hash
[Rails] Migration command summary
About rails application server
Various rails migration operations
About rails kaminari pagination
About rails version specification
MEMO about Rails 6 series
[Rails] About Slim notation
Understand migration in rails
[rails] About devise defaults
Rails "How to delete NO FILE migration files"
Rails: About partial templates
About rails strong parameters
[Beginner] About Rails Session
about the where method (rails)
[Ruby on Rails] about has_secure_password
About naming Rails model methods
[rails] Deleted NOFILE migration file
[Rails] About scss folder structure
Cancel Ruby on Rails migration
[Rails] About Rspec response test
[Rails] Delete the migration file
How to rollback migration files
About Rails scraping method Mechanize
[Rails] About the Punk List function
About the symbol <%%> in Rails erb
[Rails] About implementation of like function
Check the migration status of rails
[Rails] About helper method form_with [Basic]
About =
Consideration about Rails and Clean Architecture
Migration file to add comment to Rails table
[Ruby on Rails] About bundler (for beginners)
[Rails 6.0] About batch saving of multiple records
[Ruby on Rails] About Active Record callbacks
[Rails] About local: true described in form_with
Rails migration column changes and so on.
Rails: A little summary about data types