Allows specifying a schema name in Model._meta.db_table.

This commit is contained in:
Bertrand Bordage 2018-06-14 15:23:17 +02:00
parent f136d72a20
commit 66f6e2003c
2 changed files with 20 additions and 6 deletions

View file

@ -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(

View file

@ -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