Skip to content
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

Improve error message on CRLF issues in committed migrations #226

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading