-
-
Couldn't load subscription status.
- Fork 62
Wrap included files with include comment #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Thanks for taking a stab at this, @astephensen 🙂 |
I think you must have it misconfigured (older prettier version?) - use |
There was a problem hiding this 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:
- Must refuse to run/commit/process user-authored migrations that contain
--! Includedor--! EndIncludedcomments. Save people from copy/paste issues. - Extend the tests to cover multi-file migrations - want to ensure that the
--! Included/--! EndIncludedinteract fine with file splits. (Not anticipating any issues, but always good to have tests.) Both for commit and uncommit.
|
|
||
| // Remove included migrations | ||
| const includeRegex = | ||
| /^--![ \t]*Included[ \t]+(?<filename>.*?\.sql)[ \t]*$.*?^--![ \t]*EndIncluded[ \t]*\k<filename>[ \t]*$/gms; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want newlines to be included in filenames, so I've removed the /s flag and been more explicit. I'm actually not sure what our validation is on include paths, whether they're allowed to contain spaces/etc? For now, I've decided a filename is anything but whitespace. I don't think we need to require here that it ends in .sql?
| /^--![ \t]*Included[ \t]+(?<filename>.*?\.sql)[ \t]*$.*?^--![ \t]*EndIncluded[ \t]*\k<filename>[ \t]*$/gms; | |
| /^--![ \t]*Included[ \t]+(?<filename>\S+)[ \t]*$[\s\S]*?^--![ \t]*EndIncluded[ \t]*\k<filename>[ \t]*$/gm; |
| const decompiledBody = body.replace(includeRegex, (match) => { | ||
| return match.split("\n")[0].replace(" Included", "include"); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather use the capture group:
| const decompiledBody = body.replace(includeRegex, (match) => { | |
| return match.split("\n")[0].replace(" Included", "include"); | |
| }); | |
| const decompiledBody = body.replace( | |
| includeRegex, | |
| (_, filename) => `--! include ${filename}`, | |
| ); |
| processedFiles: ReadonlySet<string>, | ||
| ): Promise<string> { | ||
| const regex = /^--![ \t]*include[ \t]+(.*\.sql)[ \t]*$/gm; | ||
| const regex = /^--![ \t]*[iI]nclude[ \t]+(.*\.sql)[ \t]*$/gm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? Suggest we revert this:
| const regex = /^--![ \t]*[iI]nclude[ \t]+(.*\.sql)[ \t]*$/gm; | |
| const regex = /^--![ \t]*include[ \t]+(.*\.sql)[ \t]*$/gm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't mean to leave this change in. I was toying around with the idea of writing it as --! Include ... before I concluded that amount of flexibility isn't worth it, particularly when uncommitting will clobber the formatting anyway.
| const sqlPath = sqlPathByRawSqlPath[rawSqlPath]; | ||
| const content = contentBySqlPath[sqlPath]; | ||
| return content; | ||
| const included = match.replace(/^--![ \t]*include/, "--! Included"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make the change above to allow Include as well as include, this will break. I don't think we want the change on line 135. I feel like we should check for no match, but meh.
|
Thanks for the review @benjie, I will address those comments! |
|
This is also necessary for a commit rollback - if there is a consistency error and |
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.sqlfixtures/functions/hello.sqlCommitted
migrations/000001-world.sqlUncommitted
current.sqlImplementation
The method I implemented is similar to what @jcgsville described in the original issue, with a few little changes.
--! 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!--! 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
--!includeis not preseved, i.e. if the include is written as--! include ...the migration will not restore the additional whitespace when uncommitting.Notes
Performance impact
unknown - likely minimal.
Security impact
unknown - likely none.
Checklist
yarn lint:fixpasses.yarn testpasses.I have added this feature to 'Pending' in the- Does not existRELEASE_NOTES.mdfile (if one exists).If this is a breaking change I've explained why.- Not a breaking change