私は、APIを使用して私のマシン上の開発データベースに投稿できるという意味で、Node/PostgreSQL/Knexのローカル開発環境を持っています。私は今、この機能のテストを作成しようとしていますが、エラーが発生しています。ここでモクラのテストKnexを使用したPostgreSQLは私にMigrationLockedエラーを与えます
は私の設定です:
//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]);
そして私のテスト:
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',() => {
beforeEach((done) => {
knex.migrate.rollback()
.then(() => {
knex.migrate.latest()
.then(() => {
return knex.seed.run()
.then(() => {
done();
});
});
});
});
afterEach((done) => {
knex.migrate.rollback()
.then(() => {
done();
});
});
describe('POST /users',() => {
it('posts a list of users to the database with all mandatory fields', (done) => {
chai.request(app)
.post('/users')
.send({
"users": [
"steve",
"whoever",
"matt",
"another"]})
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.be.json;
done();
});
});
});
});
私はこれを実行すると、私は二回、次のエラーを取得する - 私はbeforeEachブロックでknexコールのためにと思う:
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
私はknex_migrations_lockテーブルのクリアを含む多くのことを試しました。私がオンラインで見つけることができる唯一のサポートは、DELETE FROM Migrations_lock where id <> 0;
を使用してロックテーブルをクリアすることを示唆するthisスレッドですが、ロックテーブルの値はゼロのis_locked
カラムしかありません。
何が起こっているのでしょうか?
編集:すべてのknex呼び出しを編集すると、テストは実際に実行されます。これは、私が効果的にknexを2回呼び出すことです.1回はdb.js
から1回、間接的にはserver.js
までです。その場合、どうすればこのことを避けることができますか?確かにNodeを実行するためにKnexセットアップを呼び出す必要があるためです。