Parcourir la source

Merge pull request #14216 from netchampfaris/system-console-sql

feat: Run SQL directly in System Console
version-14
Faris Ansari il y a 3 ans
committed by GitHub
Parent
révision
d7c421d7a1
Aucune clé connue n'a été trouvée dans la base pour cette signature ID de la clé GPG: 4AEE18F83AFDEB23
4 fichiers modifiés avec 66 ajouts et 8 suppressions
  1. +43
    -4
      frappe/desk/doctype/system_console/system_console.js
  2. +16
    -1
      frappe/desk/doctype/system_console/system_console.json
  3. +6
    -3
      frappe/desk/doctype/system_console/system_console.py
  4. +1
    -0
      frappe/public/js/frappe/views/reports/report_view.js

+ 43
- 4
frappe/desk/doctype/system_console/system_console.js Voir le fichier

@@ -10,18 +10,56 @@ frappe.ui.form.on('System Console', {
description: __('Execute Console script'),
ignore_inputs: true,
});
frm.set_value("type", "Python");
},

refresh: function(frm) {
frm.disable_save();
frm.page.set_primary_action(__("Execute"), $btn => {
$btn.text(__('Executing...'));
return frm.execute_action("Execute").then(() => {
$btn.text(__('Execute'));
});
$btn.text(__("Executing..."));
return frm
.execute_action("Execute")
.then(() => frm.trigger("render_sql_output"))
.finally(() => $btn.text(__("Execute")));
});
},

type: function(frm) {
if (frm.doc.type == "Python") {
frm.set_value("output", "");
if (frm.sql_output) {
frm.sql_output.destroy();
frm.get_field("sql_output").html("");
}
}
},

render_sql_output: function(frm) {
if (frm.doc.type !== "SQL") return;
if (frm.sql_output) {
frm.sql_output.destroy();
frm.get_field("sql_output").html("");
}

if (frm.doc.output.startsWith("Traceback")) {
return;
}

let result = JSON.parse(frm.doc.output);
frm.set_value("output", `${result.length} ${result.length == 1 ? 'row' : 'rows'}`);

if (result.length) {
let columns = Object.keys(result[0]);
frm.sql_output = new DataTable(
frm.get_field("sql_output").$wrapper.get(0),
{
columns,
data: result
}
);
}
},

show_processlist: function(frm) {
if (frm.doc.show_processlist) {
// keep refreshing every 5 seconds
@@ -32,6 +70,7 @@ frappe.ui.form.on('System Console', {

// end it
clearInterval(frm.processlist_interval);
frm.get_field("processlist").html('');
}
}
},


+ 16
- 1
frappe/desk/doctype/system_console/system_console.json Voir le fichier

@@ -18,9 +18,11 @@
"engine": "InnoDB",
"field_order": [
"execute_section",
"type",
"console",
"commit",
"output",
"sql_output",
"database_processes_section",
"show_processlist",
"processlist"
@@ -65,13 +67,26 @@
"fieldname": "processlist",
"fieldtype": "HTML",
"label": "processlist"
},
{
"default": "Python",
"fieldname": "type",
"fieldtype": "Select",
"label": "Type",
"options": "Python\nSQL"
},
{
"depends_on": "eval:doc.type == 'SQL'",
"fieldname": "sql_output",
"fieldtype": "HTML",
"label": "SQL Output"
}
],
"hide_toolbar": 1,
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2021-09-09 13:10:14.237113",
"modified": "2021-09-15 17:17:44.844767",
"modified_by": "Administrator",
"module": "Desk",
"name": "System Console",


+ 6
- 3
frappe/desk/doctype/system_console/system_console.py Voir le fichier

@@ -5,7 +5,7 @@
import json

import frappe
from frappe.utils.safe_exec import safe_exec
from frappe.utils.safe_exec import safe_exec, read_sql
from frappe.model.document import Document

class SystemConsole(Document):
@@ -13,8 +13,11 @@ class SystemConsole(Document):
frappe.only_for('System Manager')
try:
frappe.debug_log = []
safe_exec(self.console)
self.output = '\n'.join(frappe.debug_log)
if self.type == 'Python':
safe_exec(self.console)
self.output = '\n'.join(frappe.debug_log)
elif self.type == 'SQL':
self.output = frappe.as_json(read_sql(self.console, as_dict=1))
except: # noqa: E722
self.output = frappe.get_traceback()



+ 1
- 0
frappe/public/js/frappe/views/reports/report_view.js Voir le fichier

@@ -3,6 +3,7 @@
*/
import DataTable from 'frappe-datatable';

window.DataTable = DataTable;
frappe.provide('frappe.views');

frappe.views.ReportView = class ReportView extends frappe.views.ListView {


Chargement…
Annuler
Enregistrer