Ticket #22474: 22474-2.diff

File 22474-2.diff, 3.2 KB (added by Claude Paroz, 10 years ago)

Include classmethod fix and basic test

  • django/db/migrations/recorder.py

    diff --git a/django/db/migrations/recorder.py b/django/db/migrations/recorder.py
    index 15a3b49..8d8ae3e 100644
    a b  
    11from django.apps.registry import Apps
    22from django.db import models
     3from django.utils.encoding import python_2_unicode_compatible
    34from django.utils.timezone import now
    45
    56
    class MigrationRecorder(object):  
    1617    a row in the table always means a migration is applied.
    1718    """
    1819
     20    @python_2_unicode_compatible
    1921    class Migration(models.Model):
    2022        app = models.CharField(max_length=255)
    2123        name = models.CharField(max_length=255)
    class MigrationRecorder(object):  
    2628            app_label = "migrations"
    2729            db_table = "django_migrations"
    2830
     31        def __str__(self):
     32            return "Migration %s for %s" % (self.name, self.app)
     33
    2934    def __init__(self, connection):
    3035        self.connection = connection
    3136
    class MigrationRecorder(object):  
    4651        Returns a set of (app, name) of applied migrations.
    4752        """
    4853        self.ensure_schema()
    49         return set(tuple(x) for x in self.Migration.objects.values_list("app", "name"))
     54        return set(tuple(x) for x in self.Migration.objects.using(self.connection.alias).values_list("app", "name"))
    5055
    5156    def record_applied(self, app, name):
    5257        """
    5358        Records that a migration was applied.
    5459        """
    5560        self.ensure_schema()
    56         self.Migration.objects.create(app=app, name=name)
     61        self.Migration.objects.using(self.connection.alias).create(app=app, name=name)
    5762
    5863    def record_unapplied(self, app, name):
    5964        """
    6065        Records that a migration was unapplied.
    6166        """
    6267        self.ensure_schema()
    63         self.Migration.objects.filter(app=app, name=name).delete()
     68        self.Migration.objects.using(self.connection.alias).filter(app=app, name=name).delete()
    6469
    65     @classmethod
    66     def flush(cls):
     70    def flush(self):
    6771        """
    6872        Deletes all migration records. Useful if you're testing migrations.
    6973        """
    70         cls.Migration.objects.all().delete()
     74        self.Migration.objects.using(self.connection.alias).all().delete()
  • tests/migrations/test_loader.py

    diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py
    index f661a03..0e9b9c2 100644
    a b  
    11from unittest import skipIf
    22
    33from django.test import TestCase, override_settings
    4 from django.db import connection
     4from django.db import connection, connections
    55from django.db.migrations.loader import MigrationLoader, AmbiguityError
    66from django.db.migrations.recorder import MigrationRecorder
    77from django.utils import six
    class RecorderTests(TestCase):  
    2626            recorder.applied_migrations(),
    2727            set([("myapp", "0432_ponies")]),
    2828        )
     29        # That should not affect records of another database
     30        recorder_other = MigrationRecorder(connections['other'])
     31        self.assertEqual(
     32            recorder_other.applied_migrations(),
     33            set(),
     34        )
    2935        recorder.record_unapplied("myapp", "0432_ponies")
    3036        self.assertEqual(
    3137            recorder.applied_migrations(),
Back to Top