update.js
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const path = require('path');
const shelljs = require('shelljs');
const chalk = require('chalk');
const _ = require('lodash');
const fs = require('fs');
const config = require('../../config');
const argv = require('optimist').argv
require('yoho-node-lib').global(config);
const logger = global.yoho.logger;
const {SqlHelper} = require('../../utils');
const migrations = require('../../migrations');
const migrationPath = path.join(__dirname, '../../migrations');
const migrationTablName = '__migrations';
const scriptPath = path.join(__dirname, '../../scripts');
const env_script = process.env.npm_config_script || argv.script;
const env_verbose = process.env.npm_config_verbose || argv.verbose;
const mysqlCli = new SqlHelper();
let script_file = '';
if (env_script) {
script_file = process.env.npm_config_file || argv.file;
if (!script_file) {
console.error('请输入保存的文件名');
return;
}
}
const initMargration = () => {
return mysqlCli.execute(`create database if not exists ${config.mysql.database};`).then(dbRes => {
if (dbRes.warningCount === 0) {
console.log(chalk.green(`数据库创建成功:${config.mysql.database}`));
}
return mysqlCli.changeDatabase(config.mysql.database).then(() => {
return mysqlCli.execute(`create table if not exists ${migrationTablName} (
id int(10) unsigned not null auto_increment,
migration_name varchar(50) not null,
migration_time timestamp not null DEFAULT CURRENT_TIMESTAMP,
primary key (id)
) DEFAULT CHARSET=utf8;`).then(tbRes => {
if (tbRes.warningCount === 0) {
console.log(chalk.green(`迁移记录表创建成功:${migrationTablName}`));
}
});
});
});
};
const removeComment = (scripts) => {
const regComment1 = /\/\*(\n|.)*?\*\//g;
const regComment2 = /#.*?(\n|$)/g;
const regComment3 = /--.*?(\n|$)/g;
return scripts
.replace(regComment1, '')
.replace(regComment2, '')
.replace(regComment3, '');
};
const getSql = (migras) => {
let scripts = '';
let migrationsSqls = [];
_.each(migras, m => {
scripts += removeComment(fs.readFileSync(`${migrationPath}/${m}.sql`, 'utf8'));
});
migrationsSqls = _.filter(_.split(scripts, 'GO;'), script => _.trim(script));
return migrationsSqls;
}
const getMigrationLogSql = (migras) => {
return _.map(migras, m => {
return `insert ${migrationTablName} (migration_name) values ('${m}');\r\n`;
});
}
const script = (migras, sqls) => {
let migrationsSqls = getSql(migras);
let insertSqls = getMigrationLogSql(migras);
let scripts = _.join(_.concat(migrationsSqls, insertSqls), '');
shelljs.exec(`mkdir -p ${scriptPath}`); // 创建页面目录
fs.writeFileSync(path.join(scriptPath, `${script_file}.sql`), scripts, {
encoding: 'utf8'
});
console.log(chalk.green(`导出文件:${path.join(scriptPath, `${script_file}.sql`)}成功`))
return Promise.resolve();
};
const sqlLog = (sql) => {
env_verbose && console.log(chalk.gray(sql));
}
const update = (migras) => {
let migrationsSqls = getSql(migras);
console.log(chalk.gray(`--verbose:`))
return mysqlCli.transaction(migrationsSqls, sql => sqlLog(sql)).then(() => {
console.log(chalk.green(`脚本写入成功,准备写入迁移记录`))
let insertSqls = getMigrationLogSql(migras);
return mysqlCli.transaction(insertSqls, sql => sqlLog(sql)).then(() => {
console.log(chalk.green(`迁移完成!`))
});
}, () => {
return Promise.reject();
});
}
const migration = (migras) => {
if (migras.length) {
console.log(chalk.green(`共计:${migras.length}条迁移记录!`));
return env_script ? script(migras) : update(migras)
}
console.log(chalk.yellow('无更新记录'));
return Promise.resolve();
};
const round = (times) => {
return Math.round((times[0] * 1e9 + times[1]) / 10000) / 100;
}
let timeStart = process.hrtime();
initMargration().then(() => {
let sqlIn = _.join(_.map(migrations, m => {
return `'${m}'`;
}), ',');
mysqlCli
.query(`select * from ${migrationTablName} where migration_name in (${sqlIn})`)
.then(result => {
let migras = _.filter(migrations,
m => _.every(result,
r => m.indexOf(r.migration_name) < 0));
migration(migras).then(() => {
let timeEnd = process.hrtime(timeStart);
console.log(chalk.yellow(`运行时间:${round(timeEnd)}ms`));
process.exit();
}, () => {
console.log(chalk.red('迁移失败,脚本已回滚!'));
let timeEnd = process.hrtime(timeStart);
console.log(chalk.yellow(`运行时间:${round(timeEnd)}ms`));
process.exit();
});
});
});