diff --git a/frappe/model/document.py b/frappe/model/document.py index 96f933179c..dc0fd2caf0 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -471,7 +471,7 @@ class Document(BaseDocument): # We'd probably want the creation and owner to be set via API # or Data import at some point, that'd have to be handled here - if self.is_new() and not (frappe.flags.in_patch or frappe.flags.in_migrate): + if self.is_new() and not (frappe.flags.in_install or frappe.flags.in_patch or frappe.flags.in_migrate): self.creation = self.modified self.owner = self.modified_by diff --git a/frappe/modules/patch_handler.py b/frappe/modules/patch_handler.py index 7b635ac940..0a23d5b0f4 100644 --- a/frappe/modules/patch_handler.py +++ b/frappe/modules/patch_handler.py @@ -37,6 +37,7 @@ patches by using INI like file format: import configparser import time from enum import Enum +from textwrap import dedent, indent from typing import List, Optional import frappe @@ -148,21 +149,36 @@ def run_single(patchmodule=None, method=None, methodargs=None, force=False): def execute_patch(patchmodule, method=None, methodargs=None): """execute the patch""" block_user(True) - frappe.db.begin() + + if patchmodule.startswith("execute:"): + has_patch_file = False + patch = patchmodule.split("execute:")[1] + docstring = "" + else: + has_patch_file = True + patch = f"{patchmodule.split()[0]}.execute" + _patch = frappe.get_attr(patch) + docstring = _patch.__doc__ or "" + + if docstring: + docstring = "\n" + indent(dedent(docstring), "\t") + + print(f"Executing {patchmodule or methodargs} in {frappe.local.site} ({frappe.db.cur_db_name}){docstring}") + start_time = time.time() + frappe.db.begin() try: - print('Executing {patch} in {site} ({db})'.format(patch=patchmodule or str(methodargs), - site=frappe.local.site, db=frappe.db.cur_db_name)) if patchmodule: if patchmodule.startswith("finally:"): # run run patch at the end frappe.flags.final_patches.append(patchmodule) else: - if patchmodule.startswith("execute:"): - exec(patchmodule.split("execute:")[1],globals()) + if has_patch_file: + _patch() else: - frappe.get_attr(patchmodule.split()[0] + ".execute")() + exec(patch, globals()) update_patch_log(patchmodule) + elif method: method(**methodargs) @@ -174,7 +190,7 @@ def execute_patch(patchmodule, method=None, methodargs=None): frappe.db.commit() end_time = time.time() block_user(False) - print('Success: Done in {time}s'.format(time = round(end_time - start_time, 3))) + print(f"Success: Done in {round(end_time - start_time, 3)}s") return True diff --git a/frappe/patches.txt b/frappe/patches.txt index 0d2a6162c2..c889d9a4da 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -189,6 +189,7 @@ frappe.patches.v14_0.update_workspace2 # 20.09.2021 frappe.patches.v14_0.save_ratings_in_fraction #23-12-2021 frappe.patches.v14_0.transform_todo_schema frappe.patches.v14_0.remove_post_and_post_comment +frappe.patches.v14_0.reset_creation_datetime [post_model_sync] frappe.patches.v14_0.drop_data_import_legacy diff --git a/frappe/patches/v14_0/reset_creation_datetime.py b/frappe/patches/v14_0/reset_creation_datetime.py new file mode 100644 index 0000000000..54eb6c65af --- /dev/null +++ b/frappe/patches/v14_0/reset_creation_datetime.py @@ -0,0 +1,41 @@ +import glob +import json +import frappe +import os +from frappe.query_builder import DocType as _DocType + + +def execute(): + """Resetting creation datetimes for DocTypes""" + DocType = _DocType("DocType") + doctype_jsons = glob.glob( + os.path.join("..", "apps", "frappe", "frappe", "**", "doctype", "**", "*.json") + ) + + frappe_modules = frappe.get_all( + "Module Def", filters={"app_name": "frappe"}, pluck="name" + ) + site_doctypes = frappe.get_all( + "DocType", + filters={"module": ("in", frappe_modules), "custom": False}, + fields=["name", "creation"], + ) + + for dt_path in doctype_jsons: + with open(dt_path) as f: + try: + file_schema = frappe._dict(json.load(f)) + except Exception: + continue + + if not file_schema.name: + continue + + _site_schema = [x for x in site_doctypes if x.name == file_schema.name] + if not _site_schema: + continue + + if file_schema.creation != _site_schema[0].creation: + frappe.qb.update(DocType).set( + DocType.creation, file_schema.creation + ).where(DocType.name == file_schema.name).run()