mocha mysql knex每次失败前:不能使用锁来运行迁移

tcomlyy6  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(478)

这是我的测试设置代码

  1. const knex = require('../db').knex
  2. beforeEach(() => knex.migrate.rollback()
  3. .then(() => knex.migrate.latest())
  4. .then(() => knex.seed.run())
  5. )
  6. afterEach(() => knex.migrate.rollback()
  7. .then(() => {})
  8. )

获取以下错误

  1. Knex:warning - Can't take lock to run migrations: Migration table is already locked
  2. 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
  3. Unhandled rejection MigrationLocked: Migration table is already locked
  4. 1) "before each" hook for "is not allowed"
  5. Knex:warning - Can't take lock to run migrations: Migration table is already locked
  6. 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
  7. 2) "after each" hook for "is not allowed"

这里是 db.js ```
const Knex = require('knex')
const Bookshelf = require('bookshelf')
const config = require('config')

var bookshelf = null
var knex = null

exports.init = () => {
knex = Knex(config.get('database'))
if (process.env.NODE_ENV !== 'test') {
knex.migrate.latest()
}

bookshelf = Bookshelf(knex)
bookshelf.plugin('registry')
bookshelf.plugin('pagination')
bookshelf.plugin('bookshelf-camelcase')
bookshelf.plugin('visibility')

exports.bookshelf = bookshelf
exports.knex = knex

}
`mocha.opts`
--ui bdd
--slow 70
--growl
--recursive
--reporter spec

wgx48brx

wgx48brx1#

请删除迁移锁定表中的所有行,然后重试。可能一些迁移已经崩溃,留下了锁定。
你也不需要做回调。仅仅从之前/之后返回承诺就足够了:

  1. const knex = require('../db').knex
  2. beforeEach(() => knex.migrate.rollback()
  3. .then(() => knex.migrate.latest())
  4. .then(() => knex.seed.run())
  5. )
  6. afterEach(() => knex.migrate.rollback())

编辑:
您的db init正在运行 knex.migrate.latest() 而不是等到它完成后再返回函数。

  1. exports.init = () => {
  2. knex = Knex(config.get('database'))
  3. if (process.env.NODE_ENV !== 'test') {
  4. // this starts running migrations and execution continues without waiting that this is ready
  5. knex.migrate.latest()
  6. }
  7. bookshelf = Bookshelf(knex)
  8. bookshelf.plugin('registry')
  9. bookshelf.plugin('pagination')
  10. bookshelf.plugin('bookshelf-camelcase')
  11. bookshelf.plugin('visibility')
  12. exports.bookshelf = bookshelf
  13. exports.knex = knex
  14. }
展开查看全部
cyej8jka

cyej8jka2#

结果发现 beforeEach 因为mysql,hook花了很长时间。
使用 this.timeout 帮我解决了!

  1. beforeEach(async function () {
  2. this.timeout(60 * 1000)
  3. await knex.migrate.rollback()
  4. await knex.migrate.latest()
  5. return knex.seed.run()
  6. })

相关问题