feat: Allow user to optimize tables (#26109)
* feat: Allow user to optimize tables * fix: Update butto label * fix: Filter out single & virtual doctypes * fix: Create long job for optimization * fix: check perm --------- Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
parent
27948bccc7
commit
3f0c2330fc
2 changed files with 69 additions and 0 deletions
|
|
@ -3,4 +3,49 @@
|
|||
|
||||
frappe.query_reports["Database Storage Usage By Tables"] = {
|
||||
filters: [],
|
||||
onload: function (report) {
|
||||
report.page.add_inner_button(
|
||||
__("Optimize"),
|
||||
function () {
|
||||
let d = new frappe.ui.Dialog({
|
||||
title: "Optimize Doctype",
|
||||
fields: [
|
||||
{
|
||||
label: "Select a DocType",
|
||||
fieldname: "doctype_name",
|
||||
fieldtype: "Link",
|
||||
options: "DocType",
|
||||
get_query: function () {
|
||||
return {
|
||||
filters: { issingle: ["=", false], is_virtual: ["=", false] },
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
size: "small",
|
||||
primary_action_label: "Optimize",
|
||||
primary_action(values) {
|
||||
frappe.call({
|
||||
method: "frappe.core.report.database_storage_usage_by_tables.database_storage_usage_by_tables.optimize_doctype",
|
||||
args: {
|
||||
doctype_name: values.doctype_name,
|
||||
},
|
||||
callback: function (r) {
|
||||
if (!r.exec) {
|
||||
frappe.show_alert(
|
||||
__(
|
||||
`${values.doctype_name} has been added to queue for optimization`
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
d.hide();
|
||||
},
|
||||
});
|
||||
d.show();
|
||||
},
|
||||
__("Actions")
|
||||
);
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -38,3 +38,27 @@ def execute(filters=None):
|
|||
as_dict=1,
|
||||
)
|
||||
return COLUMNS, data
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def optimize_doctype(doctype_name: str):
|
||||
frappe.only_for("System Manager")
|
||||
frappe.enqueue(
|
||||
optimize_doctype_job,
|
||||
queue="long",
|
||||
job_id=f"optimize-{doctype_name}",
|
||||
doctype_name=doctype_name,
|
||||
deduplicate=True,
|
||||
)
|
||||
|
||||
|
||||
def optimize_doctype_job(doctype_name: str):
|
||||
from frappe.utils import get_table_name
|
||||
|
||||
doctype_table = get_table_name(doctype_name, wrap_in_backticks=True)
|
||||
if frappe.db.db_type == "mariadb":
|
||||
query = f"OPTIMIZE TABLE {doctype_table};"
|
||||
else:
|
||||
query = f"VACUUM (ANALYZE) {doctype_table};"
|
||||
|
||||
frappe.db.sql(query)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue