umzug icon indicating copy to clipboard operation
umzug copied to clipboard

Umzug 3.1.1 new SequelizeStorage() produces circular dependency error

Open brandon-kyle-bailey opened this issue 2 years ago • 1 comments

Discussed in https://github.com/sequelize/umzug/discussions/555

Originally posted by brandon-kyle-bailey May 26, 2022 Environment info:

  • "nodejs": "14.18.1"
  • "npm": "8.1.4"
  • "sequelize": "6.20.0"
  • "umzug": "3.1.1"
  • "pg": "8.7.3"
  • "typescript": "4.0.3"

Following the documentation to setup a migration flow with Sequelize and Umzug for a Postgres database presents the following error message:

{
    "errorType": "TypeError",
    "errorMessage": "Converting circular structure to JSON\n    --> starting at object with constructor 'Sequelize5'\n    |     property 'dialect' -> object with constructor 'PostgresDialect'\n    --- property 'sequelize' closes the circle",
    "stack": [
        "TypeError: Converting circular structure to JSON",
        "    --> starting at object with constructor 'Sequelize5'",
        "    |     property 'dialect' -> object with constructor 'PostgresDialect'",
        "    --- property 'sequelize' closes the circle",
        "    at JSON.stringify (<anonymous>)",
        "    at /var/task/index.js:87561:17",
        "    at /var/task/index.js:19377:31",
        "    at /var/task/index.js:23989:7",
        "    at Array.forEach (<anonymous>)",
        "    at sendLogEventToAppender (/var/task/index.js:23988:23)",
        "    at /var/task/index.js:19245:30",
        "    at Array.forEach (<anonymous>)",
        "    at sendToListeners (/var/task/index.js:19245:15)",
        "    at Object.send (/var/task/index.js:19296:9)"
    ]
}

Minimal code example: runMigrations.ts

import { SequelizeStorage, Umzug } from "umzug";
import { Sequelize } from "sequelize";
import * as pg from "pg";

const sequelize = new Sequelize(dbname, username, password, {
    host,
    port,
    dialect: "postgres",
    dialectModule: pg,
});

const sequelizeStorage = new SequelizeStorage({ sequelize });

const umzug = new Umzug({
    ...
    storage: sequelizeStorage,
    context: { sequelize },
    logger: console,
});

Migration.ts

import DataTypes from "sequelize";

export const up = async ({ context: sequelize }: { context: any }) => {
    await sequelize.getQueryInterface().createTable("some-table-name", {
        id: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: DataTypes.BIGINT,
        },
        activityFeedId: {
            type: DataTypes.UUID,
            allowNull: false,
        },
        activityId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: {
                    tableName: "some-other-table",
                },
                key: "id",
            },
        },
    });
};
export const down = async ({ context: sequelize }: { context: any }) => {
    await sequelize.getQueryInterface().dropTable("some-table-name");
};

The error presents when calling new SequelizeStorage() passing in the configures sequelize instance, per the documentation.

Kind of stuck on this. Any suggestions? TY!

brandon-kyle-bailey avatar May 26 '22 22:05 brandon-kyle-bailey

Hmm something v similar was fixed by bumping emittery: https://github.com/sequelize/umzug/commit/7baad2419fc54e1aaf6952d851cddfc4386a6d71

could you check what emittery version is being resolved in your node_modules? Not sure how it’d end up with the wrong version but worth checking.

mmkal avatar May 27 '22 20:05 mmkal

Going to close this as can't-reproduce / no-response. @brandon-kyle-bailey I suspect this is a dependencies issue, but since the latest umzug version requires the emittery version with the fix, I don't think there's anything more umzug can do. If it's a different issue, could you open a PR with a failing test case?

mmkal avatar Aug 12 '22 18:08 mmkal