Skip to content

Commit 7b5a802

Browse files
committed
Improve error message on CRLF issues in committed migrations
1 parent 5efa6fc commit 7b5a802

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/migration.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,20 @@ export function parseMigrationText(
332332
headers[key] = value ? value.trim() : value;
333333
}
334334

335+
// The `\r\n` should never exist; however Windows users may be having git convert LF to CRLF, corrupting migrations.
335336
if (strict && lines[headerLines] !== "") {
336-
throw new Error(
337-
`Invalid migration '${fullPath}': there should be two newlines after the headers section`,
338-
);
337+
if (lines[headerLines] === "\r") {
338+
throw new Error(
339+
`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:
340+
Option 1: Add \`path/to/migrations/committed/*.sql -text\` to \`.gitattributes\` in your repository
341+
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\`
342+
`,
343+
);
344+
} else {
345+
throw new Error(
346+
`Invalid migration '${fullPath}': there should be two newlines after the headers section`,
347+
);
348+
}
339349
}
340350

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

0 commit comments

Comments
 (0)