Skip to content

Comments

Reverse the order of INSERT statements in structure.sql dumps#44363

Merged
byroot merged 1 commit intorails:mainfrom
ghiculescu:reverse-structure-versions
Feb 8, 2022
Merged

Reverse the order of INSERT statements in structure.sql dumps#44363
byroot merged 1 commit intorails:mainfrom
ghiculescu:reverse-structure-versions

Conversation

@ghiculescu
Copy link
Member

@ghiculescu ghiculescu commented Feb 8, 2022

Summary

Today every new migration added adds a new line here, but also requires moving the semicolon from what was previously the most recent line:

INSERT INTO "schema_migrations" (version) VALUES
('20210924183850'),
('20211008165610'),
('20211108235701');
INSERT INTO "schema_migrations" (version) VALUES
('20190924183850'),
('20191008165610'),
+('20191108235701'),
-('20191108235701');
+('20191118181905');

This leads to merge conflicts if lots of migrations are being added at the same.

How this PR came about

The fix in this PR was suggested as a better fix by @matthewd in #43414.

We've been running with a monkey patch for it for about a month, and can confirm, it's definitely an improvement. So I'm making this as an alternative suggestion to #43414

Adding @mlarraz as a co-author - thanks for the original inspiration.
For reference #3477 added the original sorting logic.

@ghiculescu ghiculescu force-pushed the reverse-structure-versions branch 2 times, most recently from f1e5aae to a027a9a Compare February 8, 2022 19:37
@ghiculescu

This comment was marked as resolved.

@byroot
Copy link
Member

byroot commented Feb 8, 2022

Can you rebase? You have a conflict on the CHANGELOG. Other than that I'm 💯 on this PR.

@ghiculescu ghiculescu force-pushed the reverse-structure-versions branch from a027a9a to b050b00 Compare February 8, 2022 20:28
This was suggested as a better fix in rails#43414. The goal is to decrease merge conflicts that often come at the bottom due to the semi colon placement.

We've been running with a monkey patch for it for about a month, and can confirm, it's definitely an improvement. So I'm making this as an alternative suggestion to rails#43414

Adding @mlarraz as a co-author - thanks for the original inspiration.

Co-authored-by: @mlarraz <[email protected]>
@ghiculescu ghiculescu force-pushed the reverse-structure-versions branch from b050b00 to 7c8ac6c Compare February 8, 2022 21:55
@ghiculescu
Copy link
Member Author

Alright, I finally got it green.

@byroot byroot merged commit 4d4497b into rails:main Feb 8, 2022
@ghiculescu ghiculescu deleted the reverse-structure-versions branch February 8, 2022 22:31
@ans-ioki
Copy link

ans-ioki commented Dec 14, 2023

In every major rails project we will now scroll first down to the end of the structure file and then a few pages up to see the latest inserted versions. Sorry to say - i know everyone is opinionated about everything - but while i also do not like leading commas that would have been the better option for this. Printing the order inverse is imho cumbersome. If i tail the file i want to see the newest stuff last.

@gdonald
Copy link

gdonald commented Feb 15, 2024

Is there a way to override this jarring new default?

@ghiculescu
Copy link
Member Author

You could monkey patch this:

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter
      def insert_versions_sql(versions)
        if versions.is_a?(Array)
          super(versions.reverse)
        else
          super
        end
      end
    end
  end
end

@gdonald
Copy link

gdonald commented Feb 16, 2024

You could monkey patch this:

Or you could revert, and then YOU could monkey patch.

@BenSandeen
Copy link

I also dislike this change. Maybe it's better in niche situations, but for everyday use, it's worse. It's also incredibly bad for upgrading Rails, when there's already so much changing, and then we also have to see hundreds of lines of diffs in the DB files.

In my opinion, this is almost a complete negative. But, I suppose this does stick to Rails's and Ruby's conventions of inconveniencing developers and making things harder if it saves 1 or 2 characters :/

@zinefer
Copy link

zinefer commented Jan 23, 2026

In every major rails project we will now scroll first down to the end of the structure file and then a few pages up to see the latest inserted versions. Sorry to say - i know everyone is opinionated about everything - but while i also do not like leading commas that would have been the better option for this. Printing the order inverse is imho cumbersome. If i tail the file i want to see the newest stuff last.

A project I am working on has 1833 migrations. It took 61 page-ups and will only increase. 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants