mirror of
https://github.com/Fabio286/antares.git
synced 2025-06-05 21:59:22 +02:00
feat(PostgreSQL): export functions and procedures
This commit is contained in:
@ -23,7 +23,7 @@ SET xmloption = content;
|
|||||||
SET client_min_messages = warning;
|
SET client_min_messages = warning;
|
||||||
SET row_security = off;\n\n\n`;
|
SET row_security = off;\n\n\n`;
|
||||||
|
|
||||||
dump += await this.getTypes();
|
dump += await this.getCreateTypes();
|
||||||
|
|
||||||
return dump;
|
return dump;
|
||||||
}
|
}
|
||||||
@ -162,7 +162,7 @@ SET row_security = off;\n\n\n`;
|
|||||||
let rowCount = 0;
|
let rowCount = 0;
|
||||||
const sqlStr = '';
|
const sqlStr = '';
|
||||||
|
|
||||||
const countResults = await this._client.raw(`SELECT COUNT(1) as count FROM "${tableName}"`);
|
const countResults = await this._client.raw(`SELECT COUNT(1) as count FROM "${this.schemaName}"."${tableName}"`);
|
||||||
if (countResults.rows.length === 1) rowCount = countResults.rows[0].count;
|
if (countResults.rows.length === 1) rowCount = countResults.rows[0].count;
|
||||||
|
|
||||||
if (rowCount > 0) {
|
if (rowCount > 0) {
|
||||||
@ -180,7 +180,7 @@ SET row_security = off;\n\n\n`;
|
|||||||
yield sqlStr;
|
yield sqlStr;
|
||||||
|
|
||||||
const stream = await this._queryStream(
|
const stream = await this._queryStream(
|
||||||
`SELECT ${columnNames} FROM "${tableName}"`
|
`SELECT ${columnNames} FROM "${this.schemaName}"."${tableName}"`
|
||||||
);
|
);
|
||||||
|
|
||||||
for await (const row of stream) {
|
for await (const row of stream) {
|
||||||
@ -270,7 +270,7 @@ SET row_security = off;\n\n\n`;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTypes () {
|
async getCreateTypes () {
|
||||||
let sqlString = '';
|
let sqlString = '';
|
||||||
const { rows: types } = await this._client.raw(`
|
const { rows: types } = await this._client.raw(`
|
||||||
SELECT pg_type.typname, pg_enum.enumlabel
|
SELECT pg_type.typname, pg_enum.enumlabel
|
||||||
@ -337,80 +337,53 @@ SET row_security = off;\n\n\n`;
|
|||||||
for (const trigger of remappedTriggers)
|
for (const trigger of remappedTriggers)
|
||||||
sqlString += `\nCREATE TRIGGER "${trigger.trigger_name}" ${trigger.action_timing} ${trigger.events.join(' OR ')} ON "${trigger.event_object_table}" FOR EACH ${trigger.action_orientation} ${trigger.action_statement};\n`;
|
sqlString += `\nCREATE TRIGGER "${trigger.trigger_name}" ${trigger.action_timing} ${trigger.events.join(' OR ')} ON "${trigger.event_object_table}" FOR EACH ${trigger.action_orientation} ${trigger.action_statement};\n`;
|
||||||
|
|
||||||
|
// Trigger functions
|
||||||
|
const { rows: triggerFunctions } = await this._client.raw(
|
||||||
|
`SELECT DISTINCT routine_name AS name FROM information_schema.routines WHERE routine_type = 'FUNCTION' AND routine_schema = '${this.schemaName}' AND data_type = 'trigger'`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const func of triggerFunctions) {
|
||||||
|
const { rows: functionDef } = await this._client.raw(
|
||||||
|
`SELECT pg_get_functiondef((SELECT oid FROM pg_proc WHERE proname = '${func.name}')) AS definition`
|
||||||
|
);
|
||||||
|
sqlString += `\n${functionDef[0].definition};\n`;
|
||||||
|
}
|
||||||
|
|
||||||
return sqlString;
|
return sqlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFunctions () {
|
async getFunctions () {
|
||||||
|
let sqlString = '';
|
||||||
const { rows: functions } = await this._client.raw(
|
const { rows: functions } = await this._client.raw(
|
||||||
`SHOW FUNCTION STATUS WHERE \`Db\` = '${this.schemaName}';`
|
`SELECT DISTINCT routine_name AS name FROM information_schema.routines WHERE routine_type = 'FUNCTION' AND routine_schema = '${this.schemaName}' AND data_type != 'trigger'`
|
||||||
);
|
);
|
||||||
|
|
||||||
let sqlString = '';
|
|
||||||
|
|
||||||
for (const func of functions) {
|
for (const func of functions) {
|
||||||
const definer = this.getEscapedDefiner(func.Definer);
|
const { rows: functionDef } = await this._client.raw(
|
||||||
sqlString += await this.getRoutineSyntax(
|
`SELECT pg_get_functiondef((SELECT oid FROM pg_proc WHERE proname = '${func.name}')) AS definition`
|
||||||
func.Name,
|
|
||||||
func.Type,
|
|
||||||
definer
|
|
||||||
);
|
);
|
||||||
|
sqlString += `\n${functionDef[0].definition};\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlString;
|
return sqlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoutines () {
|
async getRoutines () {
|
||||||
const { rows: routines } = await this._client.raw(
|
let sqlString = '';
|
||||||
`SHOW PROCEDURE STATUS WHERE \`Db\` = '${this.schemaName}';`
|
const { rows: functions } = await this._client.raw(
|
||||||
|
`SELECT DISTINCT routine_name AS name FROM information_schema.routines WHERE routine_type = 'PROCEDURE' AND routine_schema = '${this.schemaName}'`
|
||||||
);
|
);
|
||||||
|
|
||||||
let sqlString = '';
|
for (const func of functions) {
|
||||||
|
const { rows: functionDef } = await this._client.raw(
|
||||||
for (const routine of routines) {
|
`SELECT pg_get_functiondef((SELECT oid FROM pg_proc WHERE proname = '${func.name}')) AS definition`
|
||||||
const definer = this.getEscapedDefiner(routine.Definer);
|
|
||||||
|
|
||||||
sqlString += await this.getRoutineSyntax(
|
|
||||||
routine.Name,
|
|
||||||
routine.Type,
|
|
||||||
definer
|
|
||||||
);
|
);
|
||||||
|
sqlString += `\n${functionDef[0].definition};\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlString;
|
return sqlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoutineSyntax (name, type, definer) {
|
|
||||||
const { rows: routines } = await this._client.raw(
|
|
||||||
`SHOW CREATE ${type} \`${this.schemaName}\`.\`${name}\``
|
|
||||||
);
|
|
||||||
|
|
||||||
if (routines.length === 0) return '';
|
|
||||||
|
|
||||||
const routine = routines[0];
|
|
||||||
|
|
||||||
const fieldName = `Create ${type === 'PROCEDURE' ? 'Procedure' : 'Function'}`;
|
|
||||||
const sqlMode = routine.sql_mode;
|
|
||||||
const createProcedure = routine[fieldName];
|
|
||||||
let sqlString = '';
|
|
||||||
|
|
||||||
if (createProcedure) { // If procedure body not empty
|
|
||||||
const startOffset = createProcedure.indexOf(type);
|
|
||||||
const procedureBody = createProcedure.substring(startOffset);
|
|
||||||
|
|
||||||
sqlString += `/*!50003 DROP ${type} IF EXISTS ${name}*/;;\n`;
|
|
||||||
sqlString += '/*!50003 SET @OLD_SQL_MODE=@@SQL_MODE*/;;\n';
|
|
||||||
sqlString += `/*!50003 SET SQL_MODE="${sqlMode}"*/;;\n`;
|
|
||||||
sqlString += 'DELIMITER ;;\n';
|
|
||||||
sqlString += `/*!50003 CREATE*/ /*!50020 DEFINER=${definer}*/ /*!50003 ${procedureBody}*/;;\n`;
|
|
||||||
sqlString += 'DELIMITER ;\n';
|
|
||||||
sqlString += '/*!50003 SET SQL_MODE=@OLD_SQL_MODE*/;\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
return sqlString;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getCreateType () {}
|
|
||||||
|
|
||||||
async _queryStream (sql) {
|
async _queryStream (sql) {
|
||||||
if (process.env.NODE_ENV === 'development') console.log('EXPORTER:', sql);
|
if (process.env.NODE_ENV === 'development') console.log('EXPORTER:', sql);
|
||||||
const connection = await this._client.getConnection();
|
const connection = await this._client.getConnection();
|
||||||
|
@ -349,14 +349,12 @@ export default {
|
|||||||
includeDropStatement: true
|
includeDropStatement: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const structure = ['views', 'triggers', 'routines', 'functions', 'schedulers', 'triggerFunctions'];
|
const structure = ['views', 'triggers', 'routines', 'functions', 'schedulers'];
|
||||||
|
|
||||||
structure.forEach(feat => {
|
structure.forEach(feat => {
|
||||||
const val = customizations[this.currentWorkspace.client][feat];
|
const val = customizations[this.currentWorkspace.client][feat];
|
||||||
if (val) {
|
if (val)
|
||||||
if (feat === 'triggerFunctions') feat = 'triggerFunction';// TODO: remove after l18n refactor
|
|
||||||
this.$set(this.options.includes, feat, true);
|
this.$set(this.options.includes, feat, true);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on('export-progress', this.updateProgress);
|
ipcRenderer.on('export-progress', this.updateProgress);
|
||||||
|
Reference in New Issue
Block a user