[ACCEPTED]-How does Rails keep track of which migrations have run for a database?-activerecord

Accepted answer
Score: 186

Rails creates a table in your database called 20 schema_migrations to keep track of which migrations have 19 run.

The table contains a single column, version. When 18 Rails runs a migration, it takes the leading 17 digits in the migration's file name and 16 inserts a row for that "version", indicating 15 it has been run. If you roll back that migration, Rails 14 will delete the corresponding row from schema_migrations.

For 13 example, running a migration file named 12 20120620193144_create_users.rb will insert a new row with a version of 11 20120620193144 into the schema_migrations table.

You are free at any point 10 to introduce migrations with earlier versions. Rails 9 will always run any new migrations for which 8 there is not a corresponding row in schema_migrations. The 7 leading digits don't have to be a timestamp, you 6 could call your migration 001_blah.rb. Earlier versions 5 of Rails used this format, and used sequential 4 numbering for newly generated migrations. Later 3 versions have switched to timestamps to 2 help prevent multiple developers from independently 1 generating migrations with the same number.

More Related questions