2020-11-16 13:23:28 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
const MyVC = require('./myvc');
|
2020-11-14 01:38:56 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const ejs = require('ejs');
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
class Pull {
|
|
|
|
async run(myvc, opts) {
|
|
|
|
const conn = await myvc.dbConnect();
|
|
|
|
|
|
|
|
for (const exporter of exporters)
|
|
|
|
await exporter.init();
|
|
|
|
|
|
|
|
const exportDir = `${opts.workspace}/routines`;
|
2020-12-19 01:06:06 +00:00
|
|
|
if (!await fs.pathExists(exportDir))
|
|
|
|
await fs.mkdir(exportDir);
|
|
|
|
|
|
|
|
const schemas = await fs.readdir(exportDir);
|
|
|
|
for (const schema of schemas) {
|
|
|
|
if (opts.schemas.indexOf(schema) == -1)
|
|
|
|
await fs.remove(`${exportDir}/${schema}`, {recursive: true});
|
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
for (const schema of opts.schemas) {
|
|
|
|
let schemaDir = `${exportDir}/${schema}`;
|
|
|
|
|
|
|
|
if (!await fs.pathExists(schemaDir))
|
|
|
|
await fs.mkdir(schemaDir);
|
|
|
|
|
|
|
|
for (const exporter of exporters)
|
|
|
|
await exporter.export(conn, exportDir, schema);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 01:38:56 +00:00
|
|
|
class Exporter {
|
2020-12-02 07:35:26 +00:00
|
|
|
constructor(objectName) {
|
2020-11-14 01:38:56 +00:00
|
|
|
this.objectName = objectName;
|
|
|
|
this.dstDir = `${objectName}s`;
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
async init() {
|
|
|
|
const templateDir = `${__dirname}/exporters/${this.objectName}`;
|
|
|
|
this.query = await fs.readFile(`${templateDir}.sql`, 'utf8');
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
const templateFile = await fs.readFile(`${templateDir}.ejs`, 'utf8');
|
2020-11-14 01:38:56 +00:00
|
|
|
this.template = ejs.compile(templateFile);
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
if (await fs.pathExists(`${templateDir}.js`))
|
2020-11-14 01:38:56 +00:00
|
|
|
this.formatter = require(`${templateDir}.js`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async export(conn, exportDir, schema) {
|
2020-12-02 07:35:26 +00:00
|
|
|
const [res] = await conn.query(this.query, [schema]);
|
|
|
|
if (!res.length) return;
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
const routineDir = `${exportDir}/${schema}/${this.dstDir}`;
|
2020-12-02 07:35:26 +00:00
|
|
|
if (!await fs.pathExists(routineDir))
|
|
|
|
await fs.mkdir(routineDir);
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-19 01:06:06 +00:00
|
|
|
const routineSet = new Set();
|
|
|
|
for (const params of res)
|
|
|
|
routineSet.add(params.name);
|
|
|
|
|
|
|
|
const routines = await fs.readdir(routineDir);
|
|
|
|
for (const routineFile of routines) {
|
|
|
|
const match = routineFile.match(/^(.*)\.sql$/);
|
|
|
|
if (!match) continue;
|
|
|
|
const routine = match[1];
|
|
|
|
if (!routineSet.has(routine))
|
|
|
|
await fs.remove(`${routineDir}/${routine}.sql`);
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
for (const params of res) {
|
2020-11-14 01:38:56 +00:00
|
|
|
if (this.formatter)
|
|
|
|
this.formatter(params, schema)
|
|
|
|
|
|
|
|
params.schema = schema;
|
2020-12-19 01:06:06 +00:00
|
|
|
const sql = this.template(params);
|
|
|
|
const routineFile = `${routineDir}/${params.name}.sql`;
|
|
|
|
let changed = true;
|
|
|
|
|
|
|
|
if (await fs.pathExists(routineFile)) {
|
|
|
|
const currentSql = await fs.readFile(routineFile, 'utf8');
|
|
|
|
changed = currentSql !== sql;
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
await fs.writeFile(routineFile, sql);
|
2020-11-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
const exporters = [
|
2020-11-14 01:38:56 +00:00
|
|
|
new Exporter('function'),
|
|
|
|
new Exporter('procedure'),
|
|
|
|
new Exporter('view'),
|
|
|
|
new Exporter('trigger'),
|
|
|
|
new Exporter('event')
|
|
|
|
];
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
module.exports = Pull;
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
if (require.main === module)
|
|
|
|
new MyVC().run(Pull);
|