2013-12-24 19:25:17 +08:00
|
|
|
from django.apps.registry import Apps
|
2013-05-10 23:00:55 +08:00
|
|
|
from django.db import models
|
2015-05-09 23:01:40 +08:00
|
|
|
from django.db.utils import DatabaseError
|
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
|
|
|
"""
|
|
|
|
Deals with storing migration records in the database.
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Migration(models.Model):
|
|
|
|
app = models.CharField(max_length=255)
|
|
|
|
name = models.CharField(max_length=255)
|
2013-07-26 23:46:48 +08:00
|
|
|
applied = models.DateTimeField(default=now)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-05-10 23:00:55 +08:00
|
|
|
class Meta:
|
2013-12-24 19:25:17 +08:00
|
|
|
apps = Apps()
|
2013-05-10 23:00:55 +08:00
|
|
|
app_label = "migrations"
|
|
|
|
db_table = "django_migrations"
|
|
|
|
|
2014-04-30 22:53:20 +08:00
|
|
|
def __str__(self):
|
|
|
|
return "Migration %s for %s" % (self.name, self.app)
|
|
|
|
|
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)
|
|
|
|
|
2013-05-10 23:00:55 +08:00
|
|
|
def ensure_schema(self):
|
|
|
|
"""
|
|
|
|
Ensures 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.
|
2014-09-21 06:29:13 +08:00
|
|
|
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
|
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):
|
|
|
|
"""
|
|
|
|
Returns a set of (app, name) of applied migrations.
|
|
|
|
"""
|
|
|
|
self.ensure_schema()
|
2014-04-30 22:53:20 +08:00
|
|
|
return set(tuple(x) for x in self.migration_qs.values_list("app", "name"))
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
def record_applied(self, app, name):
|
|
|
|
"""
|
|
|
|
Records that a migration was applied.
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Records that a migration was unapplied.
|
|
|
|
"""
|
|
|
|
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):
|
2013-06-19 22:36:22 +08:00
|
|
|
"""
|
|
|
|
Deletes all migration records. Useful if you're testing migrations.
|
|
|
|
"""
|
2014-04-30 22:53:20 +08:00
|
|
|
self.migration_qs.all().delete()
|