From 021e3cb0836bbf92ae443f79c12c4f30992c5b35 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Thu, 29 Dec 2022 17:58:40 +0100 Subject: [PATCH] refs #4036 sumViews option, sum & template fixes --- assets/myt.default.yml | 1 + myt-dump.js | 10 ---------- myt-init.js | 18 +++++++++++++++--- myt-pull.js | 17 ++++++++++------- myt-push.js | 2 +- myt.js | 1 + package-lock.json | 4 ++-- package.json | 2 +- template/dump/Dockerfile.base | 1 + template/dump/afterDump.sql | 1 + template/dump/beforeDump.sql | 1 + template/dump/structure.sql | 1 - template/package.json | 2 +- 13 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 template/dump/Dockerfile.base create mode 100644 template/dump/afterDump.sql create mode 100644 template/dump/beforeDump.sql delete mode 100644 template/dump/structure.sql diff --git a/assets/myt.default.yml b/assets/myt.default.yml index f02ea72..687ba47 100755 --- a/assets/myt.default.yml +++ b/assets/myt.default.yml @@ -1,6 +1,7 @@ versionSchema: myt versionDigits: 5 maxOldVersions: 30 +sumViews: true schemas: - myt fixtures: diff --git a/myt-dump.js b/myt-dump.js index 288532a..2224a24 100644 --- a/myt-dump.js +++ b/myt-dump.js @@ -52,16 +52,6 @@ class Dump extends Command { } await dumper.end(); - - console.log('Saving version.'); - await myt.dbConnect(); - const version = await myt.fetchDbVersion(); - if (version) { - await fs.writeFile( - `${opts.dumpDir}/.dump.json`, - JSON.stringify(version) - ); - } } } diff --git a/myt-init.js b/myt-init.js index 8a06075..0487330 100755 --- a/myt-init.js +++ b/myt-init.js @@ -1,6 +1,7 @@ const Myt = require('./myt'); const Command = require('./lib/command'); const fs = require('fs-extra'); +const path = require('path'); class Init extends Command { static usage = { @@ -8,12 +9,23 @@ class Init extends Command { }; async run(myt, opts) { - const templateDir = `${__dirname}/template`; + const packageFile = path.join(opts.mytDir, 'package.json'); + const packageExists = await fs.pathExists(packageFile); + + const templateDir = path.join(__dirname, 'template'); const templates = await fs.readdir(templateDir); for (let template of templates) { - const dst = `${opts.mytDir}/${template}`; + const dst = path.join(opts.mytDir, template); if (!await fs.pathExists(dst)) - await fs.copy(`${templateDir}/${template}`, dst); + await fs.copy(path.join(templateDir, template), dst); + } + + if (!packageExists) { + const packageJson = require(packageFile); + packageJson.dependencies[myt.packageJson.name] = + `^${myt.packageJson.version}`; + await fs.writeFile(packageFile, + JSON.stringify(packageJson, null, ' ')); } } } diff --git a/myt-pull.js b/myt-pull.js index b2ef549..5606f77 100755 --- a/myt-pull.js +++ b/myt-pull.js @@ -125,9 +125,11 @@ class Pull extends Command { const {opts} = this; const res = await exporter.query(conn, schema); - if (!res.length) return; + if (!res.length) return; - const routineDir = `${opts.routinesDir}/${schema}/${exporter.objectType}s`; + const type = exporter.objectType; + + const routineDir = `${opts.routinesDir}/${schema}/${type}s`; if (!await fs.pathExists(routineDir)) await fs.mkdir(routineDir); @@ -140,8 +142,10 @@ class Pull extends Command { const match = routineFile.match(/^(.*)\.sql$/); if (!match) continue; const routine = match[1]; - if (!routineSet.has(routine)) + if (!routineSet.has(routine)) { await fs.remove(`${routineDir}/${routine}.sql`); + engine.deleteShaSum(type, schema, routine) + } } for (const params of res) { @@ -150,12 +154,11 @@ class Pull extends Command { const routineFile = `${routineDir}/${routineName}.sql`; let update = opts.update; - const oldSum = engine.getShaSum(routineName); - if (oldSum || opts.sums) { + const oldSum = engine.getShaSum(type, schema, routineName); + if (oldSum || opts.sums || (opts.sumViews && type === 'view')) { const shaSum = engine.shaSum(sql); if (oldSum !== shaSum) { - engine.setShaSum( - exporter.objectType, schema, routineName, shaSum); + engine.setShaSum(type, schema, routineName, shaSum); update = true; } } else if (params.modified && engine.lastPull) { diff --git a/myt-push.js b/myt-push.js index 1d0ae6b..498686f 100644 --- a/myt-push.js +++ b/myt-push.js @@ -349,7 +349,7 @@ class Push extends Command { ); } - if (opts.sums || oldSum) + if (opts.sums || oldSum || (opts.sumViews && type === 'view')) await engine.fetchShaSum(type, schema, name); } else { const escapedName = diff --git a/myt.js b/myt.js index 6b893de..ee86e4d 100755 --- a/myt.js +++ b/myt.js @@ -45,6 +45,7 @@ class Myt { 'Myt'.green, `v${packageJson.version}`.magenta ); + this.packageJson = packageJson; let baseOpts = this.constructor.opts; baseOpts.default = Object.assign(baseOpts.default || {}, { diff --git a/package-lock.json b/package-lock.json index f8b7b18..a39161e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@verdnatura/myt", - "version": "1.5.8", + "version": "1.5.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@verdnatura/myt", - "version": "1.5.8", + "version": "1.5.9", "license": "GPL-3.0", "dependencies": { "@sqltools/formatter": "^1.2.5", diff --git a/package.json b/package.json index f74ade8..bbb0252 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@verdnatura/myt", - "version": "1.5.8", + "version": "1.5.9", "author": "Verdnatura Levante SL", "description": "MySQL version control", "license": "GPL-3.0", diff --git a/template/dump/Dockerfile.base b/template/dump/Dockerfile.base new file mode 100644 index 0000000..4c35fd5 --- /dev/null +++ b/template/dump/Dockerfile.base @@ -0,0 +1 @@ +FROM mariadb:10.7 diff --git a/template/dump/afterDump.sql b/template/dump/afterDump.sql new file mode 100644 index 0000000..d2e0ec1 --- /dev/null +++ b/template/dump/afterDump.sql @@ -0,0 +1 @@ +-- Executed after data dump diff --git a/template/dump/beforeDump.sql b/template/dump/beforeDump.sql new file mode 100644 index 0000000..c206249 --- /dev/null +++ b/template/dump/beforeDump.sql @@ -0,0 +1 @@ +-- Executed before data dump diff --git a/template/dump/structure.sql b/template/dump/structure.sql deleted file mode 100644 index ab50a96..0000000 --- a/template/dump/structure.sql +++ /dev/null @@ -1 +0,0 @@ --- Place your local structure changes here diff --git a/template/package.json b/template/package.json index fd7065b..a8ec17f 100644 --- a/template/package.json +++ b/template/package.json @@ -8,6 +8,6 @@ "type": "git" }, "dependencies": { - "@verdnatura/myt": "^1.5.8" + "@verdnatura/myt": "^1.0.0" } }