Skip to content

Commit

Permalink
fix: autoincr caching and clear site cache after restore (#22079) (#2…
Browse files Browse the repository at this point in the history
…2087)

* fix: remove hazardous cache for autoincr

* fix: move cache to redis

* fix: clear all redis cache after restoring a site

(cherry picked from commit 7939226)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Aug 17, 2023
1 parent 6bac8a8 commit af6393f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 2 additions & 0 deletions frappe/database/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ def restore_database(target, source, user, password):
source=source,
port=frappe.db.port,
)

os.system(command)
frappe.cache().delete_keys("") # Delete all keys associated with this site.
27 changes: 12 additions & 15 deletions frappe/model/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
from frappe.model import log_types
from frappe.query_builder import DocType
from frappe.utils import cint, cstr, now_datetime
from frappe.utils.caching import redis_cache

if TYPE_CHECKING:
from frappe.model.document import Document
from frappe.model.meta import Meta


# NOTE: This is used to keep track of status of sites
# whether `log_types` have autoincremented naming set for the site or not.
# Structure: {"sitename": {"doctype": 1}}
autoincremented_site_status_map = defaultdict(dict)

NAMING_SERIES_PATTERN = re.compile(r"^[\w\- \/.#{}]+$", re.UNICODE)
BRACED_PARAMS_PATTERN = re.compile(r"(\{[\w | #]+\})")

Expand Down Expand Up @@ -180,16 +176,7 @@ def is_autoincremented(doctype: str, meta: Optional["Meta"] = None) -> bool:
"""Checks if the doctype has autoincrement autoname set"""

if doctype in log_types:
site_map = autoincremented_site_status_map[frappe.local.site]
if site_map.get(doctype) is None:
query = f"""select data_type FROM information_schema.columns where column_name = 'name' and table_name = 'tab{doctype}'"""
values = ()
if frappe.db.db_type == "mariadb":
query += " and table_schema = %s"
values = (frappe.db.db_name,)
site_map[doctype] = frappe.db.sql(query, values)[0][0] == "bigint"

return bool(site_map[doctype])
return _implicitly_auto_incremented(doctype)
else:
if not meta:
meta = frappe.get_meta(doctype)
Expand All @@ -200,6 +187,16 @@ def is_autoincremented(doctype: str, meta: Optional["Meta"] = None) -> bool:
return False


@redis_cache
def _implicitly_auto_incremented(doctype) -> bool:
query = f"""select data_type FROM information_schema.columns where column_name = 'name' and table_name = 'tab{doctype}'"""
values = ()
if frappe.db.db_type == "mariadb":
query += " and table_schema = %s"
values = (frappe.db.db_name,)
return frappe.db.sql(query, values)[0][0] == "bigint"


def set_name_from_naming_options(autoname, doc):
"""
Get a name based on the autoname field option
Expand Down

0 comments on commit af6393f

Please sign in to comment.