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');
|
2021-10-14 08:34:40 +00:00
|
|
|
const nodegit = require('nodegit');
|
2022-02-02 04:05:31 +00:00
|
|
|
const ExporterEngine = require('./lib').ExporterEngine;
|
2020-12-02 07:35:26 +00:00
|
|
|
class Pull {
|
2021-10-25 13:38:07 +00:00
|
|
|
get usage() {
|
|
|
|
return {
|
|
|
|
description: 'Incorporate database routine changes into workspace',
|
|
|
|
params: {
|
|
|
|
force: 'Do it even if there are local changes',
|
2022-02-07 10:46:13 +00:00
|
|
|
checkout: 'Move to same database commit before pull',
|
2022-05-02 10:03:07 +00:00
|
|
|
update: 'Update all routines',
|
2022-04-30 00:33:41 +00:00
|
|
|
sums: 'Save SHA sums of all objects'
|
2021-10-25 13:38:07 +00:00
|
|
|
},
|
|
|
|
operand: 'remote'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
get localOpts() {
|
2021-10-22 14:11:03 +00:00
|
|
|
return {
|
2022-02-07 10:46:13 +00:00
|
|
|
alias: {
|
2021-10-22 14:11:03 +00:00
|
|
|
force: 'f',
|
2022-02-07 10:46:13 +00:00
|
|
|
checkout: 'c',
|
2022-05-02 10:03:07 +00:00
|
|
|
update: 'u',
|
2022-04-30 00:33:41 +00:00
|
|
|
sums: 's'
|
2022-02-07 10:46:13 +00:00
|
|
|
},
|
|
|
|
boolean: [
|
|
|
|
'force',
|
|
|
|
'checkout',
|
2022-05-02 10:03:07 +00:00
|
|
|
'update',
|
2022-04-30 00:33:41 +00:00
|
|
|
'sums'
|
2022-02-07 10:46:13 +00:00
|
|
|
]
|
2021-10-22 14:11:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
async run(myvc, opts) {
|
|
|
|
const conn = await myvc.dbConnect();
|
2021-10-22 14:11:03 +00:00
|
|
|
const repo = await myvc.openRepo();
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const unstagedDiff = await myvc.getUnstaged(repo);
|
|
|
|
|
|
|
|
if (await hasChanges(unstagedDiff))
|
|
|
|
throw new Error('You have unstaged changes, save them before pull');
|
|
|
|
|
|
|
|
// Check for staged changes
|
|
|
|
|
|
|
|
const stagedDiff = await myvc.getStaged(repo);
|
|
|
|
|
|
|
|
if (await hasChanges(stagedDiff))
|
|
|
|
throw new Error('You have staged changes, save them before pull');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checkout to remote commit
|
|
|
|
|
|
|
|
if (opts.checkout) {
|
|
|
|
const version = await myvc.fetchDbVersion();
|
|
|
|
|
|
|
|
if (version && version.gitCommit) {
|
|
|
|
const now = parseInt(new Date().toJSON());
|
|
|
|
const branchName = `myvc-pull_${now}`;
|
|
|
|
console.log(`Creating branch '${branchName}' from database commit.`);
|
|
|
|
const commit = await repo.getCommit(version.gitCommit);
|
|
|
|
const branch = await nodegit.Branch.create(repo,
|
|
|
|
`myvc-pull_${now}`, commit, () => {});
|
|
|
|
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.`);
|
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
const engine = new ExporterEngine(conn, opts.myvcDir);
|
|
|
|
await engine.init();
|
|
|
|
const shaSums = engine.shaSums;
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
const exportDir = `${opts.myvcDir}/routines`;
|
2020-12-19 01:06:06 +00:00
|
|
|
if (!await fs.pathExists(exportDir))
|
|
|
|
await fs.mkdir(exportDir);
|
|
|
|
|
2021-10-23 13:20:35 +00:00
|
|
|
// Delete old schemas
|
|
|
|
|
2020-12-19 01:06:06 +00:00
|
|
|
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
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
for (const schema in shaSums) {
|
|
|
|
if (!await fs.pathExists(`${exportDir}/${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) {
|
2021-10-23 13:20:35 +00:00
|
|
|
let schemaDir = `${exportDir}/${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)
|
|
|
|
await exporter.export(exportDir,
|
2022-04-30 00:33:41 +00:00
|
|
|
schema, opts.update, opts.sums);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|