node.js - knex.js - 动态模式名称迁移失败
问题描述
环境
- Knex 版本:knex@0.16.3
- 数据库+版本:MySQL 5.7.24
- 操作系统:Linux (Manjaro)
漏洞
我正在尝试使用ava
. 众所周知,ava
为每个测试文件生成不同的节点进程。
为了使测试隔离,我需要重新创建我的数据库并在所有测试文件中运行迁移。我开始引导并得到以下脚本:
import test from 'ava';
import cuid from 'cuid';
import knexFactory from 'knex';
import knexFile from './knexfile';
const knex = knexFactory(knexFile.development);
test.before(async t => {
const schemaName = `foo_${cuid()}`;
await knex.raw(`CREATE SCHEMA ${schemaName}`);
try {
await knex.migrate.latest({ schemaName });;
} catch (err) {
}
Object.assign(t.context, { schemaName });
});
test.after.always(async t => {
const { schemaName } = t.context;
await knex.raw(`DROP SCHEMA ${schemaName}`);
});
test('foo', () => {})
但是,我遇到了以下错误:
迁移文件“20190205110315_create_users_table.js”失败
迁移失败并出现错误:创建表users
(id
int unsigned not null auto_increment 主键,password
文本) - 表“用户”已经存在
发生的情况是.migrate.latest()
调用忽略了它schemaName
的参数,并且它针对文件中定义的配置运行knexfile.js
,即实际数据库。
迁移文件非常简单:
exports.up = function(knex, Promise) {
return knex.schema.createTable('users', table => {
table.increments();
table.string('email');
table.text('password');
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('users');
};
解决方案
推荐阅读
- php - How would get the value of a div's ID then send it to my PHP file using ajax?
- c# - 如何仅使用时间和星期几来使用 TryParseExact
- regex - 任何顺序的正则表达式可选捕获组
- java - 如何为 Flux 提供从过去某个时间开始的实时时间序列数据?
- cucumber - Gitlab runner 工作因黄瓜测试失败而停滞不前
- python - 字典理解多种方式
- xamarin - 错误 XF001:已多次导入 Xamarin.Forms 目标。请检查您的项目文件并删除重复的导入
- c - 如何在没有授权/离线的情况下使用 MIP SDK 提取文档 AIP 标签?
- php - 给定变量而不是文字时,checkdate 无法正常工作
- tensorflow - 用于可视化潜在空间图像的 TensorFlow 嵌入投影仪不起作用?