Skip to content

[BUG]: readMigrationFiles (drizzle-orm/migrator) Inconsistent SQL Migration Files Processing Across Different Operating Systems #4329

Open
@canisminor1990

Description

@canisminor1990

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.41.0

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

Description

When using the readMigrationFiles function from drizzle-orm/migrator, the generated SQL arrays differ between Windows and macOS/Linux environments due to different line ending conventions.

Current Behavior

The readMigrationFiles function reads SQL files and splits them at statement breakpoints. However, it preserves the native line endings:

  • On Windows: Lines end with \r\n (CRLF)
  • On macOS/Linux: Lines end with \n (LF)

This causes inconsistency in the resulting SQL arrays:

On macOS:

"sql": [
  "ALTER TABLE \"messages\" ADD COLUMN \"client_id\" text;",
  "\nALTER TABLE \"session_groups\" ADD COLUMN \"client_id\" text;",
  // ...more statements with \n line breaks
]

On Windows:

"sql": [
  "ALTER TABLE \"messages\" ADD COLUMN \"client_id\" text;",
  "\r\nALTER TABLE \"session_groups\" ADD COLUMN \"client_id\" text;",
  // ...more statements with \r\n line breaks
]

Expected Behavior

The function should produce consistent SQL arrays regardless of the operating system where it runs.

Proposed Solution

Normalize line endings when reading SQL files:

const query = fs.readFileSync(`${migrationFolderTo}/${journalEntry.tag}.sql`)
  .toString()
  .replace(/\r\n/g, '\n'); // Normalize all line endings to LF

This simple change ensures consistent behavior across all platforms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions