-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Wrap included files with include comment #211
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -42,9 +42,16 @@ export async function _uncommit(parsedSettings: ParsedSettings): Promise<void> { | |||||||||||||||
const contents = await fsp.readFile(lastMigrationFilepath, "utf8"); | ||||||||||||||||
const { headers, body } = parseMigrationText(lastMigrationFilepath, contents); | ||||||||||||||||
|
||||||||||||||||
// Remove included migrations | ||||||||||||||||
const includeRegex = | ||||||||||||||||
/^--![ \t]*Included[ \t]+(?<filename>.*?\.sql)[ \t]*$.*?^--![ \t]*EndIncluded[ \t]*\k<filename>[ \t]*$/gms; | ||||||||||||||||
const decompiledBody = body.replace(includeRegex, (match) => { | ||||||||||||||||
return match.split("\n")[0].replace(" Included", "include"); | ||||||||||||||||
}); | ||||||||||||||||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather use the capture group:
Suggested change
|
||||||||||||||||
|
||||||||||||||||
// Drop Hash, Previous and AllowInvalidHash from headers; then write out | ||||||||||||||||
const { Hash, Previous, AllowInvalidHash, ...otherHeaders } = headers; | ||||||||||||||||
const completeBody = serializeMigration(body, otherHeaders); | ||||||||||||||||
const completeBody = serializeMigration(decompiledBody, otherHeaders); | ||||||||||||||||
await writeCurrentMigration(parsedSettings, currentLocation, completeBody); | ||||||||||||||||
|
||||||||||||||||
// Delete the migration from committed and from the DB | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -132,7 +132,7 @@ export async function compileIncludes( | |||||
content: string, | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Why? Suggest we revert this:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
||||||
// Find all includes in this `content` | ||||||
const matches = [...content.matchAll(regex)]; | ||||||
|
@@ -205,10 +205,12 @@ export async function compileIncludes( | |||||
// Simple string replacement for each path matched | ||||||
const compiledContent = content.replace( | ||||||
regex, | ||||||
(_match, rawSqlPath: string) => { | ||||||
(match, rawSqlPath: string) => { | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. If you make the change above to allow |
||||||
const endIncluded = included.replace("Included", "EndIncluded"); | ||||||
return `${included}\n${content.trim()}\n${endIncluded}`; | ||||||
}, | ||||||
); | ||||||
|
||||||
|
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
?