From 66f6e2003c5104b5aa8c2e564f1aeeb7abb2378e Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Thu, 14 Jun 2018 15:23:17 +0200 Subject: [PATCH] Allows specifying a schema name in Model._meta.db_table. --- cachalot/api.py | 15 ++++++++++----- cachalot/tests/test_utils.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cachalot/api.py b/cachalot/api.py index 852a90d..7a18273 100644 --- a/cachalot/api.py +++ b/cachalot/api.py @@ -30,9 +30,14 @@ def _cache_db_tables_iterator(tables, cache_alias, db_alias): def _get_tables(tables_or_models): - return [(apps.get_model(o)._meta.db_table if '.' in o else o) - if isinstance(o, string_types) else o._meta.db_table - for o in tables_or_models] + for table_or_model in tables_or_models: + if isinstance(table_or_model, string_types) and '.' in table_or_model: + try: + table_or_model = apps.get_model(table_or_model) + except LookupError: + pass + yield (table_or_model if isinstance(table_or_model, string_types) + else table_or_model._meta.db_table) def invalidate(*tables_or_models, **kwargs): @@ -68,7 +73,7 @@ def invalidate(*tables_or_models, **kwargs): send_signal = False invalidated = set() for cache_alias, db_alias, tables in _cache_db_tables_iterator( - _get_tables(tables_or_models), cache_alias, db_alias): + list(_get_tables(tables_or_models)), cache_alias, db_alias): cache = cachalot_caches.get_cache(cache_alias, db_alias) if not isinstance(cache, AtomicCache): send_signal = True @@ -111,7 +116,7 @@ def get_last_invalidation(*tables_or_models, **kwargs): last_invalidation = 0.0 for cache_alias, db_alias, tables in _cache_db_tables_iterator( - _get_tables(tables_or_models), cache_alias, db_alias): + list(_get_tables(tables_or_models)), cache_alias, db_alias): get_table_cache_key = cachalot_settings.CACHALOT_TABLE_KEYGEN table_cache_keys = [get_table_cache_key(db_alias, t) for t in tables] invalidations = cachalot_caches.get_cache( diff --git a/cachalot/tests/test_utils.py b/cachalot/tests/test_utils.py index 5bead5c..f538bb7 100644 --- a/cachalot/tests/test_utils.py +++ b/cachalot/tests/test_utils.py @@ -1,6 +1,8 @@ -from django.db import connection +from django.core.management.color import no_style +from django.db import connection, DEFAULT_DB_ALIAS from django.utils.six import string_types +from .models import PostgresModel from ..utils import _get_tables @@ -10,6 +12,13 @@ class TestUtilsMixin: self.is_mysql = connection.vendor == 'mysql' self.force_repoen_connection() + # TODO: Remove this workaround when this issue is fixed: + # https://code.djangoproject.com/ticket/29494 + def tearDown(self): + flush_sql_list = connection.ops.sql_flush( + no_style(), (PostgresModel._meta.db_table,), ()) + connection.ops.execute_sql_flush(DEFAULT_DB_ALIAS, flush_sql_list) + def force_repoen_connection(self): if connection.vendor in ('mysql', 'postgresql'): # We need to reopen the connection or Django