Loading the schema by running all migrations

I’ve run into situations before where I’ve had to argue with other humans about loading the schema. Some will run db:schema:load, some will run db:migrate from scratch. The latter which makes my hair go full “coding horror”.

It was my understanding that schema-load was the official supported recommended™ way, however, seeing how much debate there seems to be, it might not be all that clear.

Are we able to get some more clarity about that? I see that it’s included in the migrations guide, but I’m wondering why some people are so attached to running all migrations. Any throughts?

1 Like

If you’re not careful with Git merges, it’s easy to commit the wrong (older) timestamp into schema.rb. You’d then get the PendingMigrationError of death right after running db:schema:load. Or sometimes things work fine in your development environment but break on test.

Also, I didn’t appreciate the importance of schema.rb early on when I was a solo dev. That changed after some team experience with people rebasing migrations that they already pushed.

1 Like

In the case of a large group I work with, we must use migrations, because our test and local development and CI all run SQLite, and our production runs Oracle. The pain of installing Oracle locally, and the complexity of replicating our virtual private database-driven security layer on local dev Macs, are what keep us from using the same database everywhere. Happily, using this combination keeps us from writing too much PL-SQL and other true coding horrors. :slight_smile:



Yes, that’s been a pain point for me as well! Also, I’ve had some cases where I’ve re-ordered migrations for different reasons (eg. Git branching or developing different migrations in parallel), and the “latest version includes all previous version” model breaks down.

(Fortunately, experience taught me that I could renumber migrations and play with table schema_migrations at will – as long as there’s one source of truth – but this is really non-trivial and took some time to know.)

I feel like we could have Rails do more to help developers take the recommended approach. What do you think about having Rails load the schema automatically when db:migrate is called on a fresh database (one without a schema_migrations table) before running any additional migrations?

In the past, I’ve just added raise "Use db:schema:load instead" to the first migration to prevent developers from migrating from scratch. Also, for safety, it might be best to only do this in development and test environments.