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`;
|
|
|
|
if (await fs.pathExists(exportDir))
|
|
|
|
await fs.remove(exportDir, {recursive: true});
|
|
|
|
await fs.mkdir(exportDir);
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
let sql = this.template(params);
|
2020-12-02 07:35:26 +00:00
|
|
|
await fs.writeFile(`${routineDir}/${params.name}.sql`, 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);
|