Rails 7.1 "INSERT INTO "schema_migrations" (version) VALUES" sort order inside db/structure.sql

My project is set up to use db/structure.sql to track database migrations. Prior to Rails 7.1, the order of the rows under INSERT INTO "schema_migrations" (version) VALUES was in date ascending order. Snippet from db/structure.sql is below.

INSERT INTO "schema_migrations" (version) VALUES
 ('20210713133021'),
 ('20210715181117'),
 ('20210716191530'),
 ('20210722210702'),
 ('20210730001905'),
 ('20210804143957'),

With Rails 7.1, this order is now in date descending order. Snippet from db/structure.sql is below.

INSERT INTO "schema_migrations" (version) VALUES
('20231108194548'),
('20231103150202'),
('20231101160730'),
('20231101123237'),
('20231031154044'),

Did the sort order change inside Rails 7.1. If so, why?

1 Like

It shouldn’t matter.

The data in a table has no intrinsic order. Rows in a table are an unordered set. Order only matters when retrieving or modifying that data, and that order is defined on a query-by-query basis.

(We sometimes think of a table as having the order of its primary key, in this case the version column, but this is an operational detail and a mental shortcut, not a feature of the table itself.)

In this case, there is no theoretical difference which order the rows appear in this INSERT query. They could be forwards, backwards, or random; all that matters is that the set of values being inserted is correct. When they’re retrieved with a SELECT, the ORDER BY clause used in that query will be the only thing determining the order (and if there isn’t one, the order is undefined).

1 Like

It changed in Reverse the order of `INSERT` statements in `structure.sql` dumps by ghiculescu · Pull Request #44363 · rails/rails · GitHub

3 Likes

Thanks for the PR link Alex.

So apparently its to address the two line diff from adding a new migration, because of the semicolon… and instead it’s a single line change. I guess that’s better

1 Like

The difference is convenience, not technical.

Now you can’t just tail the file or jump to the bottom to look for/at a migration conflict.

I’d rather endure changing a few characters while being forced to interact w/ a migration conflict–simple sanity check, too.

Reverse-chronological doesn’t make sense to me here, and it seems a pretty arbitrary tossing of a Rails convention mostly since Rails :man_shrugging: