-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
MigrationError: Migration 2024.06.20T07.13.41.random_file_name.ts (up) failed: Original error: migration.up is not a function #693
Comments
Same issue here. I believe this issue has been introduced after we moved from CJS (module: commonjs) to NodeNext (module: NodeNext). Does anybody have a solution for this? |
Worth trying going from If that doesn't work - if you can create a reproduction on stackblitz, I can take a look. |
@mmkal Trying I tried this package aorund for the first time, but stumbled upon this issue for half of a day. See my simple example: app/migrations/20250228.migration.js import { DataTypes } from '@sequelize/core';
export default {
async up({ context: queryInterface }) {
await queryInterface.createTable('Users', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
balance: {
type: DataTypes.FLOAT,
defaultValue: 0,
allowNull: false,
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
});
},
async down({ context }) {
await context.dropTable('Users');
},
}; app/migrations/umzug.js import { Sequelize } from 'sequelize';
import { Umzug, SequelizeStorage } from 'umzug';
import connection from '../config/connection.js';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const { database, username, password, host, port, dialect } = connection.development;
const sequelize = new Sequelize(
database,
username,
password,
{
host,
port,
dialect,
}
);
const umzug = new Umzug({
migrations: {
glob: 'app/migrations/*.migration.js',
resolve: async ({ name, path, context }) => {
const migration = await import(resolve(__dirname, path));
return {
name,
up: async () => await migration.up({ context }),
down: async () => await migration.down({ context }),
};
},
},
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: console,
});
export default umzug; app/migrations/runMigrations.js import umzup from './umzug.js';
(async () => {
try {
console.log('Started migrations');
await umzug.up();
console.log('Migrated successefuly');
return;
} catch (error) {
console.error('Error when tried to mirate:', error);
}
})(); And finally the error: { event: 'migrating', name: undefined }
Error when tried to mirate: MigrationError: Migration undefined (up) failed: Original error: m.up is not a function
at /app/node_modules/umzug/lib/umzug.js:170:27
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Umzug.runCommand (/app/node_modules/umzug/lib/umzug.js:126:20)
at async file:///app/app/migrations/runMigrations.js:6:5 {
cause: TypeError: m.up is not a function Even though when i logged the I don't know yet where and why... |
I see you are mixing the usage of sequelize v6 and v7. You need to either use |
@WikiRik I use import Sequelize from 'sequelize';
export default {
async up({ context: queryInterface }) {
await queryInterface.createTable('Users', {
id: {
type: Sequelize.INTEGER,
... And again, even with const umzug = new Umzug({
migrations: {
glob: 'app/migrations/*.migration.js',
resolve: async ({ name, path, context }) => {
// const migration = await import(resolve(__dirname, path));
const migration = require(path).default;
console.log(migration) // Ouputs: { up: [AsyncFunction: up], down: [AsyncFunction: down] }
return {
name,
up: () => migration.up({ context }),
down: () => migration.down({ context }),
};
},
},
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: console,
});
// export default umzug;
module.exports = umzug; gives me the same error: UPD P.S. |
@WikiRik if we do in https://github.com/sequelize/umzug/blob/main/src/umzug.ts#L259 instead of just this: await m.up(params) Something like this: const esmMigrationWithAsyncResolver = await m;
if (esmMigrationWithAsyncResolver) {
esmMigrationWithAsyncResolver.up(params);
} else {
await m.up(params);
} And we change places with synchronous access to the const params: MigrationParams<Ctx> = {name: m.name, path: m.path, context} to const params: MigrationParams<Ctx> = {name: m.name || (await m).name, path: m.path, context}
then it all works fine and set of migrations completes well. So i assume, to fix this issue you need to work properly with the |
To get around this I manually read and transformed my migrations files like so: const migrations = fs.readdirSync(migrationsDir).map(name => {
const { default: migration } = require(`./migrations/${name}`);
return {
up: async (params: MigrationParams<QueryInterface>) => await migration.up(params.context, sequelize),
down: async (params: MigrationParams<QueryInterface>) => await migration.down(params.context, sequelize),
name,
};
});
const umzug = new Umzug({
migrations,
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: console,
}); Not ideal, but it worked! |
"mysql2": "^3.10.0",
"sequelize": "^6.37.3",
"umzug": "^3.8.1"
node version v20.10.0
We have migrations written in typescript like
which gets compiled to .js files and is run through nodejs lambda
The error is happening randomly without any concurrent pattern seen.
FYI: we have few files like random_file.ts.ts in between as tech debt but couldnt reproduce with them as well
also this is how i am initializing umzug
The text was updated successfully, but these errors were encountered: