Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex: Rollback specific migration?

Tags:

I'd like to be able to rollback a specific knex migration on the command line.

For example:

knex migrate:rollback('20161104101325')

Is this possible?

like image 900
steel Avatar asked Nov 04 '16 16:11

steel


People also ask

How do I rollback migration in KNEX?

Rolling back the migration means running the code in the down method in the migration file, which destroys the whole fruits table and all the data in it. We could then adjust the line where the weight column is created by adding the call to notNullable() . We could finally run the migration again.

How do I rollback specific migrations?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements. where n is number of migrations to rollback, counting from latest migration. where xxxxx is the version number of the migration.


2 Answers

From the doc:

To undo the specified migration that was run

$ knex migrate:down 001_migration_name.js

To run the specified migration that has not yet been run

$ knex migrate:up 001_migration_name.js

To list both completed and pending migrations:

$ knex migrate:list

(to list and check)

(migrate:list was released in 0.19.3! if not available update knex (npm i -g knex))

like image 110
Mohamed Allal Avatar answered Sep 28 '22 07:09

Mohamed Allal


Update: This answer is now out of date - the functionality was since implemented in knex as stated by mcating comment and by Mohamed in this answer

At this point of time this is not possible - you can find the discussion of this feature in this GitHub ticket: #666

You can use knex-migrate which is a migration toolkit for knex and supports the --only flag for rollbacks.

like image 37
kyrisu Avatar answered Sep 28 '22 08:09

kyrisu