fix: multiple postgres transaction abort issues

- wrap setup fixtures in savepoint
- wrap scheduled_job_type in savepoint
- ignore duplicates where it's ignored by exc
- dont attempt to delete from deleted table
- delete custom field and commit - stale meta causes future inserts to
  insert unknown field.
This commit is contained in:
Ankush Menat 2022-04-21 20:37:40 +05:30
parent 8861752675
commit 359c7768f5
6 changed files with 13 additions and 19 deletions

View file

@ -185,9 +185,12 @@ def insert_single_event(frequency: str, event: str, cron_format: str = None):
if not frappe.db.exists(
"Scheduled Job Type", {"method": event, "frequency": frequency, **cron_expr}
):
savepoint = "scheduled_job_type_creation"
try:
frappe.db.savepoint(savepoint)
doc.insert()
except frappe.DuplicateEntryError:
frappe.db.rollback(save_point=savepoint)
doc.delete()
doc.insert()

View file

@ -161,6 +161,7 @@ def create_custom_field(doctype, df, ignore_validate=False, is_system_generated=
custom_field.update(df)
custom_field.flags.ignore_validate = ignore_validate
custom_field.insert()
return custom_field
def create_custom_fields(custom_fields, ignore_validate=False, update=True):

View file

@ -431,22 +431,12 @@ def make_records(records, debug=False):
if doc.meta.get_field(parent_link_field) and not doc.get(parent_link_field):
doc.flags.ignore_mandatory = True
savepoint = "setup_fixtures_creation"
try:
doc.insert(ignore_permissions=True)
frappe.db.commit()
except frappe.DuplicateEntryError as e:
# print("Failed to insert duplicate {0} {1}".format(doctype, doc.name))
# pass DuplicateEntryError and continue
if e.args and e.args[0] == doc.doctype and e.args[1] == doc.name:
# make sure DuplicateEntryError is for the exact same doc and not a related doc
frappe.clear_messages()
else:
raise
frappe.db.savepoint(savepoint)
doc.insert(ignore_permissions=True, ignore_if_duplicate=True)
except Exception as e:
frappe.db.rollback()
frappe.db.rollback(save_point=savepoint)
exception = record.get("__exception")
if exception:
config = _dict(exception)

View file

@ -72,7 +72,7 @@ class TestNewsletterMixin:
"doctype": doctype,
**email_filters,
}
).insert()
).insert(ignore_if_duplicate=True)
except Exception:
frappe.db.rollback(save_point=savepoint)
frappe.db.update(doctype, email_filters, "unsubscribed", 0)

View file

@ -182,10 +182,12 @@ class TestDB(unittest.TestCase):
self.assertIn("tabToDo", frappe.flags.touched_tables)
frappe.flags.touched_tables = set()
create_custom_field("ToDo", {"label": "ToDo Custom Field"})
cf = create_custom_field("ToDo", {"label": "ToDo Custom Field"})
self.assertIn("tabToDo", frappe.flags.touched_tables)
self.assertIn("tabCustom Field", frappe.flags.touched_tables)
if cf:
cf.delete()
frappe.db.commit()
frappe.flags.in_migrate = False
frappe.flags.touched_tables.clear()

View file

@ -100,8 +100,6 @@ class TestRenameDoc(unittest.TestCase):
frappe.delete_doc("DocType", dt)
frappe.db.sql_ddl(f"DROP TABLE IF EXISTS `tab{dt}`")
frappe.delete_doc_if_exists("Renamed Doc", "ToDo")
# reset original value of developer_mode conf
frappe.conf.developer_mode = self._original_developer_flag