Skip to content

Commit

Permalink
feat(Customize Form): add "Trim Table" action
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Apr 11, 2024
1 parent a674e91 commit 9238f46
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
31 changes: 31 additions & 0 deletions frappe/custom/doctype/customize_form/customize_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ frappe.ui.form.on("Customize Form", {
__("Actions")
);

frm.add_custom_button(
__("Trim Table"),
function () {
frm.trigger("trim_table");
},
__("Actions")
);

const is_autoname_autoincrement = frm.doc.autoname === "autoincrement";
frm.set_df_property("naming_rule", "hidden", is_autoname_autoincrement);
frm.set_df_property("autoname", "read_only", is_autoname_autoincrement);
Expand Down Expand Up @@ -194,6 +202,29 @@ frappe.ui.form.on("Customize Form", {
);
},

trim_table(frm) {
frappe.confirm(
__(
"Warning: DATA LOSS IMMINENT! Proceeding will permanently delete the database columns associated with fields that have been removed from this DocType. This action is irreversible. Do you wish to continue?"
),
() => {
return frm.call({
doc: frm.doc,
method: "trim_table",
callback: function (r) {
if (!r.exc) {
frappe.show_alert({
message: __("Table Trimmed"),
indicator: "green",
});
frappe.customize_form.clear_locals_and_refresh(frm);
}
},
});
}
);
},

setup_export(frm) {
if (frappe.boot.developer_mode) {
frm.add_custom_button(
Expand Down
14 changes: 14 additions & 0 deletions frappe/custom/doctype/customize_form/customize_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from frappe.model import core_doctypes_list, no_value_fields
from frappe.model.docfield import supports_translation
from frappe.model.document import Document
from frappe.model.meta import trim_table
from frappe.utils import cint


Expand Down Expand Up @@ -639,6 +640,19 @@ def reset_layout(self):
frappe.clear_cache(doctype=self.doc_type)
self.fetch_to_customize()

@frappe.whitelist()
def trim_table(self):
"""Removes database fields that don't exist in the doctype.
This may be needed as maintenance since removing a field in a DocType
doesn't automatically delete the db field.
"""
if not self.doc_type:
return

trim_table(self.doc_type, dry_run=False)
self.fetch_to_customize()

@classmethod
def allow_fieldtype_change(self, old_type: str, new_type: str) -> bool:
"""allow type change, if both old_type and new_type are in same field group.
Expand Down

0 comments on commit 9238f46

Please sign in to comment.