From 0ec37b136a3788950d7c015bc00338d180b54bd9 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 13 Jun 2022 14:12:53 +0200 Subject: [PATCH] #4036 Command fixtures: Code unified --- myvc-dump.js | 53 ++++------------------------------- myvc-fixtures.js | 54 ++--------------------------------- myvc.js | 65 ++++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- server/Dockerfile | 1 + template/package.json | 2 +- 6 files changed, 72 insertions(+), 105 deletions(-) diff --git a/myvc-dump.js b/myvc-dump.js index aa594c7..4ccecfe 100644 --- a/myvc-dump.js +++ b/myvc-dump.js @@ -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; diff --git a/myvc-fixtures.js b/myvc-fixtures.js index 372b90d..9913c87 100644 --- a/myvc-fixtures.js +++ b/myvc-fixtures.js @@ -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; diff --git a/myvc.js b/myvc.js index 8f4f0f2..9abcea5 100755 --- a/myvc.js +++ b/myvc.js @@ -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`; diff --git a/package.json b/package.json index 5f617b9..57c596b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/Dockerfile b/server/Dockerfile index 43444a6..8848342 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -26,6 +26,7 @@ COPY \ myvc.js \ myvc-push.js \ lib.js \ + docker.js \ myvc.default.yml \ db.ini \ ./ diff --git a/template/package.json b/template/package.json index f6f5a2f..003335a 100644 --- a/template/package.json +++ b/template/package.json @@ -8,6 +8,6 @@ "type": "git" }, "dependencies": { - "myvc": "^1.4.12" + "myvc": "^1.4.13" } }