myt/myvc-dump.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-12-02 07:35:26 +00:00
2020-12-04 16:30:26 +00:00
const MyVC = require('./myvc');
2020-12-02 07:35:26 +00:00
const fs = require('fs-extra');
const path = require('path');
class Dump {
get usage() {
return {
description: 'Dumps structure and fixtures from remote',
operand: 'remote'
};
}
get localOpts() {
2020-12-02 07:35:26 +00:00
return {
default: {
2020-12-04 16:30:26 +00:00
remote: 'production'
2020-12-02 07:35:26 +00:00
}
};
}
async run(myvc, opts) {
2022-06-13 12:12:53 +00:00
const dumpStream = await myvc.initDump('.dump.sql');
2020-12-02 07:35:26 +00:00
2022-07-12 07:45:05 +00:00
console.log('Dumping structure.');
2020-12-02 07:35:26 +00:00
let dumpArgs = [
'--default-character-set=utf8',
'--no-data',
'--comments',
'--triggers',
'--routines',
'--events',
'--databases'
];
dumpArgs = dumpArgs.concat(opts.schemas);
2022-06-13 12:12:53 +00:00
await myvc.runDump('myvc-dump.sh', dumpArgs, dumpStream);
2020-12-02 07:35:26 +00:00
2022-07-12 07:45:05 +00:00
console.log('Dumping fixtures.');
2022-06-13 12:12:53 +00:00
await myvc.dumpFixtures(dumpStream, opts.fixtures);
2022-07-12 07:45:05 +00:00
console.log('Dumping privileges.');
const privs = opts.privileges;
if (privs && Array.isArray(privs.tables)) {
let args = [
'--no-create-info',
'--skip-triggers',
'--insert-ignore',
'--complete-insert'
];
if (privs.where) args.push('--where', privs.where);
args = args.concat(['mysql'], privs.tables);
await dumpStream.write('USE `mysql`;\n', 'utf8');
await myvc.runDump('myvc-dump.sh', args, dumpStream);
}
2020-12-02 07:35:26 +00:00
await dumpStream.end();
2022-07-12 07:45:05 +00:00
console.log('Saving version.');
2022-06-13 12:12:53 +00:00
await myvc.dbConnect();
2020-12-02 07:35:26 +00:00
const version = await myvc.fetchDbVersion();
2022-05-08 20:44:40 +00:00
if (version) {
2022-06-13 12:12:53 +00:00
const dumpDir = path.join(opts.myvcDir, 'dump');
2020-12-02 07:35:26 +00:00
await fs.writeFile(
`${dumpDir}/.dump.json`,
JSON.stringify(version)
);
}
}
}
module.exports = Dump;
if (require.main === module)
new MyVC().run(Dump);