Login

South unran migration check

Author:
rednaw
Posted:
July 11, 2013
Language:
Python
Version:
1.5
Score:
0 (after 0 ratings)

Middleware that checks if you ran all South migrations. If not, it will throw an exception. Make sure to only use this middleware in development!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from south import migration
from south.models import MigrationHistory


class SouthUnranMigrationCheck(object):

    def process_request(self, request):
        '''
        Checks if you ran all South migrations. If not, it will throw an
        exception (DidNotApplyAllMigrations).
        '''

        unapplied_migrations = self.unapplied_migrations()

        if len(unapplied_migrations) > 0:

            message = u'You haven\'t run the following migrations: {}'.format(
                u''.join(
                    [u'\n  "{}" in app "{}".'.format(name, app)
                    for name, app in unapplied_migrations]
                )
            )

            raise DidNotApplyAllMigrations(message)

    def unapplied_migrations(self):
        '''
        Returns a list of tuples of unapplied migrations. The tuples consist of
        a migration name and an app label.
        '''
        
        applied_migrations = self.applied_migrations()
        unapplied_migrations = []

        for app_migration_files in migration.all_migrations():

            for migration_file in app_migration_files:

                app_label = migration_file.app_label()
                migration_name = migration_file.name()

                if migration_name not in applied_migrations[app_label]:
                    unapplied_migrations.append((migration_name, app_label))

        return unapplied_migrations

    def applied_migrations(self):
        '''
        Returns a dictionary with the app name in the key, and a list of
        migrations in the value.
        '''

        applied_migrations = {}

        for applied_migration in MigrationHistory.objects.all():

            if applied_migration.app_name not in applied_migrations:
                applied_migrations[applied_migration.app_name] = []

            applied_migrations[applied_migration.app_name].append(
                applied_migration.migration)

        return applied_migrations


class DidNotApplyAllMigrations(Exception):
    '''
    Exception that indicates that you havent run all migrations.
    '''
    pass

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.