Skip to content

Use --! Included blocks on commit so uncommit can undo includes#211

Merged
benjie merged 14 commits intographile:mainfrom
astephensen:add-include-comment
Apr 22, 2026
Merged

Use --! Included blocks on commit so uncommit can undo includes#211
benjie merged 14 commits intographile:mainfrom
astephensen:add-include-comment

Conversation

@astephensen
Copy link
Copy Markdown
Contributor

@astephensen astephensen commented Apr 16, 2024

Description

This PR will wrap included files with comments, as first described in #208.

This also facilitates removing the included content when performing an uncommit.

Have opened as a draft as I'm open to any and all feedback!

Pre-Commit

  • current.sql
select 'foo';
--!include functions/hello.sql
  • fixtures/functions/hello.sql
create or replace function public.hello() returns text as $$
  select 'Hello, world!';
$$ language sql;

Committed

  • migrations/000001-world.sql
--! Previous: -
--! Hash: sha1:2d5fea0f4300a51ebf5c9d2416def4f2d22563d3
--! Message: World

select 'foo';
--! Included functions/hello.sql
create or replace function public.hello() returns text as $$
  select 'Hello, world!';
$$ language sql;
--! EndIncluded functions/hello.sql

Uncommitted

  • current.sql
--! Message: World

select 'foo';
--!include functions/hello.sql

Implementation

The method I implemented is similar to what @jcgsville described in the original issue, with a few little changes.

  • The colon was dropped between the header and the filename — I was encountering issues with --! Include: being treated as an actual header if the first statement was an include. Figured dropping the colon helps distinguish this as a file include, but I'm open to suggestions on how to fix this!
  • The filename is also added after the --! EndIncluded — This allows a simple regex back reference to be used to delete the included file (if the filename wasn't added, nested includes could be trickier to remove).

The main downside of this approach is the original formatting of the --!include is not preseved, i.e. if the include is written as --! include ... the migration will not restore the additional whitespace when uncommitting.

Notes

  • I had to run prettier across a lot of files that I didn't touch — not sure if this was missed in a previous PR or if I didn't have it configured correctly locally, but it was causing the tests to fail.

Performance impact

unknown - likely minimal.

Security impact

unknown - likely none.

Checklist

  • My code matches the project's code style and yarn lint:fix passes.
  • I've added tests for the new feature, and yarn test passes.
  • I have detailed the new feature in the relevant documentation.
  • I have added this feature to 'Pending' in the RELEASE_NOTES.md file (if one exists). - Does not exist
  • If this is a breaking change I've explained why. - Not a breaking change

@astephensen astephensen marked this pull request as draft April 16, 2024 10:10
@jcgsville
Copy link
Copy Markdown
Contributor

Thanks for taking a stab at this, @astephensen 🙂

@benjie
Copy link
Copy Markdown
Member

benjie commented May 14, 2024

I had to run prettier across a lot of files that I didn't touch — not sure if this was missed in a previous PR or if I didn't have it configured correctly locally, but it was causing the tests to fail.

I think you must have it misconfigured (older prettier version?) - use yarn && yarn lint:fix to fix formatting issues.

Copy link
Copy Markdown
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good to me. The main things I'd like to see to get this merged are:

  1. Must refuse to run/commit/process user-authored migrations that contain --! Included or --! EndIncluded comments. Save people from copy/paste issues.
  2. Extend the tests to cover multi-file migrations - want to ensure that the --! Included/--! EndIncluded interact fine with file splits. (Not anticipating any issues, but always good to have tests.) Both for commit and uncommit.

Comment thread src/commands/uncommit.ts Outdated
Comment thread src/commands/uncommit.ts Outdated
Comment thread src/migration.ts Outdated
Comment thread src/migration.ts Outdated
@astephensen
Copy link
Copy Markdown
Contributor Author

Thanks for the review @benjie, I will address those comments!

@zarybnicky
Copy link
Copy Markdown

This is also necessary for a commit rollback - if there is a consistency error and commit fails, the ABORTED AND ROLLED BACK reverts current/ files to a form with includes already expanded, same as the standalone uncommit command does.

@zarybnicky
Copy link
Copy Markdown

@astephensen Are you still interested in pushing this through? I'd take over if not

@benjie benjie marked this pull request as ready for review April 22, 2026 16:00
Copy link
Copy Markdown
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is essentially ready to merge. I've not manually tested it yet though.

@benjie benjie changed the title Wrap included files with include comment Use --! Included blocks on commit so uncommit can undo includes Apr 22, 2026
@benjie benjie merged commit 86d6202 into graphile:main Apr 22, 2026
7 checks passed
@benjie
Copy link
Copy Markdown
Member

benjie commented Apr 22, 2026

Released in graphile-migrate@2.0.0-rc.3

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants