Anatomy of a Migration
Migrations are subclasses of the Rails class ActiveRecord::Migration. When necessary, migrations can contain up and down methods:
| | class SomeMeaningfulName < ActiveRecord::Migration |
| | def up |
| | # ... |
| | end |
| | |
| | def down |
| | # ... |
| | end |
| | end |
The name of the class, after all uppercase letters are downcased and preceded by an underscore, must match the portion of the filename after the version number. For example, the previous class could be found in a file named 20240610000017_some_meaningful_name.rb. No two migrations can contain classes with the same name.
The up method is responsible for applying the schema changes for this migration, while the down method undoes those changes. Let’s make this more ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access