1
0
mirror of https://github.com/django/django.git synced 2025-10-27 23:56:08 +00:00

First phase of loading migrations from disk

This commit is contained in:
Andrew Godwin
2013-05-10 16:00:55 +01:00
parent cb4b0de49e
commit 9ce8354672
9 changed files with 265 additions and 9 deletions

View File

@@ -0,0 +1,64 @@
import datetime
from django.db import models
from django.db.models.loading import BaseAppCache
class MigrationRecorder(object):
"""
Deals with storing migration records in the database.
Because this table is actually itself used for dealing with model
creation, it's the one thing we can't do normally via syncdb or migrations.
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)
applied = models.DateTimeField(default=datetime.datetime.utcnow)
class Meta:
app_cache = BaseAppCache()
app_label = "migrations"
db_table = "django_migrations"
def __init__(self, connection):
self.connection = connection
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.
if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
return
# Make the table
editor = self.connection.schema_editor()
editor.start()
editor.create_model(self.Migration)
editor.commit()
def applied_migrations(self):
"""
Returns a set of (app, name) of applied migrations.
"""
self.ensure_schema()
return set(tuple(x) for x in self.Migration.objects.values_list("app", "name"))
def record_applied(self, app, name):
"""
Records that a migration was applied.
"""
self.ensure_schema()
self.Migration.objects.create(app=app, name=name)
def record_unapplied(self, app, name):
"""
Records that a migration was unapplied.
"""
self.ensure_schema()
self.Migration.objects.filter(app=app, name=name).delete()