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
|
|
|
|
|
|
|
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();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
const version = await myt.fetchDbVersion() || {};
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
`Database information:`
|
|
|
|
+ `\n -> Version: ${version.number}`
|
|
|
|
+ `\n -> Commit: ${version.gitCommit}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!version.number)
|
2021-10-25 13:38:07 +00:00
|
|
|
version.number = String('0').padStart(opts.versionDigits, '0');
|
2020-12-05 21:50:45 +00:00
|
|
|
if (!/^[0-9]*$/.test(version.number))
|
|
|
|
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.');
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-01-31 13:07:08 +00:00
|
|
|
function logVersion(version, name, error) {
|
|
|
|
console.log('', version.bold, name);
|
|
|
|
}
|
|
|
|
function logScript(type, message, error) {
|
|
|
|
console.log(' ', type.bold, message);
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
2022-03-29 10:36:30 +00:00
|
|
|
function isUndoScript(script) {
|
|
|
|
return /\.undo\.sql$/.test(script);
|
|
|
|
}
|
2020-12-02 07:35:26 +00:00
|
|
|
|
|
|
|
if (await fs.pathExists(versionsDir)) {
|
|
|
|
const versionDirs = await fs.readdir(versionsDir);
|
|
|
|
|
|
|
|
for (const versionDir of versionDirs) {
|
|
|
|
if (versionDir == 'README.md')
|
|
|
|
continue;
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
const dirVersion = myt.parseVersionDir(versionDir);
|
2022-04-30 00:06:56 +00:00
|
|
|
if (!dirVersion) {
|
2022-01-31 13:07:08 +00:00
|
|
|
logVersion('[?????]'.yellow, versionDir,
|
|
|
|
`Wrong directory name.`
|
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:06:56 +00:00
|
|
|
const versionNumber = dirVersion.number;
|
|
|
|
const versionName = dirVersion.name;
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
if (versionNumber.length != version.number.length) {
|
2022-01-31 13:07:08 +00:00
|
|
|
logVersion('[*****]'.gray, versionDir,
|
|
|
|
`Bad version length, should have ${version.number.length} characters.`
|
|
|
|
);
|
2020-12-02 07:35:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptsDir = `${versionsDir}/${versionDir}`;
|
|
|
|
const scripts = await fs.readdir(scriptsDir);
|
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
const [versionLog] = await conn.query(
|
|
|
|
`SELECT file FROM versionLog
|
|
|
|
WHERE code = ?
|
|
|
|
AND number = ?
|
|
|
|
AND errorNumber IS NULL`,
|
|
|
|
[opts.code, versionNumber]
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const script of scripts)
|
|
|
|
if (!isUndoScript(script)
|
|
|
|
&& versionLog.findIndex(x => x.file == script) === -1) {
|
|
|
|
silent = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (silent) continue;
|
|
|
|
logVersion(`[${versionNumber}]`.cyan, versionName);
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
for (const script of scripts) {
|
2022-01-31 13:07:08 +00:00
|
|
|
if (!/^[0-9]{2}-[a-zA-Z0-9_]+(.undo)?\.sql$/.test(script)) {
|
|
|
|
logScript('[W]'.yellow, script, `Wrong file name.`);
|
2020-12-02 07:35:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-29 10:36:30 +00:00
|
|
|
if (isUndoScript(script))
|
2022-01-31 13:07:08 +00:00
|
|
|
continue;
|
2020-12-02 07:35:26 +00:00
|
|
|
|
2022-01-31 13:07:08 +00:00
|
|
|
const [[row]] = await conn.query(
|
|
|
|
`SELECT errorNumber FROM versionLog
|
|
|
|
WHERE code = ?
|
|
|
|
AND number = ?
|
|
|
|
AND file = ?`,
|
|
|
|
[
|
|
|
|
opts.code,
|
|
|
|
versionNumber,
|
|
|
|
script
|
|
|
|
]
|
|
|
|
);
|
|
|
|
const apply = !row || row.errorNumber;
|
|
|
|
const actionMsg = apply ? '[+]'.green : '[I]'.blue;
|
|
|
|
|
|
|
|
logScript(actionMsg, script);
|
|
|
|
if (!apply) continue;
|
|
|
|
|
|
|
|
let err;
|
|
|
|
try {
|
2022-12-29 09:15:02 +00:00
|
|
|
await connExt.queryFromFile(pushConn,
|
2022-02-02 04:05:31 +00:00
|
|
|
`${scriptsDir}/${script}`);
|
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,
|
|
|
|
versionNumber,
|
|
|
|
script,
|
|
|
|
err && err.errno,
|
|
|
|
err && err.message
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (err) throw err;
|
2020-12-02 07:35:26 +00:00
|
|
|
nChanges++;
|
|
|
|
}
|
|
|
|
|
2022-02-07 14:54:24 +00:00
|
|
|
await this.updateVersion('number', versionNumber);
|
2020-12-02 07:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2022-12-29 09:15:02 +00:00
|
|
|
let changes = await this.changedRoutines(version.gitCommit);
|
2020-12-04 09:15:29 +00:00
|
|
|
changes = this.parseChanges(changes);
|
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 {
|
2022-12-23 13:47:44 +00:00
|
|
|
if (opts.trigger && change.type.name === 'TRIGGER')
|
|
|
|
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);
|
2022-02-02 04:05:31 +00:00
|
|
|
const isEqual = newSql == oldSql;
|
|
|
|
|
|
|
|
let actionMsg;
|
|
|
|
if ((exists && isEqual) || (!exists && !oldSql))
|
|
|
|
actionMsg = '[I]'.blue;
|
|
|
|
else if (exists)
|
|
|
|
actionMsg = '[+]'.green;
|
|
|
|
else
|
|
|
|
actionMsg = '[-]'.red;
|
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
const typeMsg = `[${change.type.abbr}]`[change.type.color];
|
|
|
|
console.log('', actionMsg.bold, typeMsg.bold, change.fullName);
|
|
|
|
|
2022-03-29 10:36:30 +00:00
|
|
|
if (!isEqual) {
|
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
|
|
|
|
|
|
|
if (nRoutines > 0) {
|
|
|
|
console.log(` -> ${nRoutines} routines have changed.`);
|
|
|
|
} 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
|
|
|
|
2022-04-04 18:53:18 +00:00
|
|
|
if (head && version.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
|
|
|
}
|
|
|
|
|
2020-12-04 09:15:29 +00:00
|
|
|
parseChanges(changes) {
|
2020-12-02 07:35:26 +00:00
|
|
|
const routines = [];
|
2020-12-05 21:50:45 +00:00
|
|
|
if (changes)
|
|
|
|
for (const change of changes)
|
|
|
|
routines.push(new Routine(change));
|
2020-12-02 07:35:26 +00:00
|
|
|
return routines;
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
return changes.sort((a, b) => {
|
|
|
|
if (b.mark != a.mark)
|
|
|
|
return b.mark == '-' ? 1 : -1;
|
|
|
|
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);
|