KoaJS 2 之 migration with knex

安装knexfile

1
npm install -g knex

然后在项目的根目录

1
knex init

将会产生knexfile.js,内容类似如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Update with your config settings.
module.exports = {
development: {
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'root',
password: '',
database: '<YOUR TEST DB NAME>',
charset: 'utf8'
}
},
staging: {
...
},
production: {
...
}
};

如果想要根据具体环境来执行具体配置,可以使用如下命令来指定环境

1
knex migrate:latest --env production

更多的使用可以参考Knex docs

创建Migration

1
knex migrate:make create_person

将会创建migrations目录并且将migration的文件放入文件夹中

默认的内容如下:

1
2
3
4
5
6
7
exports.up = function(knex, Promise){

}

exports.down = function(knex, Promise){

}

接下来我们在这里面实现具体的表信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
exports.up = function(knex, Promise){
return Promise.all([
knex.schema.createTable('person', function(table){
table.increments('id').primary();
table.integer('parentId').unsigned().references('id').inTable('person');
table.string('firstName');
table.string('fullName');
table.integer('age');
table.json('address');
})
]);
}

exports.down = function(knex, Promise){
return Promise.all([
knex.schema.dropTable('person')
]);
}

当应用migration的时候up被调用,当执行回滚的时候down被调用

在这些功能中,你可以使用Knex Schema functions

执行如下命令来使用新的migration

1
knex migrate:latest

更新数据库表

1
knex migrate:make update_person

内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.table('person', function(table){
table.string('twitter');
})
])
}

exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.table('person', function(table){
table.dropColumn('twitter');
})
]);
}

回滚操作

回滚操作执行下面的命令

1
knex migrate:rollback

knex对于创建跟修改表变得很容易。