Skip to content

Commit

Permalink
Improve error message on CRLF issues in committed migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Sep 19, 2024
1 parent 5efa6fc commit 7b5a802
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,20 @@ export function parseMigrationText(
headers[key] = value ? value.trim() : value;
}

// The `\r\n` should never exist; however Windows users may be having git convert LF to CRLF, corrupting migrations.
if (strict && lines[headerLines] !== "") {
throw new Error(
`Invalid migration '${fullPath}': there should be two newlines after the headers section`,
);
if (lines[headerLines] === "\r") {
throw new Error(
`Invalid migration '${fullPath}': it looks like the line endings have been corrupted - perhaps you have configured git to replace LF with CRLF? Here's a couple potential solutions:
Option 1: Add \`path/to/migrations/committed/*.sql -text\` to \`.gitattributes\` in your repository
Option 2: Globally reconfigure git to convert CRLF to LF on commit, but never convert LF back to CRLF: \`git config --global core.autocrlf input\`
`,
);
} else {
throw new Error(
`Invalid migration '${fullPath}': there should be two newlines after the headers section`,
);
}
}

const body = lines.slice(headerLines).join("\n").trim() + "\n";
Expand Down

0 comments on commit 7b5a802

Please sign in to comment.