2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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
|
2014-04-30 22:53:20 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2013-07-26 23:46:48 +08:00
|
|
|
from django.utils.timezone import now
|
2013-05-10 23:00:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MigrationRecorder(object):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2014-04-30 22:53:20 +08:00
|
|
|
@python_2_unicode_compatible
|
2013-05-10 23:00:55 +08:00
|
|
|
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
|
2013-05-18 17:06:30 +08:00
|
|
|
with self.connection.schema_editor() as editor:
|
|
|
|
editor.create_model(self.Migration)
|
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()
|