How does Rails keep track of which migrations have run for a database?

Zack Xu

According to Rails doc: http://guides.rubyonrails.org/migrations.html

"Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate."

How does ActiveRecord actually do this? Where does Active Record store the data?

I suspect this might be stored in the database itself? In a table somewhere.

On my development machine, I ran all the migrations. Then I copied the production database over using mysqldump. Then I ran "rake db:migrate:status", it shows correctly the migrations that need to run on the production database.

I used to think that ActiveRecord keeps track of the last migration run using the timestamp. But I think this is not true because ActiveRecord correctly runs the "older" migrations merged in from another code branch.

Could someone with inside knowledge of this elaborate? Thanks

user229044

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

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

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How does django know which migrations have been run?

How do you see what rails3 migrations have been run against a database

How to keep track of "streaks" in the database?

Is there a way to track which Sub have been run

Simple way to run rails migrations for a remote database

How to run database migrations on new database in Azure?

TypeORM CLI - How to run MIGRATIONS on a SPECIFIC DATABASE

How does Netbeans keep track of the projects' locations?

How does CPython keep track of the type of an object

How does this function keep track of clicks?

How does the Software Center keep track of packages?

How does a computer keep track of Ram?

How does SQS keep track of messages?

Cloud run with database migrations

How can I keep track of which text entry is clicked?

how to keep track of image which should not goes out of the container

How does SQLAlchemy track database changes?

How to keep track of columns that have been changed in a pandas dataframe

How to control which order the EF Core run custom migrations?

How to run migrations on specific database using call_command() in Django?

How to run Django migrations inside kubernetes and apply changes to the database pod?

Have migrations and a service that needs the database?

How does the kernel keep track of frames being mapped to other processes?

How does fgets() keep track of what line it's on?

How does git keep track of versions in the remote repository?

How does process manager keep track of the association between aggregates

How does `lsof` keep track of open file descriptors' filenames?

How does Python keep track of changes in class attributes?

How does one keep track of all the components in a Java Application?