#4036 Command fixtures: Code unified

This commit is contained in:
Juan Ferrer 2022-06-13 14:12:53 +02:00
parent 7ed98621ce
commit 0ec37b136a
6 changed files with 72 additions and 105 deletions

View File

@ -2,7 +2,6 @@
const MyVC = require('./myvc');
const fs = require('fs-extra');
const path = require('path');
const docker = require('./docker');
class Dump {
get usage() {
@ -21,29 +20,9 @@ class Dump {
}
async run(myvc, opts) {
const iniPath = path.join(opts.subdir || '', 'remotes', opts.iniFile);
const dumpDir = `${opts.myvcDir}/dump`;
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',
file: path.join(__dirname, 'server', 'Dockerfile.client')
}, opts.debug);
const dumpStream = await myvc.initDump('.dump.sql');
let dumpArgs = [
`--defaults-file=${iniPath}`,
'--default-character-set=utf8',
'--no-data',
'--comments',
@ -53,43 +32,21 @@ class Dump {
'--databases'
];
dumpArgs = dumpArgs.concat(opts.schemas);
await this.dockerRun('myvc-dump.sh', dumpArgs, execOptions);
const fixturesArgs = [
`--defaults-file=${iniPath}`,
'--no-create-info',
'--skip-triggers',
'--insert-ignore'
];
for (const schema in opts.fixtures) {
const escapedSchema = '`'+ schema.replace('`', '``') +'`';
await dumpStream.write(
`USE ${escapedSchema};\n`,
'utf8'
);
const args = fixturesArgs.concat([schema], opts.fixtures[schema]);
await this.dockerRun('mysqldump', args, execOptions);
}
await myvc.runDump('myvc-dump.sh', dumpArgs, dumpStream);
await myvc.dumpFixtures(dumpStream, opts.fixtures);
await dumpStream.end();
await myvc.dbConnect();
const version = await myvc.fetchDbVersion();
if (version) {
const dumpDir = path.join(opts.myvcDir, 'dump');
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`,
rm: true
}, execOptions);
}
}
module.exports = Dump;

View File

@ -1,8 +1,5 @@
const MyVC = require('./myvc');
const fs = require('fs-extra');
const path = require('path');
const docker = require('./docker');
class Fixtures {
get usage() {
@ -15,61 +12,16 @@ class Fixtures {
get localOpts() {
return {
default: {
remote: 'local'
remote: 'docker'
}
};
}
async run(myvc, opts) {
const iniPath = path.join(opts.subdir || '', 'remotes', opts.iniFile);
const dumpDir = `${opts.myvcDir}/dump`;
if (!await fs.pathExists(dumpDir))
await fs.mkdir(dumpDir);
const dumpFile = `${dumpDir}/fixtures.sql`;
const dumpStream = await fs.createWriteStream(dumpFile);
const execOptions = {
stdio: [
process.stdin,
dumpStream,
process.stderr
]
};
await docker.build(__dirname, {
tag: 'myvc/client',
file: path.join(__dirname, 'server', 'Dockerfile.client')
}, opts.debug);
const fixturesArgs = [
`--defaults-file=${iniPath}`,
'--no-create-info',
'--skip-triggers',
'--insert-ignore'
];
for (const schema in opts.localFixtures) {
const escapedSchema = '`'+ schema.replace('`', '``') +'`';
await dumpStream.write(
`USE ${escapedSchema};\n`,
'utf8'
);
const args = fixturesArgs.concat([schema], opts.localFixtures[schema]);
await this.dockerRun('mysqldump', args, execOptions);
}
const dumpStream = await myvc.initDump('fixtures.sql');
await myvc.dumpFixtures(dumpStream, opts.localFixtures);
await dumpStream.end();
}
async dockerRun(command, args, execOptions) {
const commandArgs = [command].concat(args);
await docker.run('myvc/client', commandArgs, {
addHost: 'host.docker.internal:host-gateway',
volume: `${this.opts.myvcDir}:/workspace`,
rm: true
}, execOptions);
}
}
module.exports = Fixtures;

65
myvc.js
View File

@ -9,8 +9,8 @@ const ini = require('ini');
const path = require('path');
const mysql = require('mysql2/promise');
const nodegit = require('nodegit');
const { networkInterfaces } = require('os');
const camelToSnake = require('./lib').camelToSnake;
const docker = require('./docker');
class MyVC {
async run(command) {
@ -403,8 +403,8 @@ class MyVC {
}
async cachedChanges() {
const dumpDir = `${this.opts.myvcDir}/dump`;
const dumpChanges = `${dumpDir}/.changes`;
const dumpDir = path.join(this.opts.myvcDir, 'dump');
const dumpChanges = path.join(dumpDir, '.changes');
if (!await fs.pathExists(dumpChanges))
return null;
@ -420,12 +420,69 @@ class MyVC {
for await (const line of rl) {
changes.push({
mark: line.charAt(0),
path: line.substr(1)
path: line.substring(1)
});
}
return changes;
}
async initDump(dumpFile) {
const dumpDir = path.join(this.opts.myvcDir, 'dump');
if (!await fs.pathExists(dumpDir))
await fs.mkdir(dumpDir);
const dumpPath = path.join(dumpDir, dumpFile);
// FIXME: If it's called after docker.build() statement it creates an
// "invalid" WriteStream
const dumpStream = await fs.createWriteStream(dumpPath);
await docker.build(__dirname, {
tag: 'myvc/client',
file: path.join(__dirname, 'server', 'Dockerfile.client')
}, this.opts.debug);
return dumpStream;
}
async dumpFixtures(dumpStream, tables) {
const fixturesArgs = [
'--no-create-info',
'--skip-triggers',
'--insert-ignore'
];
for (const schema in tables) {
const escapedSchema = '`'+ schema.replace('`', '``') +'`';
await dumpStream.write(
`USE ${escapedSchema};\n`,
'utf8'
);
const args = fixturesArgs.concat([schema], tables[schema]);
await this.runDump('mysqldump', args, dumpStream);
}
}
async runDump(command, args, dumpStream) {
const iniPath = path.join(this.opts.subdir || '', 'remotes', this.opts.iniFile);
const myArgs = [
`--defaults-file=${iniPath}`
];
const execOptions = {
stdio: [
process.stdin,
dumpStream,
process.stderr
]
};
const commandArgs = [command].concat(myArgs, args);
await docker.run('myvc/client', commandArgs, {
addHost: 'host.docker.internal:host-gateway',
volume: `${this.opts.myvcDir}:/workspace`,
rm: true
}, execOptions);
}
showHelp(opts, usage, command) {
const prefix = `${'Usage:'.gray} [npx] myvc`;

View File

@ -1,6 +1,6 @@
{
"name": "myvc",
"version": "1.4.12",
"version": "1.4.13",
"author": "Verdnatura Levante SL",
"description": "MySQL Version Control",
"license": "GPL-3.0",

View File

@ -26,6 +26,7 @@ COPY \
myvc.js \
myvc-push.js \
lib.js \
docker.js \
myvc.default.yml \
db.ini \
./

View File

@ -8,6 +8,6 @@
"type": "git"
},
"dependencies": {
"myvc": "^1.4.12"
"myvc": "^1.4.13"
}
}