Skip to content

fix(sequelize): hard delete in unlogMigration so paranoid models can rerun migrations - #727

Open
0z5a wants to merge 1 commit into
sequelize:mainfrom
0z5a:fix/sequelize-storage-paranoid
Open

0z5a wants to merge 1 commit into
sequelize:mainfrom
0z5a:fix/sequelize-storage-paranoid

Conversation

@0z5a

@0z5a 0z5a commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #706

I started from #706 and checked it against the code. SequelizeStorage.getModel() defines the meta model with sequelize.define(...), so it picks up the Sequelize instance's define defaults. When those include paranoid: true and the storage has timestamps: true, as in the report, the meta model is paranoid. unlogMigration then calls model.destroy({where: ...}), which only sets deletedAt. executed() no longer lists the migration, so umzug treats it as pending, but its row is still in the table. Running it up again inserts the same name, and the primary key on the name column rejects it with SequelizeUniqueConstraintError: Validation error.

The change passes force: true to destroy in unlogMigration, so the row is always removed. For a model that isn't paranoid, force changes nothing. It also covers a paranoid model passed in through the model option.

Rows that were soft-deleted before this change stay in the table, so a migration that was already reverted this way still needs its row removed by hand before it can run again.

How I found it: I followed unlogMigration and executed in src/storage/sequelize.ts and checked which options getModel() passes to define. It sets timestamps but not paranoid, so the instance default decides it.

Where this can show up in practice: projects that turn on paranoid for all their models through the Sequelize define option and enable timestamps on the umzug storage. Every migration they revert then can't be run again, which breaks the usual edit, revert and rerun loop while writing a migration, and also running up again after a rollback in a deployment. This is my estimate beyond the report in #706.

Testing:

  • New test in test/storage/sequelize.test.ts: with define: {paranoid: true} and timestamps: true, it logs a migration, unlogs it, checks that no row is left (counting soft-deleted rows too), logs it again and checks executed(). Without the change it fails on the row count, and without that assertion it fails with SequelizeUniqueConstraintError: Validation error, as in the issue.
  • pnpm test (107 tests) and pnpm lint pass on macOS arm64 with Node 24.14.1 and SQLite.
  • Not run: other dialects (Postgres, MySQL), and the CLI with a real project.

…rerun migrations

With paranoid set through the Sequelize define defaults, destroy only set deletedAt, so a reverted migration could not be logged again because of the primary key on its name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling up, down and up again with paranoid sequelize storage gives uniqness error for Migrations table

1 participant