fix: Set creation dates of standard DocTypes correctlyversion-14
@@ -471,7 +471,7 @@ class Document(BaseDocument): | |||||
# We'd probably want the creation and owner to be set via API | # 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 | # 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.creation = self.modified | ||||
self.owner = self.modified_by | self.owner = self.modified_by | ||||
@@ -37,6 +37,7 @@ patches by using INI like file format: | |||||
import configparser | import configparser | ||||
import time | import time | ||||
from enum import Enum | from enum import Enum | ||||
from textwrap import dedent, indent | |||||
from typing import List, Optional | from typing import List, Optional | ||||
import frappe | 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): | def execute_patch(patchmodule, method=None, methodargs=None): | ||||
"""execute the patch""" | """execute the patch""" | ||||
block_user(True) | 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() | start_time = time.time() | ||||
frappe.db.begin() | |||||
try: | 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: | ||||
if patchmodule.startswith("finally:"): | if patchmodule.startswith("finally:"): | ||||
# run run patch at the end | # run run patch at the end | ||||
frappe.flags.final_patches.append(patchmodule) | frappe.flags.final_patches.append(patchmodule) | ||||
else: | else: | ||||
if patchmodule.startswith("execute:"): | |||||
exec(patchmodule.split("execute:")[1],globals()) | |||||
if has_patch_file: | |||||
_patch() | |||||
else: | else: | ||||
frappe.get_attr(patchmodule.split()[0] + ".execute")() | |||||
exec(patch, globals()) | |||||
update_patch_log(patchmodule) | update_patch_log(patchmodule) | ||||
elif method: | elif method: | ||||
method(**methodargs) | method(**methodargs) | ||||
@@ -174,7 +190,7 @@ def execute_patch(patchmodule, method=None, methodargs=None): | |||||
frappe.db.commit() | frappe.db.commit() | ||||
end_time = time.time() | end_time = time.time() | ||||
block_user(False) | 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 | return True | ||||
@@ -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.save_ratings_in_fraction #23-12-2021 | ||||
frappe.patches.v14_0.transform_todo_schema | frappe.patches.v14_0.transform_todo_schema | ||||
frappe.patches.v14_0.remove_post_and_post_comment | frappe.patches.v14_0.remove_post_and_post_comment | ||||
frappe.patches.v14_0.reset_creation_datetime | |||||
[post_model_sync] | [post_model_sync] | ||||
frappe.patches.v14_0.drop_data_import_legacy | frappe.patches.v14_0.drop_data_import_legacy | ||||
@@ -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() |