2013-12-24 19:25:17 +08:00
|
|
|
from django.apps.registry import Apps
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db import DatabaseError, models
|
2019-10-19 03:00:34 +08:00
|
|
|
from django.utils.functional import classproperty
|
2013-07-26 23:46:48 +08:00
|
|
|
from django.utils.timezone import now
|
2013-05-10 23:00:55 +08:00
|
|
|
|
2015-05-09 23:01:40 +08:00
|
|
|
from .exceptions import MigrationSchemaMissing
|
|
|
|
|
2013-05-10 23:00:55 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MigrationRecorder:
|
2013-05-10 23:00:55 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Deal with storing migration records in the database.
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
Because this table is actually itself used for dealing with model
|
2014-12-27 01:34:26 +08:00
|
|
|
creation, it's the one thing we can't do normally via migrations.
|
2013-05-10 23:00:55 +08:00
|
|
|
We manually handle table creation/schema updating (using schema backend)
|
|
|
|
and then have a floating model to do queries with.
|
|
|
|
|
|
|
|
If a migration is unapplied its row is removed from the table. Having
|
|
|
|
a row in the table always means a migration is applied.
|
|
|
|
"""
|
2019-01-23 06:49:30 +08:00
|
|
|
_migration_class = None
|
2013-05-10 23:00:55 +08:00
|
|
|
|
2019-01-23 06:49:30 +08:00
|
|
|
@classproperty
|
|
|
|
def Migration(cls):
|
|
|
|
"""
|
|
|
|
Lazy load to avoid AppRegistryNotReady if installed apps import
|
|
|
|
MigrationRecorder.
|
|
|
|
"""
|
|
|
|
if cls._migration_class is None:
|
|
|
|
class Migration(models.Model):
|
|
|
|
app = models.CharField(max_length=255)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
applied = models.DateTimeField(default=now)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2019-01-23 06:49:30 +08:00
|
|
|
class Meta:
|
|
|
|
apps = Apps()
|
|
|
|
app_label = 'migrations'
|
|
|
|
db_table = 'django_migrations'
|
2013-05-10 23:00:55 +08:00
|
|
|
|
2019-01-23 06:49:30 +08:00
|
|
|
def __str__(self):
|
|
|
|
return 'Migration %s for %s' % (self.name, self.app)
|
|
|
|
|
|
|
|
cls._migration_class = Migration
|
|
|
|
return cls._migration_class
|
2014-04-30 22:53:20 +08:00
|
|
|
|
2013-05-10 23:00:55 +08:00
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
|
|
|
|
2014-04-30 22:53:20 +08:00
|
|
|
@property
|
|
|
|
def migration_qs(self):
|
|
|
|
return self.Migration.objects.using(self.connection.alias)
|
|
|
|
|
2016-11-05 16:16:23 +08:00
|
|
|
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())
|
|
|
|
|
2013-05-10 23:00:55 +08:00
|
|
|
def ensure_schema(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Ensure the table exists and has the correct schema."""
|
2013-05-10 23:00:55 +08:00
|
|
|
# If the table's there, that's fine - we've never changed its schema
|
|
|
|
# in the codebase.
|
2016-11-05 16:16:23 +08:00
|
|
|
if self.has_table():
|
2013-05-10 23:00:55 +08:00
|
|
|
return
|
|
|
|
# Make the table
|
2015-05-09 23:01:40 +08:00
|
|
|
try:
|
|
|
|
with self.connection.schema_editor() as editor:
|
|
|
|
editor.create_model(self.Migration)
|
|
|
|
except DatabaseError as exc:
|
|
|
|
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
def applied_migrations(self):
|
2019-03-08 08:36:55 +08:00
|
|
|
"""
|
|
|
|
Return a dict mapping (app_name, migration_name) to Migration instances
|
|
|
|
for all applied migrations.
|
|
|
|
"""
|
2016-11-05 16:16:23 +08:00
|
|
|
if self.has_table():
|
2019-03-08 08:36:55 +08:00
|
|
|
return {(migration.app, migration.name): migration for migration in self.migration_qs}
|
2016-11-05 16:16:23 +08:00
|
|
|
else:
|
2017-06-20 03:24:41 +08:00
|
|
|
# If the django_migrations table doesn't exist, then no migrations
|
2016-11-05 16:16:23 +08:00
|
|
|
# are applied.
|
2019-03-08 08:36:55 +08:00
|
|
|
return {}
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
def record_applied(self, app, name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Record that a migration was applied."""
|
2013-05-10 23:00:55 +08:00
|
|
|
self.ensure_schema()
|
2014-04-30 22:53:20 +08:00
|
|
|
self.migration_qs.create(app=app, name=name)
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
def record_unapplied(self, app, name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Record that a migration was unapplied."""
|
2013-05-10 23:00:55 +08:00
|
|
|
self.ensure_schema()
|
2014-04-30 22:53:20 +08:00
|
|
|
self.migration_qs.filter(app=app, name=name).delete()
|
2013-06-19 22:36:22 +08:00
|
|
|
|
2014-04-30 22:53:20 +08:00
|
|
|
def flush(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Delete all migration records. Useful for testing migrations."""
|
2014-04-30 22:53:20 +08:00
|
|
|
self.migration_qs.all().delete()
|