From 38aec9007326f21fb943b07e82ab8068f588529f Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 22 Sep 2023 10:25:06 +0200 Subject: [PATCH] fixes #6204 tested code --- assets/structure.sql | 5 +++++ myt-push.js | 18 ++++++++++-------- myt.js | 18 +++++++++++++----- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/assets/structure.sql b/assets/structure.sql index 368332e..12c713a 100644 --- a/assets/structure.sql +++ b/assets/structure.sql @@ -22,3 +22,8 @@ CREATE TABLE `versionLog` ( ALTER TABLE `versionLog` ADD PRIMARY KEY (`code`,`number`,`file`); + +CREATE TABLE `util`.`versionConfig` ( + `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + `realm` VARCHAR(16) NULL DEFAULT NULL COMMENT 'Data set on which the project runs' +) ENGINE=InnoDB; diff --git a/myt-push.js b/myt-push.js index b0b0e76..380ad1c 100644 --- a/myt-push.js +++ b/myt-push.js @@ -151,19 +151,19 @@ class Push extends Command { function isUndoScript(script) { return /\.undo\.sql$/.test(script); } - function isOtherEnvScript(script, env) { + function isOtherRealmScript(script, realm) { const splitScript = script.split('.'); - const envPart = splitScript[splitScript.length - 2]; + const realmPart = splitScript[splitScript.length - 2]; if (splitScript.length <= 2) { return false; } - if (!env) { - return !!envPart; + if (!realm) { + return !!realmPart; } - - return envPart && envPart !== env; + + return realmPart && realmPart !== realm; } @@ -207,6 +207,8 @@ class Push extends Command { [opts.code, versionNumber] ); + const realm = await myt.fetchDbRealm(); + for (const script of scripts) if (!isUndoScript(script) && versionLog.findIndex(x => x.file == script) === -1) { @@ -218,14 +220,14 @@ class Push extends Command { logVersion(`[${versionNumber}]`.cyan, versionName); for (const script of scripts) { - if (!/^[0-9]{2}-[a-zA-Z0-9_]+(.undo)?\.sql$/.test(script)) { + if (!/^[0-9]{2}-[a-zA-Z0-9_]+(\..+)?\.sql$/.test(script)) { logScript('[W]'.yellow, script, `Wrong file name.`); continue; } if (isUndoScript(script)) continue; - if (isOtherEnvScript(script, opts.env)) + if (isOtherRealmScript(script, realm)) continue; const [[row]] = await conn.query( diff --git a/myt.js b/myt.js index e5c5659..f034fbc 100755 --- a/myt.js +++ b/myt.js @@ -172,7 +172,7 @@ class Myt { const defaultConfig = require(`${__dirname}/assets/myt.default.yml`); const config = Object.assign({}, defaultConfig); - + const configFile = 'myt.config.yml'; const configPath = path.join(opts.workspace, configFile); @@ -205,7 +205,7 @@ class Myt { opts.dumpDir = path.join(opts.mytDir, 'dump'); // Database configuration - + let iniDir = path.join(__dirname, 'assets'); let iniFile = 'db.ini'; @@ -214,10 +214,10 @@ class Myt { iniFile = `${opts.remote}.ini`; } const iniPath = path.join(iniDir, iniFile); - + if (!await fs.pathExists(iniPath)) throw new Error(`Database config file not found: ${iniPath}`); - + let dbConfig; try { const iniData = ini.parse(await fs.readFile(iniPath, 'utf8')).client; @@ -299,7 +299,7 @@ class Myt { AND TABLE_NAME = 'version'`, [opts.versionSchema] ); - + if (!res.tableExists) { const structure = await fs.readFile( `${__dirname}/assets/structure.sql`, 'utf8'); @@ -323,6 +323,14 @@ class Myt { return version; } + + async fetchDbRealm() { + const [[realm]] = await this.conn.query( + `SELECT realm + FROM versionConfig` + ); + return realm?.realm; + } parseVersionDir(versionDir) { const match = versionDir.match(/^([0-9]+)-([a-zA-Z0-9]+)?$/); if (!match) return null;