#4036 Export local fixtures

This commit is contained in:
Juan Ferrer 2022-06-09 20:41:18 +02:00
parent a0ebfcf3f5
commit 8b03a86155
8 changed files with 99 additions and 6 deletions

View File

@ -49,6 +49,7 @@ Database versioning commands:
Local server management commands: Local server management commands:
* **dump**: Export database structure and fixtures. * **dump**: Export database structure and fixtures.
* **fixtures**: Export local database fixtures.
* **run**: Build and start local database server container. * **run**: Build and start local database server container.
* **start**: Start local database server container. * **start**: Start local database server container.
@ -212,6 +213,15 @@ in *dump* folder. If no remote is specified *production* is used.
$ myvc dump [<remote>] $ myvc dump [<remote>]
``` ```
### fixtures
Exports local database fixtures into *dump/fixtures.sql* files. If no remote is
specified *local* is used.
```text
$ myvc fixtures [<remote>]
```
### run ### run
Builds and starts local database server container. It only rebuilds the image Builds and starts local database server container. It only rebuilds the image

View File

@ -79,8 +79,10 @@ const docker = {
const child = spawn('docker', execArgs, execOptions || undefined); const child = spawn('docker', execArgs, execOptions || undefined);
child.on('exit', code => { child.on('exit', code => {
if (code !== 0) { if (code !== 0) {
const args = JSON.stringify(execArgs); const quotedArgs = execArgs
reject(new Error(`docker: ${args}: exit code ${code}`)); .map(x => /\s/g.test(x) ? `"${x}"` : x)
.join(' ');
reject(new Error(`Docker exit code ${code}: 'docker ${quotedArgs}'`));
} else } else
resolve(code); resolve(code);
}); });

View File

@ -21,7 +21,6 @@ class Dump {
} }
async run(myvc, opts) { async run(myvc, opts) {
const conn = await myvc.dbConnect();
const iniPath = path.join(opts.subdir || '', 'remotes', opts.iniFile); const iniPath = path.join(opts.subdir || '', 'remotes', opts.iniFile);
const dumpDir = `${opts.myvcDir}/dump`; const dumpDir = `${opts.myvcDir}/dump`;
@ -63,8 +62,9 @@ class Dump {
'--insert-ignore' '--insert-ignore'
]; ];
for (const schema in opts.fixtures) { for (const schema in opts.fixtures) {
const escapedSchema = '`'+ schema.replace('`', '``') +'`';
await dumpStream.write( await dumpStream.write(
`USE ${conn.escapeId(schema, true)};\n`, `USE ${escapedSchema};\n`,
'utf8' 'utf8'
); );

79
myvc-fixtures.js Normal file
View File

@ -0,0 +1,79 @@
const MyVC = require('./myvc');
const fs = require('fs-extra');
const path = require('path');
const docker = require('./docker');
class Fixtures {
get usage() {
return {
description: 'Dumps local fixtures from database',
operand: 'remote'
};
}
get localOpts() {
return {
default: {
remote: 'local'
}
};
}
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);
}
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;
if (require.main === module)
new MyVC().run(Fixtures);

View File

@ -68,6 +68,7 @@ class MyVC {
'version', 'version',
'clean', 'clean',
'dump', 'dump',
'fixtures',
'start', 'start',
'run' 'run'
]; ];

View File

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

1
template/.gitignore vendored
View File

@ -2,3 +2,4 @@
node_modules node_modules
remotes/*.ini remotes/*.ini
!remotes/local.ini !remotes/local.ini
dump/.changes

View File

@ -8,6 +8,6 @@
"type": "git" "type": "git"
}, },
"dependencies": { "dependencies": {
"myvc": "^1.4.10" "myvc": "^1.4.11"
} }
} }