2022-12-21 13:17:50 +00:00
|
|
|
const Myt = require('./myt');
|
2022-12-21 12:34:17 +00:00
|
|
|
const Command = require('./lib/command');
|
2020-11-14 01:38:56 +00:00
|
|
|
const fs = require('fs-extra');
|
2021-10-14 08:34:40 +00:00
|
|
|
const nodegit = require('nodegit');
|
2022-12-21 12:34:17 +00:00
|
|
|
const ExporterEngine = require('./lib/exporter-engine');
|
2022-12-29 09:15:02 +00:00
|
|
|
const repoExt = require('./lib/repo');
|
2022-12-21 13:17:50 +00:00
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
class Pull extends Command {
|
|
|
|
static usage = {
|
|
|
|
description: 'Incorporate database routine changes into workspace',
|
|
|
|
params: {
|
|
|
|
force: 'Do it even if there are local changes',
|
|
|
|
checkout: 'Move to same database commit before pull',
|
|
|
|
update: 'Update all routines',
|
|
|
|
sums: 'Save SHA sums of all objects'
|
|
|
|
},
|
|
|
|
operand: 'remote'
|
|
|
|
};
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
static opts = {
|
2022-12-21 12:34:17 +00:00
|
|
|
alias: {
|
|
|
|
force: 'f',
|
|
|
|
checkout: 'c',
|
|
|
|
update: 'u',
|
|
|
|
sums: 's'
|
|
|
|
},
|
|
|
|
boolean: [
|
|
|
|
'force',
|
|
|
|
'checkout',
|
|
|
|
'update',
|
|
|
|
'sums'
|
|
|
|
]
|
|
|
|
};
|
2021-10-22 14:11:03 +00:00
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
async run(myt, opts) {
|
|
|
|
const conn = await myt.dbConnect();
|
|
|
|
const repo = await myt.openRepo();
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
if (!opts.force) {
|
|
|
|
async function hasChanges(diff) {
|
|
|
|
if (diff)
|
|
|
|
for (const patch of await diff.patches()) {
|
|
|
|
const match = patch
|
|
|
|
.newFile()
|
|
|
|
.path()
|
|
|
|
.match(/^routines\/(.+)\.sql$/);
|
|
|
|
if (match) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for unstaged changes
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
const unstagedDiff = await repoExt.getUnstaged(repo);
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
if (await hasChanges(unstagedDiff))
|
|
|
|
throw new Error('You have unstaged changes, save them before pull');
|
|
|
|
|
|
|
|
// Check for staged changes
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
const stagedDiff = await repoExt.getStaged(repo);
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
if (await hasChanges(stagedDiff))
|
|
|
|
throw new Error('You have staged changes, save them before pull');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checkout to remote commit
|
|
|
|
|
|
|
|
if (opts.checkout) {
|
2022-12-21 13:17:50 +00:00
|
|
|
const version = await myt.fetchDbVersion();
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
if (version && version.gitCommit) {
|
|
|
|
const now = parseInt(new Date().toJSON());
|
2022-12-21 13:17:50 +00:00
|
|
|
const branchName = `myt-pull_${now}`;
|
2021-10-22 14:11:03 +00:00
|
|
|
console.log(`Creating branch '${branchName}' from database commit.`);
|
|
|
|
const commit = await repo.getCommit(version.gitCommit);
|
|
|
|
const branch = await nodegit.Branch.create(repo,
|
2022-12-21 13:17:50 +00:00
|
|
|
`myt-pull_${now}`, commit, () => {});
|
2021-10-22 14:11:03 +00:00
|
|
|
await repo.checkoutBranch(branch);
|
|
|
|
}
|
2021-10-14 08:34:40 +00:00
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2021-10-22 14:11:03 +00:00
|
|
|
// Export routines to SQL files
|
|
|
|
|
|
|
|
console.log(`Incorporating routine changes.`);
|
|
|
|
|
2023-01-25 17:16:43 +00:00
|
|
|
const engine = new ExporterEngine(conn, opts);
|
2022-02-02 04:05:31 +00:00
|
|
|
await engine.init();
|
|
|
|
const shaSums = engine.shaSums;
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
const routinesDir = opts.routinesDir;
|
|
|
|
if (!await fs.pathExists(routinesDir))
|
|
|
|
await fs.mkdir(routinesDir);
|
2020-12-19 01:06:06 +00:00
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
// Delete old schemas
|
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
const schemas = await fs.readdir(routinesDir);
|
2020-12-19 01:06:06 +00:00
|
|
|
for (const schema of schemas) {
|
|
|
|
if (opts.schemas.indexOf(schema) == -1)
|
2022-12-21 12:34:17 +00:00
|
|
|
await fs.remove(`${routinesDir}/${schema}`, {recursive: true});
|
2020-12-19 01:06:06 +00:00
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
for (const schema in shaSums) {
|
2022-12-21 12:34:17 +00:00
|
|
|
if (!await fs.pathExists(`${routinesDir}/${schema}`))
|
2022-04-30 00:06:56 +00:00
|
|
|
engine.deleteSchemaSums(schema);
|
2022-02-02 04:05:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
// Export objects to SQL files
|
2021-10-22 14:11:03 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
for (const schema of opts.schemas) {
|
2022-12-21 12:34:17 +00:00
|
|
|
let schemaDir = `${routinesDir}/${schema}`;
|
2020-12-02 07:35:26 +00:00
|
|
|
if (!await fs.pathExists(schemaDir))
|
|
|
|
await fs.mkdir(schemaDir);
|
2022-04-30 00:06:56 +00:00
|
|
|
|
|
|
|
for (const exporter of engine.exporters)
|
2022-12-29 13:27:16 +00:00
|
|
|
await this.export(conn, engine, exporter, schema);
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
2021-10-22 14:11:03 +00:00
|
|
|
|
2022-04-30 00:06:56 +00:00
|
|
|
await engine.refreshPullDate();
|
|
|
|
await engine.saveInfo();
|
2020-11-14 01:38:56 +00:00
|
|
|
}
|
2022-12-29 13:27:16 +00:00
|
|
|
|
|
|
|
async export(conn, engine, exporter, schema) {
|
|
|
|
const {opts} = this;
|
|
|
|
|
|
|
|
const res = await exporter.query(conn, schema);
|
2022-12-29 16:58:40 +00:00
|
|
|
if (!res.length) return;
|
2022-12-29 13:27:16 +00:00
|
|
|
|
2022-12-29 16:58:40 +00:00
|
|
|
const type = exporter.objectType;
|
|
|
|
|
|
|
|
const routineDir = `${opts.routinesDir}/${schema}/${type}s`;
|
2022-12-29 13:27:16 +00:00
|
|
|
if (!await fs.pathExists(routineDir))
|
|
|
|
await fs.mkdir(routineDir);
|
|
|
|
|
|
|
|
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];
|
2022-12-29 16:58:40 +00:00
|
|
|
if (!routineSet.has(routine)) {
|
2022-12-29 13:27:16 +00:00
|
|
|
await fs.remove(`${routineDir}/${routine}.sql`);
|
2022-12-29 16:58:40 +00:00
|
|
|
engine.deleteShaSum(type, schema, routine)
|
|
|
|
}
|
2022-12-29 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const params of res) {
|
|
|
|
const routineName = params.name;
|
|
|
|
const sql = exporter.format(params);
|
|
|
|
const routineFile = `${routineDir}/${routineName}.sql`;
|
|
|
|
let update = opts.update;
|
|
|
|
|
2022-12-29 16:58:40 +00:00
|
|
|
const oldSum = engine.getShaSum(type, schema, routineName);
|
|
|
|
if (oldSum || opts.sums || (opts.sumViews && type === 'view')) {
|
2022-12-29 13:27:16 +00:00
|
|
|
const shaSum = engine.shaSum(sql);
|
|
|
|
if (oldSum !== shaSum) {
|
2022-12-29 16:58:40 +00:00
|
|
|
engine.setShaSum(type, schema, routineName, shaSum);
|
2022-12-29 13:27:16 +00:00
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
} else if (params.modified && engine.lastPull) {
|
|
|
|
if (params.modified > engine.lastPull)
|
|
|
|
update = true;
|
|
|
|
} else if (await fs.pathExists(routineFile)) {
|
|
|
|
const currentSql = await fs.readFile(routineFile, 'utf8');
|
|
|
|
if (sql != currentSql)
|
|
|
|
update = true;
|
|
|
|
} else
|
|
|
|
update = true;
|
|
|
|
|
|
|
|
if (update)
|
|
|
|
await fs.writeFile(routineFile, sql);
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-12-21 13:17:50 +00:00
|
|
|
new Myt().run(Pull);
|