Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a migration using sequalize-cli

I manually deleted a migration file name 20171125081136-create-task.js.

After deleting the migration file, I ran this command

db:migrate:undo:all

While running this command I'm getting an error in the terminal: ERROR: Unable to find migration: 20171125081136-create-task.js.

Due to this error I'm stuck and not able to undo other migration files that exists.

like image 842
Rahul Dagli Avatar asked Nov 19 '18 10:11

Rahul Dagli


People also ask

How do I delete a migration in Sequelize?

Undoing Migrations​ With migration you can revert to old state by just running a command. You can use db:migrate:undo , this command will revert the most recent migration. You can revert back to the initial state by undoing all migrations with the db:migrate:undo:all command.

How do I delete a migration?

Click the Migrations tab to display a table containing the available migrations. For the migration to delete, click the trash icon, delete, on the right side of the table, and then select Delete migration.

How do I delete a row in Sequelize?

To delete rows of data from your SQL table using Sequelize, you need to use the provided destroy() method. The destroy() method can be called from any Model or instance of your Model to delete rows from your table.


2 Answers

In your case, you must add the deleted migration file back in because Sequelize requires it to roll back your migrations. If you don't have it, you can add a blank migration file titled 20171125081136-create-task.js. The file must have a down function that returns a successful promise.

'use strict';

module.exports = {
  up: function(queryInterface, Sequelize) {
    return Promise.resolve()
  },

  down: function(queryInterface) {
    return Promise.resolve()
  }
};

Going forward, if you want to delete a migration:

  1. Undo the latest migration: node_modules/.bin/sequelize db:migrate:undo
  2. Delete the latest migration file
like image 112
mcranston18 Avatar answered Sep 24 '22 16:09

mcranston18


I was getting the same issue an this is how I solved it:

Sequelize stores the migration history within a separate table, ex "SequelizeMeta". If you delete a migration file and no longer want to use it after, you can remove the migration rows corresponding to your migration file from the SequelizeMeta table.

Hope that helps!

like image 21
Come.T Avatar answered Sep 25 '22 16:09

Come.T