How Rails handle migration version?

Post by Lưu Đại at 28-05-2023
Rails manages migration through migration files in db/migration and schema table that will be create automatically when database is created. 

1. Manage through db/migration

  • Each migration files is named <timestamp>_migration_name. Timestamp is the time that migration was created. 
  • Migration files always be sorted (ascending) by its timestamp. 

2. Manage through schema_migrations table

  • Each time a migration file is ran, Rails will add 1 record to schema_migrations table, the value is the timestamp of ran migration file.
  • Each time we run rails db:migration Rails will check if there're migrations that has not show up in schema_migrations table, it will run those missing migrations. 
  • Each time we run rails db:rollback the rollback timestamp record will be deleted from schema_migrations.
image.png