myt/myvc-dump.js

100 lines
2.6 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');
2020-12-04 10:53:11 +00:00
const docker = require('./docker');
2020-12-02 07:35:26 +00:00
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) {
const conn = await myvc.dbConnect();
2022-03-17 09:03:23 +00:00
const iniPath = path.join(opts.subdir || '', 'remotes', opts.iniFile);
2020-12-02 07:35:26 +00:00
const dumpDir = `${opts.myvcDir}/dump`;
2020-12-02 07:35:26 +00:00
if (!await fs.pathExists(dumpDir))
await fs.mkdir(dumpDir);
const dumpFile = `${dumpDir}/.dump.sql`;
const dumpStream = await fs.createWriteStream(dumpFile);
const execOptions = {
stdio: [
process.stdin,
dumpStream,
process.stderr
]
};
await docker.build(__dirname, {
tag: 'myvc/client',
2020-12-05 21:50:45 +00:00
file: path.join(__dirname, 'server', 'Dockerfile.client')
2020-12-04 09:15:29 +00:00
}, opts.debug);
2020-12-02 07:35:26 +00:00
let dumpArgs = [
2022-03-17 09:03:23 +00:00
`--defaults-file=${iniPath}`,
2020-12-02 07:35:26 +00:00
'--default-character-set=utf8',
'--no-data',
'--comments',
'--triggers',
'--routines',
'--events',
'--databases'
];
dumpArgs = dumpArgs.concat(opts.schemas);
await this.dockerRun('myvc-dump.sh', dumpArgs, execOptions);
const fixturesArgs = [
2022-03-17 09:03:23 +00:00
`--defaults-file=${iniPath}`,
2020-12-02 07:35:26 +00:00
'--no-create-info',
'--skip-triggers',
'--insert-ignore'
];
for (const schema in opts.fixtures) {
await dumpStream.write(
`USE ${conn.escapeId(schema, true)};\n`,
'utf8'
);
const args = fixturesArgs.concat([schema], opts.fixtures[schema]);
await this.dockerRun('mysqldump', args, execOptions);
}
await dumpStream.end();
const version = await myvc.fetchDbVersion();
if (version){
await fs.writeFile(
`${dumpDir}/.dump.json`,
JSON.stringify(version)
);
}
}
async dockerRun(command, args, execOptions) {
const commandArgs = [command].concat(args);
await docker.run('myvc/client', commandArgs, {
volume: `${this.opts.myvcDir}:/workspace`,
2020-12-04 16:30:26 +00:00
rm: true
2020-12-02 07:35:26 +00:00
}, execOptions);
}
}
module.exports = Dump;
if (require.main === module)
new MyVC().run(Dump);