ActiveRecord `schema_migrations` store a hash of the migration text

We use ActiveRecord migrations for our application. I’m trying to optimize our test initialization time by avoiding running migrations when possible. A common pattern that happens is a developer will introduce a new migration, and then modify the file to iterate on the schema while working on their feature. Rails sees that the migration timestamp is present in the schema_migrations table, and won’t re-run the migration.

If ActiveRecord also stored a hash of the migration text (ie all the code that Rails sends to the database server), then it could be determined whether or not a migration has changed from when Rails initially ran the migration. This would be helpful for our cache avoidance - we’d check each migration’s presence in the version and then if the hashes match. If they match, then we have a good cache, and can avoid redoing work.

Maybe I’m missing something but Rails by default doesn’t check the schema_migrations table when maintaining the test schema.

Instead, it checks if the hash of the schema file is the same as the one that is in its internal metadata table.

Isn’t that cache already sufficient? If you changed the schema of your database Rails will load the new schema. If you did, Rails will not run any migration.

Or are you seeing a different behavior?

1 Like