We've learned in lecture how to generate models in the terminal with the command:

rails generate model "model_name" "attribute:attribute_type",.. etc.

And to fully complete this model generation, we would call rails db:migrate to "migrate" all of the edits we've made to our models.

But let's start from the top.

rails db:migrate? What's a migration?

Simply put, a migration is an edit you make to the structure of your database. Our database is the collection of tables that hold our models' attributes, as defined within db/schema.rb.

Example db/schema.rb:

Here, my database contains cats, products, todos, and users.

Throughout my time with this Rails app, my tables will change depending on how my project needs change over time.

For example, right now, my User model has a username, email, and age column. Let's say I decide that my users should have a bio attribute to describe themselves. How do we do this?

I know what some of you folks are thinking; just edit the file directly. Do not do this.

The reason is that db/schema.rb is an autogenerated file — it's the "flattened" version of all of the files inside of your db/migrate folder.

Migrations are great in that they abstract all of what needs to be done to edit a table with simple commands within the terminal. Also, migrations are easy to implement because of their simple language syntax.

How migrations work

The way a migration works is by this format:

rails generate migration MigrationName <extra options>

The MigrationName here is significant! From the Rails documentation:

If the migration name is of the form AddXXXToYYY or RemoveXXXFromYYY and is followed by a list of column names and types (in <extra options>), then a migration containing the appropriate add_column and remove_column statements will be created.