myt/myt-dump.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-12-02 07:35:26 +00:00
2022-12-21 13:17:50 +00:00
const Myt = require('./myt');
const Command = require('./lib/command');
2020-12-02 07:35:26 +00:00
const fs = require('fs-extra');
class Dump extends Command {
static usage = {
description: 'Dumps structure and fixtures from remote',
operand: 'remote'
};
static localOpts = {
default: {
remote: 'production'
}
};
2020-12-02 07:35:26 +00:00
2022-12-21 13:17:50 +00:00
async run(myt, opts) {
const dumpStream = await myt.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-12-21 13:17:50 +00:00
await myt.runDump('docker-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-12-21 13:17:50 +00:00
await myt.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');
2022-12-21 13:17:50 +00:00
await myt.runDump('mysqldump', args, dumpStream);
2022-07-12 07:45:05 +00:00
}
2020-12-02 07:35:26 +00:00
await dumpStream.end();
2022-07-12 07:45:05 +00:00
console.log('Saving version.');
2022-12-21 13:17:50 +00:00
await myt.dbConnect();
const version = await myt.fetchDbVersion();
2022-05-08 20:44:40 +00:00
if (version) {
2020-12-02 07:35:26 +00:00
await fs.writeFile(
`${opts.dumpDir}/.dump.json`,
2020-12-02 07:35:26 +00:00
JSON.stringify(version)
);
}
}
}
module.exports = Dump;
if (require.main === module)
2022-12-21 13:17:50 +00:00
new Myt().run(Dump);
2020-12-02 07:35:26 +00:00