Skip to content

Commit 4d381c4

Browse files
committed
[IMP] util/pg: cache column_exists for common columns
In many cases we know that columns must exist, it's better to cache them to avoid thousands of small queries.
1 parent 86409e5 commit 4d381c4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/util/pg.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Utility functions for interacting with PostgreSQL."""
33

44
import collections
5+
import functools
56
import logging
67
import os
78
import re
@@ -43,7 +44,7 @@
4344

4445
from .exceptions import MigrationError, SleepyDeveloperError
4546
from .helpers import _validate_table, model_of_table
46-
from .misc import Sentinel, log_progress
47+
from .misc import Sentinel, log_progress, version_gte
4748

4849
_logger = logging.getLogger(__name__)
4950

@@ -472,6 +473,29 @@ def _column_info(cr, table, column):
472473
return cr.fetchone()
473474

474475

476+
_COLUMNS = {}
477+
if version_gte("10.0"): # before 10.0 it could be arch or arch_db, because it changed in 9.0
478+
_COLUMNS[("ir_ui_view", "arch_db")] = True
479+
if not version_gte("8.0"): # before 8.0 we know it was arch
480+
_COLUMNS[("ir_ui_view", "arch_db")] = False
481+
if version_gte("14.0"): # new from 13.0
482+
_COLUMNS[("ir_module_module_dependency", "auto_install_required")] = False
483+
484+
485+
def _forced_cache(cache):
486+
def decorator(f):
487+
@functools.wraps(f)
488+
def wrapper(cr, *args, **kwargs):
489+
if not kwargs and tuple(args) in cache:
490+
return cache[tuple(args)]
491+
return f(cr, *args, **kwargs)
492+
493+
return wrapper
494+
495+
return decorator
496+
497+
498+
@_forced_cache(_COLUMNS)
475499
def column_exists(cr, table, column):
476500
"""
477501
Return whether a column exists.

0 commit comments

Comments
 (0)