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

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

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


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