2022-12-21 13:17:50 +00:00
|
|
|
const Myt = require('./myt');
|
2022-12-21 12:34:17 +00:00
|
|
|
const Command = require('./lib/command');
|
2020-12-02 07:35:26 +00:00
|
|
|
const fs = require('fs-extra');
|
2020-12-04 10:53:11 +00:00
|
|
|
const nodegit = require('nodegit');
|
2022-12-21 12:34:17 +00:00
|
|
|
const ExporterEngine = require('./lib/exporter-engine');
|
2022-12-29 09:15:02 +00:00
|
|
|
const connExt = require('./lib/conn');
|
|
|
|
const repoExt = require('./lib/repo');
|
2022-12-29 13:27:16 +00:00
|
|
|
const SqlString = require('sqlstring');
|
2020-12-04 09:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pushes changes to remote.
|
|
|
|
*/
|
2022-12-21 12:34:17 +00:00
|
|
|
class Push extends Command {
|
|
|
|
static usage = {
|
|
|
|
description: 'Apply changes into database',
|
|
|
|
params: {
|
|
|
|
force: 'Answer yes to all questions',
|
|
|
|
commit: 'Wether to save the commit SHA into database',
|
2022-12-23 13:47:44 +00:00
|
|
|
sums: 'Save SHA sums of pushed objects',
|
|
|
|
triggers: 'Wether to exclude triggers, used to generate local DB'
|
2022-12-21 12:34:17 +00:00
|
|
|
},
|
|
|
|
operand: 'remote'
|
|
|
|
};
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
static opts = {
|
2022-12-21 12:34:17 +00:00
|
|
|
alias: {
|
|
|
|
force: 'f',
|
|
|
|
commit: 'c',
|
2022-12-23 13:47:44 +00:00
|
|
|
sums: 's',
|
|
|
|
triggers: 't'
|
2022-12-21 12:34:17 +00:00
|
|
|
},
|
|
|
|
boolean: [
|
|
|
|
'force',
|
|
|
|
'commit',
|
2022-12-23 13:47:44 +00:00
|
|
|
'sums',
|
|
|
|
'triggers'
|
2022-12-21 12:34:17 +00:00
|
|
|
]
|
|
|
|
};
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
async run(myt, opts) {
|
|
|
|
const conn = await myt.dbConnect();
|
2020-12-02 07:35:26 +00:00
|
|
|
this.conn = conn;
|
|
|
|
|
2022-06-09 09:42:03 +00:00
|
|
|
if (opts.remote == 'local')
|
2022-04-30 00:33:41 +00:00
|
|
|
opts.commit = true;
|
2022-04-30 00:06:56 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
// Obtain exclusive lock
|
|
|
|
|
|
|
|
const [[row]] = await conn.query(
|
2022-12-21 13:17:50 +00:00
|
|
|
`SELECT GET_LOCK('myt_push', 30) getLock`);
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2023-04-25 15:06:57 +00:00
|
|
|
const pingTimeout = setInterval(async() => {
|
|
|
|
try {
|
|
|
|
await conn.ping()
|
|
|
|
} catch (e) {}
|
|
|
|
}, 60 * 1000);
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
if (!row.getLock) {
|
|
|
|
let isUsed = 0;
|
|
|
|
|
|
|
|
if (row.getLock == 0) {
|
|
|
|
const [[row]] = await conn.query(
|
2022-12-21 13:17:50 +00:00
|
|
|
`SELECT IS_USED_LOCK('myt_push') isUsed`);
|
2021-10-25 13:38:07 +00:00
|
|
|
isUsed = row.isUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Cannot obtain exclusive lock, used by connection ${isUsed}`);
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
async function releaseLock() {
|
2022-12-21 13:17:50 +00:00
|
|
|
await conn.query(`DO RELEASE_LOCK('myt_push')`);
|
2022-03-29 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-21 13:17:50 +00:00
|
|
|
await this.push(myt, opts, conn);
|
2022-03-29 10:36:30 +00:00
|
|
|
} catch(err) {
|
|
|
|
try {
|
|
|
|
await releaseLock();
|
2023-04-25 15:06:57 +00:00
|
|
|
} catch (e) {}
|
2022-03-29 10:36:30 +00:00
|
|
|
throw err;
|
2023-04-25 15:06:57 +00:00
|
|
|
} finally {
|
|
|
|
clearInterval(pingTimeout);
|
2022-03-29 10:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await releaseLock();
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
async push(myt, opts, conn) {
|
|
|
|
const pushConn = await myt.createConnection();
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
// Get database version
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
const dbVersion = await myt.fetchDbVersion() || {};
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
`Database information:`
|
2024-01-14 14:49:49 +00:00
|
|
|
+ `\n -> Version: ${dbVersion.number}`
|
|
|
|
+ `\n -> Commit: ${dbVersion.gitCommit}`
|
2020-12-02 07:35:26 +00:00
|
|
|
);
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
if (!dbVersion.number)
|
|
|
|
dbVersion.number = String('0').padStart(opts.versionDigits, '0');
|
|
|
|
if (!/^[0-9]*$/.test(dbVersion.number))
|
2020-12-05 21:50:45 +00:00
|
|
|
throw new Error('Wrong database version');
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
// Prevent push to production by mistake
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
if (opts.remote == 'production') {
|
2020-12-02 07:35:26 +00:00
|
|
|
console.log(
|
|
|
|
'\n ( ( ) ( ( ) ) '
|
|
|
|
+ '\n )\\ ))\\ ) ( /( )\\ ) ( ))\\ ) ( /( ( /( '
|
|
|
|
+ '\n(()/(()/( )\\()|()/( ( )\\ ) /(()/( )\\()) )\\())'
|
|
|
|
+ '\n /(_))(_)|(_)\\ /(_)) )\\ (((_) ( )(_))(_)|(_)\\ ((_)\\ '
|
|
|
|
+ '\n(_))(_)) ((_|_))_ _ ((_))\\___(_(_()|__)) ((_) _((_)'
|
|
|
|
+ '\n| _ \\ _ \\ / _ \\| \\| | | ((/ __|_ _|_ _| / _ \\| \\| |'
|
|
|
|
+ '\n| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |'
|
|
|
|
+ '\n|_| |_|_\\ \\___/|___/ \\___/ \\___| |_| |___| \\___/|_|\\_|'
|
|
|
|
+ '\n'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!opts.force) {
|
|
|
|
const readline = require('readline');
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
const answer = await new Promise(resolve => {
|
|
|
|
rl.question('Are you sure? (Default: no) [yes|no] ', resolve);
|
|
|
|
});
|
|
|
|
rl.close();
|
|
|
|
|
|
|
|
if (answer !== 'yes')
|
|
|
|
throw new Error('Changes aborted');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
// Apply versions
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
console.log('Applying versions.');
|
|
|
|
|
2024-01-19 09:16:47 +00:00
|
|
|
let nVersions = 0;
|
2020-12-02 07:35:26 +00:00
|
|
|
let nChanges = 0;
|
2022-03-29 10:36:30 +00:00
|
|
|
let silent = true;
|
2022-12-21 12:34:17 +00:00
|
|
|
const versionsDir = opts.versionsDir;
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
function logVersion(version, name, action, error) {
|
|
|
|
let actionMsg;
|
|
|
|
switch(action) {
|
|
|
|
case 'apply':
|
|
|
|
actionMsg = '[A]'.green;
|
|
|
|
break;
|
|
|
|
case 'ignore':
|
|
|
|
actionMsg = '[I]'.blue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
actionMsg = '[W]'.yellow;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('', (actionMsg + version).bold, name);
|
2022-01-31 13:07:08 +00:00
|
|
|
}
|
|
|
|
function logScript(type, message, error) {
|
|
|
|
console.log(' ', type.bold, message);
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
2024-01-14 14:49:49 +00:00
|
|
|
|
2023-01-25 17:24:02 +00:00
|
|
|
const skipFiles = new Set([
|
|
|
|
'README.md',
|
|
|
|
'.archive'
|
|
|
|
]);
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
if (await fs.pathExists(versionsDir)) {
|
|
|
|
const versionDirs = await fs.readdir(versionsDir);
|
|
|
|
for (const versionDir of versionDirs) {
|
2023-01-25 17:24:02 +00:00
|
|
|
if (skipFiles.has(versionDir)) continue;
|
2024-01-14 14:49:49 +00:00
|
|
|
const version = await myt.loadVersion(versionDir);
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
if (!version) {
|
|
|
|
logVersion('[?????]'.yellow, versionDir, 'warn',
|
2022-01-31 13:07:08 +00:00
|
|
|
`Wrong directory name.`
|
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-14 14:49:49 +00:00
|
|
|
if (version.number.length != dbVersion.number.length) {
|
|
|
|
logVersion('[*****]'.gray, versionDir, 'warn'
|
|
|
|
`Bad version length, should have ${dbVersion.number.length} characters.`
|
2022-01-31 13:07:08 +00:00
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
const {apply} = version;
|
|
|
|
if (apply) silent = false;
|
2022-03-29 10:36:30 +00:00
|
|
|
if (silent) continue;
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
const action = apply ? 'apply' : 'ignore';
|
|
|
|
logVersion(`[${version.number}]`.cyan, version.name, action);
|
|
|
|
if (!apply) continue;
|
2023-09-28 08:02:40 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
for (const script of version.scripts) {
|
|
|
|
const scriptFile = script.file;
|
|
|
|
|
|
|
|
if (!script.matchRegex) {
|
|
|
|
logScript('[W]'.yellow, scriptFile, `Wrong file name.`);
|
2022-01-31 13:07:08 +00:00
|
|
|
continue;
|
2024-01-14 14:49:49 +00:00
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
const actionMsg = script.apply ? '[+]'.green : '[I]'.blue;
|
|
|
|
logScript(actionMsg, scriptFile);
|
|
|
|
|
|
|
|
if (!script.apply) continue;
|
2022-01-31 13:07:08 +00:00
|
|
|
|
|
|
|
let err;
|
|
|
|
try {
|
2022-12-29 09:15:02 +00:00
|
|
|
await connExt.queryFromFile(pushConn,
|
2024-01-14 14:49:49 +00:00
|
|
|
`${versionsDir}/${versionDir}/${scriptFile}`);
|
2022-01-31 13:07:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
await conn.query(
|
|
|
|
`INSERT INTO versionLog
|
|
|
|
SET code = ?,
|
|
|
|
number = ?,
|
|
|
|
file = ?,
|
|
|
|
user = USER(),
|
|
|
|
updated = NOW(),
|
|
|
|
errorNumber = ?,
|
|
|
|
errorMessage = ?
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
updated = VALUES(updated),
|
|
|
|
user = VALUES(user),
|
|
|
|
errorNumber = VALUES(errorNumber),
|
|
|
|
errorMessage = VALUES(errorMessage)`,
|
|
|
|
[
|
|
|
|
opts.code,
|
2024-01-14 14:49:49 +00:00
|
|
|
version.number,
|
|
|
|
scriptFile,
|
2022-01-31 13:07:08 +00:00
|
|
|
err && err.errno,
|
|
|
|
err && err.message
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (err) throw err;
|
2020-12-02 07:35:26 +00:00
|
|
|
nChanges++;
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
await this.updateVersion('number', version.number);
|
2024-01-19 09:16:47 +00:00
|
|
|
nVersions++;
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 09:16:47 +00:00
|
|
|
if (nVersions) {
|
|
|
|
console.log(` -> ${nVersions} versions with ${nChanges} changes applied.`);
|
|
|
|
} else
|
|
|
|
console.log(` -> No versions applied.`);
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
// Apply routines
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
console.log('Applying changed routines.');
|
2022-03-17 21:45:05 +00:00
|
|
|
|
|
|
|
const gitExists = await fs.pathExists(`${opts.workspace}/.git`);
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
let nRoutines = 0;
|
2024-01-14 14:49:49 +00:00
|
|
|
const changes = await this.changedRoutines(dbVersion.gitCommit);
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
const routines = [];
|
|
|
|
for (const change of changes)
|
|
|
|
if (change.isRoutine)
|
2021-10-27 10:56:25 +00:00
|
|
|
routines.push([
|
|
|
|
change.schema,
|
|
|
|
change.name,
|
|
|
|
change.type.name
|
|
|
|
]);
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
if (routines.length) {
|
|
|
|
await conn.query(
|
|
|
|
`DROP TEMPORARY TABLE IF EXISTS tProcsPriv`
|
|
|
|
);
|
|
|
|
await conn.query(
|
|
|
|
`CREATE TEMPORARY TABLE tProcsPriv
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT * FROM mysql.procs_priv
|
2021-10-27 10:56:25 +00:00
|
|
|
WHERE (Db, Routine_name, Routine_type) IN (?)`,
|
2020-12-02 07:35:26 +00:00
|
|
|
[routines]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-25 17:16:43 +00:00
|
|
|
const engine = new ExporterEngine(conn, opts);
|
2022-02-02 04:05:31 +00:00
|
|
|
await engine.init();
|
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
async function finalize() {
|
2022-04-30 00:06:56 +00:00
|
|
|
await engine.saveInfo();
|
2022-03-29 10:36:30 +00:00
|
|
|
|
|
|
|
if (routines.length) {
|
|
|
|
await conn.query('FLUSH PRIVILEGES');
|
|
|
|
await conn.query(`DROP TEMPORARY TABLE tProcsPriv`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const change of changes)
|
|
|
|
try {
|
2023-08-11 13:41:03 +00:00
|
|
|
if (opts.triggers && change.type.name === 'TRIGGER')
|
2022-12-23 13:47:44 +00:00
|
|
|
continue;
|
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
const schema = change.schema;
|
|
|
|
const name = change.name;
|
|
|
|
const type = change.type.name.toLowerCase();
|
2022-12-21 12:34:17 +00:00
|
|
|
const fullPath = `${opts.routinesDir}/${change.path}.sql`;
|
2020-12-04 09:15:29 +00:00
|
|
|
const exists = await fs.pathExists(fullPath);
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
let newSql;
|
|
|
|
if (exists)
|
|
|
|
newSql = await fs.readFile(fullPath, 'utf8');
|
|
|
|
const oldSql = await engine.fetchRoutine(type, schema, name);
|
2022-04-30 00:06:56 +00:00
|
|
|
const oldSum = engine.getShaSum(type, schema, name);
|
2023-08-11 13:41:03 +00:00
|
|
|
|
|
|
|
const isMockFn = type == 'function'
|
|
|
|
&& schema == opts.versionSchema
|
|
|
|
&& opts.remote == 'local'
|
|
|
|
&& opts.mockDate
|
|
|
|
&& opts.mockFunctions
|
|
|
|
&& opts.mockFunctions.indexOf(name) !== -1;
|
|
|
|
const ignore = newSql == oldSql || isMockFn;
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2023-03-15 17:13:01 +00:00
|
|
|
let statusMsg;
|
|
|
|
if (exists && !oldSql)
|
|
|
|
statusMsg = '[+]'.green;
|
|
|
|
else if (!exists)
|
|
|
|
statusMsg = '[-]'.red;
|
|
|
|
else
|
|
|
|
statusMsg = '[·]'.yellow;
|
|
|
|
|
2022-02-02 04:05:31 +00:00
|
|
|
let actionMsg;
|
2023-08-11 13:41:03 +00:00
|
|
|
if (ignore)
|
2022-02-02 04:05:31 +00:00
|
|
|
actionMsg = '[I]'.blue;
|
|
|
|
else
|
2023-03-15 17:13:01 +00:00
|
|
|
actionMsg = '[A]'.green;
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
const typeMsg = `[${change.type.abbr}]`[change.type.color];
|
2023-03-15 17:13:01 +00:00
|
|
|
console.log('',
|
2024-01-14 14:49:49 +00:00
|
|
|
(actionMsg + statusMsg).bold,
|
2023-03-15 17:13:01 +00:00
|
|
|
typeMsg.bold,
|
|
|
|
change.fullName
|
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2023-08-11 13:41:03 +00:00
|
|
|
if (!ignore) {
|
2022-12-29 13:27:16 +00:00
|
|
|
const scapedSchema = SqlString.escapeId(schema, true);
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
if (exists) {
|
2022-02-07 10:46:13 +00:00
|
|
|
if (change.type.name === 'VIEW')
|
|
|
|
await pushConn.query(`USE ${scapedSchema}`);
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
await connExt.multiQuery(pushConn, newSql);
|
2022-02-07 10:46:13 +00:00
|
|
|
|
|
|
|
if (change.isRoutine) {
|
|
|
|
await conn.query(
|
|
|
|
`INSERT IGNORE INTO mysql.procs_priv
|
|
|
|
SELECT * FROM tProcsPriv
|
|
|
|
WHERE Db = ?
|
|
|
|
AND Routine_name = ?
|
|
|
|
AND Routine_type = ?`,
|
|
|
|
[schema, name, change.type.name]
|
|
|
|
);
|
2021-10-25 13:38:07 +00:00
|
|
|
}
|
2022-02-07 10:46:13 +00:00
|
|
|
|
2022-12-29 16:58:40 +00:00
|
|
|
if (opts.sums || oldSum || (opts.sumViews && type === 'view'))
|
2022-04-30 00:06:56 +00:00
|
|
|
await engine.fetchShaSum(type, schema, name);
|
2021-10-22 14:11:03 +00:00
|
|
|
} else {
|
|
|
|
const escapedName =
|
|
|
|
scapedSchema + '.' +
|
2022-12-29 13:27:16 +00:00
|
|
|
SqlString.escapeId(name, true);
|
2021-10-22 14:11:03 +00:00
|
|
|
|
|
|
|
const query = `DROP ${change.type.name} IF EXISTS ${escapedName}`;
|
|
|
|
await pushConn.query(query);
|
2022-02-02 04:05:31 +00:00
|
|
|
|
|
|
|
engine.deleteShaSum(type, schema, name);
|
2021-10-22 14:11:03 +00:00
|
|
|
}
|
2022-02-07 10:46:13 +00:00
|
|
|
|
|
|
|
nRoutines++;
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
2022-03-29 10:36:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
try {
|
|
|
|
await finalize();
|
|
|
|
} catch (e) {
|
2022-03-30 11:20:58 +00:00
|
|
|
console.error(e);
|
2022-03-29 10:36:30 +00:00
|
|
|
}
|
|
|
|
throw err;
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
await finalize();
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2024-01-19 09:16:47 +00:00
|
|
|
if (nRoutines) {
|
|
|
|
console.log(` -> ${nRoutines} routines changed.`);
|
2020-12-02 07:35:26 +00:00
|
|
|
} else
|
|
|
|
console.log(` -> No routines changed.`);
|
2021-10-16 06:44:19 +00:00
|
|
|
|
2022-04-30 00:33:41 +00:00
|
|
|
if (gitExists && opts.commit) {
|
2022-03-17 21:45:05 +00:00
|
|
|
const repo = await nodegit.Repository.open(this.opts.workspace);
|
|
|
|
const head = await repo.getHeadCommit();
|
2021-10-16 06:44:19 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
if (head && dbVersion.gitCommit !== head.sha())
|
2022-03-17 21:45:05 +00:00
|
|
|
await this.updateVersion('gitCommit', head.sha());
|
|
|
|
}
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
// End
|
2022-02-02 04:05:31 +00:00
|
|
|
|
|
|
|
await pushConn.end();
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 14:54:24 +00:00
|
|
|
async updateVersion(column, value) {
|
2022-12-29 13:27:16 +00:00
|
|
|
column = SqlString.escapeId(column, true);
|
2022-01-31 13:07:08 +00:00
|
|
|
await this.conn.query(
|
|
|
|
`INSERT INTO version
|
|
|
|
SET code = ?,
|
|
|
|
${column} = ?,
|
|
|
|
updated = NOW()
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
${column} = VALUES(${column}),
|
|
|
|
updated = VALUES(updated)`,
|
|
|
|
[
|
2022-02-07 14:54:24 +00:00
|
|
|
this.opts.code,
|
2022-01-31 13:07:08 +00:00
|
|
|
value
|
|
|
|
]
|
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
async changedRoutines(commitSha) {
|
|
|
|
const repo = await this.myt.openRepo();
|
|
|
|
const changes = [];
|
|
|
|
const changesMap = new Map();
|
|
|
|
|
|
|
|
async function pushChanges(diff) {
|
|
|
|
if (!diff) return;
|
|
|
|
const patches = await diff.patches();
|
|
|
|
|
|
|
|
for (const patch of patches) {
|
|
|
|
const path = patch.newFile().path();
|
|
|
|
const match = path.match(/^routines\/(.+)\.sql$/);
|
|
|
|
if (!match) continue;
|
|
|
|
|
|
|
|
let change = changesMap.get(match[1]);
|
|
|
|
if (!change) {
|
|
|
|
change = {path: match[1]};
|
|
|
|
changes.push(change);
|
|
|
|
changesMap.set(match[1], change);
|
|
|
|
}
|
|
|
|
change.mark = patch.isDeleted() ? '-' : '+';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const head = await repo.getHeadCommit();
|
|
|
|
|
|
|
|
if (head && commitSha) {
|
|
|
|
let commit;
|
|
|
|
let notFound;
|
|
|
|
|
|
|
|
try {
|
|
|
|
commit = await repo.getCommit(commitSha);
|
|
|
|
notFound = false;
|
|
|
|
} catch (err) {
|
|
|
|
if (err.errorFunction == 'Commit.lookup')
|
|
|
|
notFound = true;
|
|
|
|
else
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notFound) {
|
|
|
|
console.warn(`Database commit not found, trying git fetch`.yellow);
|
|
|
|
await repo.fetchAll();
|
|
|
|
commit = await repo.getCommit(commitSha);
|
|
|
|
}
|
|
|
|
|
|
|
|
const commitTree = await commit.getTree();
|
|
|
|
|
|
|
|
const headTree = await head.getTree();
|
|
|
|
const diff = await headTree.diff(commitTree);
|
|
|
|
await pushChanges(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
await pushChanges(await repoExt.getUnstaged(repo));
|
|
|
|
await pushChanges(await repoExt.getStaged(repo));
|
|
|
|
|
2023-02-16 10:06:58 +00:00
|
|
|
const routines = [];
|
|
|
|
for (const change of changes)
|
|
|
|
routines.push(new Routine(change));
|
|
|
|
|
|
|
|
return routines.sort((a, b) => {
|
2022-12-29 09:15:02 +00:00
|
|
|
if (b.mark != a.mark)
|
2023-03-15 17:13:01 +00:00
|
|
|
return b.mark == '-' ? -1 : 1;
|
2023-02-16 10:06:58 +00:00
|
|
|
|
|
|
|
if (b.type.name !== a.type.name) {
|
|
|
|
if (b.type.name == 'VIEW')
|
|
|
|
return -1;
|
|
|
|
if (a.type.name == 'VIEW')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
return a.path.localeCompare(b.path);
|
|
|
|
});
|
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 09:15:29 +00:00
|
|
|
const typeMap = {
|
|
|
|
events: {
|
|
|
|
name: 'EVENT',
|
|
|
|
abbr: 'EVNT',
|
|
|
|
color: 'cyan'
|
|
|
|
},
|
|
|
|
functions: {
|
|
|
|
name: 'FUNCTION',
|
|
|
|
abbr: 'FUNC',
|
|
|
|
color: 'cyan'
|
|
|
|
},
|
|
|
|
procedures: {
|
|
|
|
name: 'PROCEDURE',
|
|
|
|
abbr: 'PROC',
|
|
|
|
color: 'yellow'
|
|
|
|
},
|
|
|
|
triggers: {
|
|
|
|
name: 'TRIGGER',
|
|
|
|
abbr: 'TRIG',
|
|
|
|
color: 'blue'
|
|
|
|
},
|
|
|
|
views: {
|
|
|
|
name: 'VIEW',
|
|
|
|
abbr: 'VIEW',
|
|
|
|
color: 'magenta'
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
const routineTypes = new Set([
|
|
|
|
'FUNCTION',
|
|
|
|
'PROCEDURE'
|
|
|
|
]);
|
|
|
|
|
2020-12-04 09:15:29 +00:00
|
|
|
class Routine {
|
|
|
|
constructor(change) {
|
|
|
|
const path = change.path;
|
|
|
|
const split = path.split('/');
|
|
|
|
|
|
|
|
const schema = split[0];
|
|
|
|
const type = typeMap[split[1]];
|
|
|
|
const name = split[2];
|
|
|
|
|
2022-06-09 09:42:03 +00:00
|
|
|
if (split.length !== 3 || !type)
|
|
|
|
throw new Error(`Wrong routine path for '${path}', check that the sql file is located in the correct directory`);
|
|
|
|
|
2020-12-04 09:15:29 +00:00
|
|
|
Object.assign(this, {
|
|
|
|
path,
|
|
|
|
mark: change.mark,
|
|
|
|
type,
|
|
|
|
schema,
|
|
|
|
name,
|
|
|
|
fullName: `${schema}.${name}`,
|
2021-10-27 10:56:25 +00:00
|
|
|
isRoutine: routineTypes.has(type.name)
|
2020-12-04 09:15:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
module.exports = Push;
|
|
|
|
|
|
|
|
if (require.main === module)
|
2022-12-21 13:17:50 +00:00
|
|
|
new Myt().run(Push);
|