MCPcopy
hub / github.com/django/django / MigrationRecorder

Class MigrationRecorder

django/db/migrations/recorder.py:9–111  ·  view source on GitHub ↗

Deal 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 migrations. We manually handle table creation/schema updating (using schema backend) and then have a floa

Source from the content-addressed store, hash-verified

7
8
9class 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

Calls

no outgoing calls