refs #4036 sumViews option, sum & template fixes

This commit is contained in:
Juan Ferrer 2022-12-29 17:58:40 +01:00
parent 2f31d0f98e
commit 021e3cb083
13 changed files with 35 additions and 26 deletions

View File

@ -1,6 +1,7 @@
versionSchema: myt versionSchema: myt
versionDigits: 5 versionDigits: 5
maxOldVersions: 30 maxOldVersions: 30
sumViews: true
schemas: schemas:
- myt - myt
fixtures: fixtures:

View File

@ -52,16 +52,6 @@ class Dump extends Command {
} }
await dumper.end(); 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)
);
}
} }
} }

View File

@ -1,6 +1,7 @@
const Myt = require('./myt'); const Myt = require('./myt');
const Command = require('./lib/command'); const Command = require('./lib/command');
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path');
class Init extends Command { class Init extends Command {
static usage = { static usage = {
@ -8,12 +9,23 @@ class Init extends Command {
}; };
async run(myt, opts) { 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); const templates = await fs.readdir(templateDir);
for (let template of templates) { for (let template of templates) {
const dst = `${opts.mytDir}/${template}`; const dst = path.join(opts.mytDir, template);
if (!await fs.pathExists(dst)) 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, ' '));
} }
} }
} }

View File

@ -125,9 +125,11 @@ class Pull extends Command {
const {opts} = this; const {opts} = this;
const res = await exporter.query(conn, schema); 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)) if (!await fs.pathExists(routineDir))
await fs.mkdir(routineDir); await fs.mkdir(routineDir);
@ -140,8 +142,10 @@ class Pull extends Command {
const match = routineFile.match(/^(.*)\.sql$/); const match = routineFile.match(/^(.*)\.sql$/);
if (!match) continue; if (!match) continue;
const routine = match[1]; const routine = match[1];
if (!routineSet.has(routine)) if (!routineSet.has(routine)) {
await fs.remove(`${routineDir}/${routine}.sql`); await fs.remove(`${routineDir}/${routine}.sql`);
engine.deleteShaSum(type, schema, routine)
}
} }
for (const params of res) { for (const params of res) {
@ -150,12 +154,11 @@ class Pull extends Command {
const routineFile = `${routineDir}/${routineName}.sql`; const routineFile = `${routineDir}/${routineName}.sql`;
let update = opts.update; let update = opts.update;
const oldSum = engine.getShaSum(routineName); const oldSum = engine.getShaSum(type, schema, routineName);
if (oldSum || opts.sums) { if (oldSum || opts.sums || (opts.sumViews && type === 'view')) {
const shaSum = engine.shaSum(sql); const shaSum = engine.shaSum(sql);
if (oldSum !== shaSum) { if (oldSum !== shaSum) {
engine.setShaSum( engine.setShaSum(type, schema, routineName, shaSum);
exporter.objectType, schema, routineName, shaSum);
update = true; update = true;
} }
} else if (params.modified && engine.lastPull) { } else if (params.modified && engine.lastPull) {

View File

@ -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); await engine.fetchShaSum(type, schema, name);
} else { } else {
const escapedName = const escapedName =

1
myt.js
View File

@ -45,6 +45,7 @@ class Myt {
'Myt'.green, 'Myt'.green,
`v${packageJson.version}`.magenta `v${packageJson.version}`.magenta
); );
this.packageJson = packageJson;
let baseOpts = this.constructor.opts; let baseOpts = this.constructor.opts;
baseOpts.default = Object.assign(baseOpts.default || {}, { baseOpts.default = Object.assign(baseOpts.default || {}, {

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.8", "version": "1.5.9",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.8", "version": "1.5.9",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"@sqltools/formatter": "^1.2.5", "@sqltools/formatter": "^1.2.5",

View File

@ -1,6 +1,6 @@
{ {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.8", "version": "1.5.9",
"author": "Verdnatura Levante SL", "author": "Verdnatura Levante SL",
"description": "MySQL version control", "description": "MySQL version control",
"license": "GPL-3.0", "license": "GPL-3.0",

View File

@ -0,0 +1 @@
FROM mariadb:10.7

View File

@ -0,0 +1 @@
-- Executed after data dump

View File

@ -0,0 +1 @@
-- Executed before data dump

View File

@ -1 +0,0 @@
-- Place your local structure changes here

View File

@ -8,6 +8,6 @@
"type": "git" "type": "git"
}, },
"dependencies": { "dependencies": {
"@verdnatura/myt": "^1.5.8" "@verdnatura/myt": "^1.0.0"
} }
} }