fixes #6204 tested code

This commit is contained in:
Javi Gallego 2023-09-22 10:25:06 +02:00
parent 11a57b8168
commit 38aec90073
3 changed files with 28 additions and 13 deletions

View File

@ -22,3 +22,8 @@ CREATE TABLE `versionLog` (
ALTER TABLE `versionLog` ALTER TABLE `versionLog`
ADD PRIMARY KEY (`code`,`number`,`file`); 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;

View File

@ -151,19 +151,19 @@ class Push extends Command {
function isUndoScript(script) { function isUndoScript(script) {
return /\.undo\.sql$/.test(script); return /\.undo\.sql$/.test(script);
} }
function isOtherEnvScript(script, env) { function isOtherRealmScript(script, realm) {
const splitScript = script.split('.'); const splitScript = script.split('.');
const envPart = splitScript[splitScript.length - 2]; const realmPart = splitScript[splitScript.length - 2];
if (splitScript.length <= 2) { if (splitScript.length <= 2) {
return false; return false;
} }
if (!env) { if (!realm) {
return !!envPart; return !!realmPart;
} }
return envPart && envPart !== env; return realmPart && realmPart !== realm;
} }
@ -207,6 +207,8 @@ class Push extends Command {
[opts.code, versionNumber] [opts.code, versionNumber]
); );
const realm = await myt.fetchDbRealm();
for (const script of scripts) for (const script of scripts)
if (!isUndoScript(script) if (!isUndoScript(script)
&& versionLog.findIndex(x => x.file == script) === -1) { && versionLog.findIndex(x => x.file == script) === -1) {
@ -218,14 +220,14 @@ class Push extends Command {
logVersion(`[${versionNumber}]`.cyan, versionName); logVersion(`[${versionNumber}]`.cyan, versionName);
for (const script of scripts) { 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.`); logScript('[W]'.yellow, script, `Wrong file name.`);
continue; continue;
} }
if (isUndoScript(script)) if (isUndoScript(script))
continue; continue;
if (isOtherEnvScript(script, opts.env)) if (isOtherRealmScript(script, realm))
continue; continue;
const [[row]] = await conn.query( const [[row]] = await conn.query(

18
myt.js
View File

@ -172,7 +172,7 @@ class Myt {
const defaultConfig = require(`${__dirname}/assets/myt.default.yml`); const defaultConfig = require(`${__dirname}/assets/myt.default.yml`);
const config = Object.assign({}, defaultConfig); const config = Object.assign({}, defaultConfig);
const configFile = 'myt.config.yml'; const configFile = 'myt.config.yml';
const configPath = path.join(opts.workspace, configFile); const configPath = path.join(opts.workspace, configFile);
@ -205,7 +205,7 @@ class Myt {
opts.dumpDir = path.join(opts.mytDir, 'dump'); opts.dumpDir = path.join(opts.mytDir, 'dump');
// Database configuration // Database configuration
let iniDir = path.join(__dirname, 'assets'); let iniDir = path.join(__dirname, 'assets');
let iniFile = 'db.ini'; let iniFile = 'db.ini';
@ -214,10 +214,10 @@ class Myt {
iniFile = `${opts.remote}.ini`; iniFile = `${opts.remote}.ini`;
} }
const iniPath = path.join(iniDir, iniFile); const iniPath = path.join(iniDir, iniFile);
if (!await fs.pathExists(iniPath)) if (!await fs.pathExists(iniPath))
throw new Error(`Database config file not found: ${iniPath}`); throw new Error(`Database config file not found: ${iniPath}`);
let dbConfig; let dbConfig;
try { try {
const iniData = ini.parse(await fs.readFile(iniPath, 'utf8')).client; const iniData = ini.parse(await fs.readFile(iniPath, 'utf8')).client;
@ -299,7 +299,7 @@ class Myt {
AND TABLE_NAME = 'version'`, AND TABLE_NAME = 'version'`,
[opts.versionSchema] [opts.versionSchema]
); );
if (!res.tableExists) { if (!res.tableExists) {
const structure = await fs.readFile( const structure = await fs.readFile(
`${__dirname}/assets/structure.sql`, 'utf8'); `${__dirname}/assets/structure.sql`, 'utf8');
@ -323,6 +323,14 @@ class Myt {
return version; return version;
} }
async fetchDbRealm() {
const [[realm]] = await this.conn.query(
`SELECT realm
FROM versionConfig`
);
return realm?.realm;
}
parseVersionDir(versionDir) { parseVersionDir(versionDir) {
const match = versionDir.match(/^([0-9]+)-([a-zA-Z0-9]+)?$/); const match = versionDir.match(/^([0-9]+)-([a-zA-Z0-9]+)?$/);
if (!match) return null; if (!match) return null;