sequelize / umzug

Framework agnostic migration tool for Node.js
MIT License
1.98k stars 158 forks source link

Migrations not applying on Windows - "applied 0 migrations" message #676

Open anikghosh256 opened 1 week ago

anikghosh256 commented 1 week ago

Environment

Description

I'm encountering an issue on Windows where running migrations with Umzug results in the message "applied 0 migrations." without actually applying any migrations. The same setup works as expected on macOS and Ubuntu.

Steps to Reproduce

  1. Set up Umzug with SequelizeStorage and given code.
  2. Run migrations on Windows.

Expected Behavior

Migrations should be applied, and the database should be updated accordingly.

Actual Behavior

The output is { event: 'up', message: 'applied 0 migrations.' } and no migrations are applied to the database.

Code Snippet

import { Umzug, SequelizeStorage } from "umzug";
import { Sequelize, DataTypes } from "sequelize";
import * as path from "path";
import "dotenv/config";

const sequelize = new Sequelize({
  dialect: "postgres",
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  logging: process.env.SEQUELIZE_LOG === "true",
});

export const migrator = new Umzug({
  migrations: {
    glob: [
      "migrations/*.{js,cjs,mjs}",
      { cwd: path.dirname(import.meta.url.replace("file://", "")) },
    ],
  },
  context: { sequelize, DataTypes },
  storage: new SequelizeStorage({
    sequelize,
  }),
  logger: console,
});

migrator.runAsCLI();

Additional Context

My package.json has "type": "module". This issue occurs only on Windows; the migrations work as expected on macOS and Ubuntu.

mmkal commented 1 week ago

This will be tricky to debug because I don't have a windows machine. I suspect it's something to do with fast-glob not finding files. Try finding the compiled version of this line in your node_modules and adding some logging around it:

const paths = await glob(globString, {...globOptions, ignore, absolute: true})

If that's returning an empty array, there's your problem. You can then play around with the glob options to see what needs changing.

anikghosh256 commented 1 week ago

Thanks for the suggestion, @mmkal. Indeed, it returns an empty array on Windows. The issue seems to stem from how path.dirname(import.meta.url.replace("file://", "")) behaves on Windows, yielding a path like /D:/dev/. When I manually specify the cwd option, I encounter the following error:

failed: Original error: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'

This leads me to believe that the package might need to incorporate OS-specific handling to address this discrepancy.