Removing Migrations With Knex.js In My Node.js Application

wuno picture wuno · Nov 11, 2016 · Viewed 15.3k times · Source

I am trying to get knex working in my node.js application. I was following a tutorial and at some point created a table but could not repeat the process. I removed the table and deleted all the migrations folders. At tis point I started over but after creating a new migration and then running knex migrate:latest I get an error saying the migration directory is corrupt because the original migration I had is missing.

I was under the impression that if the file is missing it should not know it was ever there.

What is the proper way to remove a migration from my project?

knexfile.js

 development: {
    client: 'pg',
    connection: {
    host: '127.0.0.1',
    user:     'postgres',
    password: 'password',
    database: 'myDatabase'
    },
     pool: {
      min: 10,
      max: 20
    },
    migrations: {
      directory: __dirname + '/db/migrations'
    },
    seeds: {
      directory: __dirname + '/db/seeds/development'
    }

db.js

var config      = require('../knexfile.js');  
var env         = 'development';  
var knex        = require('knex')(config[env]);

module.exports = knex;
console.log('Getting knex');
knex.migrate.latest([config]); 
console.log('Applying migration...');

Running this gives error,

knex migrate:latest

Using environment: development
Error: The migration directory is corrupt, the following files are missing: 20161110130954_auth_level.js

but this migration does not exist because I deleted it.

Answer

marman picture marman · Nov 29, 2016

You had to rollback a migration (knex migrate:rollback) before deleting the file, I guess what you can do is:

touch [full_path_to_migrations_here]/migrations/20161110130954_auth_level.js

knex migrate:rollback

rm [full_path_to_migrations_here]/migrations/20161110130954_auth_level.js

Reference here https://github.com/tgriesser/knex/issues/1569