You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

recorder.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from django.apps.registry import Apps
  2. from django.db import models
  3. from django.db.utils import DatabaseError
  4. from django.utils.decorators import classproperty
  5. from django.utils.timezone import now
  6. from .exceptions import MigrationSchemaMissing
  7. class MigrationRecorder:
  8. """
  9. Deal with storing migration records in the database.
  10. Because this table is actually itself used for dealing with model
  11. creation, it's the one thing we can't do normally via migrations.
  12. We manually handle table creation/schema updating (using schema backend)
  13. and then have a floating model to do queries with.
  14. If a migration is unapplied its row is removed from the table. Having
  15. a row in the table always means a migration is applied.
  16. """
  17. _migration_class = None
  18. @classproperty
  19. def Migration(cls):
  20. """
  21. Lazy load to avoid AppRegistryNotReady if installed apps import
  22. MigrationRecorder.
  23. """
  24. if cls._migration_class is None:
  25. class Migration(models.Model):
  26. app = models.CharField(max_length=255)
  27. name = models.CharField(max_length=255)
  28. applied = models.DateTimeField(default=now)
  29. class Meta:
  30. apps = Apps()
  31. app_label = 'migrations'
  32. db_table = 'django_migrations'
  33. def __str__(self):
  34. return 'Migration %s for %s' % (self.name, self.app)
  35. cls._migration_class = Migration
  36. return cls._migration_class
  37. def __init__(self, connection):
  38. self.connection = connection
  39. @property
  40. def migration_qs(self):
  41. return self.Migration.objects.using(self.connection.alias)
  42. def has_table(self):
  43. """Return True if the django_migrations table exists."""
  44. return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  45. def ensure_schema(self):
  46. """Ensure the table exists and has the correct schema."""
  47. # If the table's there, that's fine - we've never changed its schema
  48. # in the codebase.
  49. if self.has_table():
  50. return
  51. # Make the table
  52. try:
  53. with self.connection.schema_editor() as editor:
  54. editor.create_model(self.Migration)
  55. except DatabaseError as exc:
  56. raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
  57. def applied_migrations(self):
  58. """Return a set of (app, name) of applied migrations."""
  59. if self.has_table():
  60. return {tuple(x) for x in self.migration_qs.values_list('app', 'name')}
  61. else:
  62. # If the django_migrations table doesn't exist, then no migrations
  63. # are applied.
  64. return set()
  65. def record_applied(self, app, name):
  66. """Record that a migration was applied."""
  67. self.ensure_schema()
  68. self.migration_qs.create(app=app, name=name)
  69. def record_unapplied(self, app, name):
  70. """Record that a migration was unapplied."""
  71. self.ensure_schema()
  72. self.migration_qs.filter(app=app, name=name).delete()
  73. def flush(self):
  74. """Delete all migration records. Useful for testing migrations."""
  75. self.migration_qs.all().delete()