1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #27858 -- Prevented read-only management commands from creating the django_migrations table.

MigrationRecorder now assumes that if the django_migrations table
doesn't exist, then no migrations are applied.

Reverted documentation change from refs #23808.
This commit is contained in:
Marti Raudsepp
2016-11-05 10:16:23 +02:00
committed by Tim Graham
parent 4f1eb64ad0
commit fda55c71a8
7 changed files with 30 additions and 32 deletions

View File

@@ -86,6 +86,10 @@ class MigrationExecutor:
Django first needs to create all project states before a migration is
(un)applied and in a second step run all the database operations.
"""
# The django_migrations table must be present to record applied
# migrations.
self.recorder.ensure_schema()
if plan is None:
plan = self.migration_plan(targets)
# Create the forwards plan Django would follow on an empty database

View File

@@ -39,11 +39,15 @@ class MigrationRecorder:
def migration_qs(self):
return self.Migration.objects.using(self.connection.alias)
def has_table(self):
"""Return True if the django_migrations table exists."""
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
def ensure_schema(self):
"""Ensure the table exists and has the correct schema."""
# If the table's there, that's fine - we've never changed its schema
# in the codebase.
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
if self.has_table():
return
# Make the table
try:
@@ -54,8 +58,12 @@ class MigrationRecorder:
def applied_migrations(self):
"""Return a set of (app, name) of applied migrations."""
self.ensure_schema()
return {tuple(x) for x in self.migration_qs.values_list("app", "name")}
if self.has_table():
return {tuple(x) for x in self.migration_qs.values_list('app', 'name')}
else:
# If the django_migrations table doesn't eixst, then no migrations
# are applied.
return set()
def record_applied(self, app, name):
"""Record that a migration was applied."""