Ticket #14799: t14799.diff

File t14799.diff, 2.7 KB (added by Russell Keith-Magee, 14 years ago)

Draft fix for problem.

  • django/test/simple.py

    diff -r 78ad3faa689b django/test/simple.py
    a b  
    222222        # and which ones are test mirrors or duplicate entries in DATABASES
    223223        mirrored_aliases = {}
    224224        test_databases = {}
     225        db_dependencies = {}
    225226        for alias in connections:
    226227            connection = connections[alias]
    227228            if connection.settings_dict['TEST_MIRROR']:
     
    239240                        connection.settings_dict['NAME'],
    240241                    ), []).append(alias)
    241242
     243                if 'TEST_DEPENDENCIES' in connection.settings_dict:
     244                    db_dependencies[alias] = connection.settings_dict['TEST_DEPENDENCIES']
     245                else:
     246                    if alias != 'default':
     247                        db_dependencies[alias] = connection.settings_dict.get('TEST_DEPENDENCIES', ['default'])
     248
     249        # Ensure that the order in which the test databases are created
     250        # satisfies any dependencies between databases.
     251        ordered_test_databases = []
     252        resolved_databases = set()
     253        test_databases = test_databases.items()
     254        while test_databases:
     255            changed = False
     256            deferred = []
     257
     258            while test_databases:
     259                signature, aliases = test_databases.pop()
     260
     261                dependencies_satisfied = True
     262                for alias in aliases:
     263                    if alias in db_dependencies:
     264                        if all(a in resolved_databases for a in db_dependencies[alias]):
     265                            # all dependencies for this alias are satisfied
     266                            db_dependencies.pop(alias)
     267                            resolved_databases.add(alias)
     268                    else:
     269                        resolved_databases.add(alias)
     270
     271                if dependencies_satisfied:
     272                    ordered_test_databases.append((signature, aliases))
     273                    changed = True
     274                else:
     275                    deferred.append((signature, aliases))
     276
     277            if not changed:
     278                raise Exception("Circular dependency in TEST_DEPENDENCIES")
     279            test_databases = deferred
     280
    242281        # Second pass -- actually create the databases.
    243282        old_names = []
    244283        mirrors = []
    245         for (host, port, engine, db_name), aliases in test_databases.items():
     284        for (host, port, engine, db_name), aliases in ordered_test_databases:
    246285            # Actually create the database for the first connection
    247286            connection = connections[aliases[0]]
    248287            old_names.append((connection, db_name, True))
Back to Top