Made MigrationRecorder cache has_table() result if django_migrations table exists.
This commit is contained in:
parent
9d756afb07
commit
ea8cbca579
@ -47,6 +47,7 @@ class MigrationRecorder:
|
|||||||
|
|
||||||
def __init__(self, connection):
|
def __init__(self, connection):
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
self._has_table = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def migration_qs(self):
|
def migration_qs(self):
|
||||||
@ -54,9 +55,16 @@ class MigrationRecorder:
|
|||||||
|
|
||||||
def has_table(self):
|
def has_table(self):
|
||||||
"""Return True if the django_migrations table exists."""
|
"""Return True if the django_migrations table exists."""
|
||||||
|
# If the migrations table has already been confirmed to exist, don't
|
||||||
|
# recheck it's existence.
|
||||||
|
if self._has_table:
|
||||||
|
return True
|
||||||
|
# It hasn't been confirmed to exist, recheck.
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
tables = self.connection.introspection.table_names(cursor)
|
tables = self.connection.introspection.table_names(cursor)
|
||||||
return self.Migration._meta.db_table in tables
|
|
||||||
|
self._has_table = self.Migration._meta.db_table in tables
|
||||||
|
return self._has_table
|
||||||
|
|
||||||
def ensure_schema(self):
|
def ensure_schema(self):
|
||||||
"""Ensure the table exists and has the correct schema."""
|
"""Ensure the table exists and has the correct schema."""
|
||||||
|
@ -48,6 +48,16 @@ class RecorderTests(TestCase):
|
|||||||
set(),
|
set(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_has_table_cached(self):
|
||||||
|
"""
|
||||||
|
The has_table() method caches a positive result and not continually
|
||||||
|
query for the existence of the migrations table.
|
||||||
|
"""
|
||||||
|
recorder = MigrationRecorder(connection)
|
||||||
|
with self.assertNumQueries(1):
|
||||||
|
self.assertEqual(recorder.has_table(), True)
|
||||||
|
self.assertEqual(recorder.has_table(), True)
|
||||||
|
|
||||||
|
|
||||||
class LoaderTests(TestCase):
|
class LoaderTests(TestCase):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user