| 1 |
from django.apps.registry import Apps |
| 2 |
from django.db import DatabaseError, models |
| 3 |
from django.utils.functional import classproperty |
| 4 |
from django.utils.timezone import now |
| 5 |
|
| 6 |
from .exceptions import MigrationSchemaMissing |
| 7 |
|
| 8 |
|
| 9 |
class MigrationRecorder: |
| 10 |
""" |
| 11 |
Deal with storing migration records in the database. |
| 12 |
|
| 13 |
Because this table is actually itself used for dealing with model |
| 14 |
creation, it's the one thing we can't do normally via migrations. |
| 15 |
We manually handle table creation/schema updating (using schema backend) |
| 16 |
and then have a floating model to do queries with. |
| 17 |
|
| 18 |
If a migration is unapplied its row is removed from the table. Having |
| 19 |
a row in the table always means a migration is applied. |
| 20 |
""" |
| 21 |
|
| 22 |
_migration_class = None |
| 23 |
|
| 24 |
@classproperty |
| 25 |
def Migration(cls): |
| 26 |
""" |
| 27 |
Lazy load to avoid AppRegistryNotReady if installed apps import |
| 28 |
MigrationRecorder. |
| 29 |
""" |
| 30 |
if cls._migration_class is None: |
| 31 |
|
| 32 |
class Migration(models.Model): |
| 33 |
app = models.CharField(max_length=255) |
| 34 |
name = models.CharField(max_length=255) |
| 35 |
applied = models.DateTimeField(default=now) |
| 36 |
|
| 37 |
class Meta: |
| 38 |
apps = Apps() |
| 39 |
app_label = "migrations" |
| 40 |
db_table = "django_migrations" |
| 41 |
|
| 42 |
def __str__(self): |
| 43 |
return "Migration %s for %s" % (self.name, self.app) |
| 44 |
|
| 45 |
cls._migration_class = Migration |
| 46 |
return cls._migration_class |
| 47 |
|
| 48 |
def __init__(self, connection): |
| 49 |
self.connection = connection |
| 50 |
self._has_table = False |
| 51 |
|
| 52 |
@property |
| 53 |
def migration_qs(self): |
| 54 |
return self.Migration.objects.using(self.connection.alias) |
| 55 |
|
| 56 |
def has_table(self): |
| 57 |
"""Return True if the django_migrations table exists.""" |
| 58 |
# If the migrations table has already been confirmed to exist, don't |
| 59 |
# recheck it's existence. |
| 60 |
if self._has_table: |
| 61 |
return True |
| 62 |
# It hasn't been confirmed to exist, recheck. |
| 63 |
with self.connection.cursor() as cursor: |
| 64 |
tables = self.connection.introspection.table_names(cursor) |
| 65 |
|
| 66 |
self._has_table = self.Migration._meta.db_table in tables |
| 67 |
return self._has_table |
| 68 |
|
| 69 |
def ensure_schema(self): |
| 70 |
"""Ensure the table exists and has the correct schema.""" |
| 71 |
# If the table's there, that's fine - we've never changed its schema |
| 72 |
# in the codebase. |
| 73 |
if self.has_table(): |
| 74 |
return |
| 75 |
# Make the table |
| 76 |
try: |
| 77 |
with self.connection.schema_editor() as editor: |
| 78 |
editor.create_model(self.Migration) |
| 79 |
except DatabaseError as exc: |
| 80 |
raise MigrationSchemaMissing( |
| 81 |
"Unable to create the django_migrations table (%s)" % exc |
| 82 |
) |
| 83 |
|
| 84 |
def applied_migrations(self): |
| 85 |
""" |
| 86 |
Return a dict mapping (app_name, migration_name) to Migration instances |
| 87 |
for all applied migrations. |
| 88 |
""" |
| 89 |
if self.has_table(): |
| 90 |
return { |
| 91 |
(migration.app, migration.name): migration |
| 92 |
for migration in self.migration_qs |
| 93 |
} |
| 94 |
else: |
| 95 |
# If the django_migrations table doesn't exist, then no migrations |
| 96 |
# are applied. |
| 97 |
return {} |
| 98 |
|
| 99 |
def record_applied(self, app, name): |
| 100 |
"""Record that a migration was applied.""" |
| 101 |
self.ensure_schema() |
| 102 |
self.migration_qs.create(app=app, name=name) |
| 103 |
|
| 104 |
def record_unapplied(self, app, name): |
| 105 |
"""Record that a migration was unapplied.""" |
| 106 |
self.ensure_schema() |
| 107 |
self.migration_qs.filter(app=app, name=name).delete() |
| 108 |
|
| 109 |
def flush(self): |
| 110 |
"""Delete all migration records. Useful for testing migrations.""" |
| 111 |
self.migration_qs.all().delete() |