Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Customize Form): add "Trim Table" action (backport #25923) #25934

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions frappe/custom/doctype/customize_form/customize_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ frappe.ui.form.on("Customize Form", {
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);

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

frm.events.setup_export(frm);
Expand All @@ -156,6 +164,40 @@ frappe.ui.form.on("Customize Form", {
}
},

async trim_table(frm) {
let dropped_columns = await frappe.xcall(
"frappe.custom.doctype.customize_form.customize_form.get_orphaned_columns",
{ doctype: frm.doc.doc_type }
);

if (!dropped_columns?.length) {
frappe.toast(__("This doctype has no orphan fields to trim"));
return;
}
let msg = __(
"Warning: DATA LOSS IMMINENT! Proceeding will permanently delete following database columns from doctype {0}:",
[frm.doc.doc_type.bold()]
);
msg += "<ol>" + dropped_columns.map((col) => `<li>${col}</li>`).join("") + "</ol>";
msg += __("This action is irreversible. Do you wish to continue?");

frappe.confirm(msg, () => {
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
21 changes: 21 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 @@ -553,6 +554,19 @@ def reset_to_defaults(self):
reset_customization(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 All @@ -565,6 +579,13 @@ def in_field_group(group):
return any(map(in_field_group, ALLOWED_FIELDTYPE_CHANGE))


@frappe.whitelist()
def get_orphaned_columns(doctype: str):
frappe.only_for("System Manager")
frappe.db.begin(read_only=True) # Avoid any potential bug from writing to db
return trim_table(doctype, dry_run=True)


def reset_customization(doctype):
setters = frappe.get_all(
"Property Setter",
Expand Down