Mocha testing PostgreSQL with Knex is giving me a MigrationLocked error
<p>I have got a local development environment working for Node/PostgreSQL/Knex in the sense that I can post to a development database on my machine using an API. I am now trying to create tests for this functionality but am getting an error.</p>

<p>Here's my config:</p>

<pre><code>//knexfile.js
module.exports = {
  development: {
    client: 'pg',
    connection: {
        host: '127.0.0.1',
        user: 'dbUser',
        password: 'dbpword',
        port: 5432,
        database: 'example-name'
    },
    migrations: {
      directory: __dirname + '/db/migrations'
    },
    seeds: {
      directory: __dirname + '/db/seeds/development'
    }
  },
}

//db.js
const config = require('../knexfile.js');
const env = process.env.NODE_ENV || 'development';
const knex = require("knex")(config[env]);

module.exports = knex;

knex.migrate.latest([config]);
</code></pre>

<p>And then my tests:</p>

<pre><code>import chai from 'chai';
import { expect } from 'chai';
import chaiHttp from 'chai-http';
import knex from '../../db/db';
import app from '../../server';

chai.use(chaiHttp);

describe('Tokens API', () =&gt; {

  beforeEach((done) =&gt; {
   knex.migrate.rollback()
   .then(() =&gt; {
     knex.migrate.latest()
     .then(() =&gt; {
       return knex.seed.run()
       .then(() =&gt; {
        done();
       });
     });
   });
});

afterEach((done) =&gt; {
  knex.migrate.rollback()
  .then(() =&gt; {
    done();
  });
});

describe('POST /users', () =&gt; {
  it('posts a list of users to the database with all mandatory fields', (done) =&gt; {
    chai.request(app)
    .post('/users')
    .send({
        "users": [
              "steve",
              "whoever",
              "matt",
              "another"]})
      .end((err, res) =&gt; {
        expect(err).to.be.null;
        expect(res).to.have.status(200);
        expect(res).to.be.json;
        done();
      });
    });
  });
});
</code></pre>

<p>When I run this I get the following error twice - I think for the knex calls in the beforeEach block:</p>

<pre><code>Knex:warning - Can't take lock to run migrations: Migration table is already locked
Knex:warning - If you are sure migrations are not running you can release the lock manually by deleting all the rows from migrations lock table: knex_migrations_lock
Unhandled rejection MigrationLocked: Migration table is already locked
</code></pre>

<p>I have tried numerous things - including clearing out the knex_migrations_lock table. The only support I can find online is <a href="https://github.com/tgriesser/knex/issues/1217" rel="nofollow">this</a> thread, which suggests clearing out the lock table using <code>DELETE FROM Migrations_lock where id &lt;&gt; 0;</code>, however my lock table only has a <code>is_locked</code> column with zero values.</p>

<p>Any idea what's going on?</p>

<p><em>EDIT:</em> I've just realised if you edit out all the knex calls, the test actually passes. Could this be because I am effectively calling knex twice - once from <code>db.js</code> and once indirectly through <code>server.js</code>? If that's the case, how do I avoid doing this - because surely I need to call the knex setup for Node to run it?</p>