Michael Manfre avatar Michael Manfre committed 08bba34

Override BaseDatabaseCreation.create_test_db to remove Site id coercion that uses a MSSQL unsupported update of an identity column. Fixes #116

Comments (0)

Files changed (1)

sqlserver_ado/creation.py

                 print "Re-enabling Transactions"
             self.connection.connection.supportsTransactions = self._supports_transactions
 
+    def create_test_db(self, verbosity=1, autoclobber=False):
+        """
+        Duplicate of BaseDatabaseCreation.create_test_db to disable broken Site id coersion.
+        Fixes django #17467 that was introduced by django #15573.
+        """
+        # Don't import django.core.management if it isn't needed.
+        from django.core.management import call_command
+
+        test_database_name = self._get_test_db_name()
+
+        if verbosity >= 1:
+            test_db_repr = ''
+            if verbosity >= 2:
+                test_db_repr = " ('%s')" % test_database_name
+            print "Creating test database for alias '%s'%s..." % (self.connection.alias, test_db_repr)
+
+        self._create_test_db(verbosity, autoclobber)
+
+        self.connection.close()
+        self.connection.settings_dict["NAME"] = test_database_name
+
+        # Confirm the feature set of the test database
+        self.connection.features.confirm()
+
+        # Report syncdb messages at one level lower than that requested.
+        # This ensures we don't get flooded with messages during testing
+        # (unless you really ask to be flooded)
+        call_command('syncdb',
+            verbosity=max(verbosity - 1, 0),
+            interactive=False,
+            database=self.connection.alias,
+            load_initial_data=False)
+
+        # We need to then do a flush to ensure that any data installed by
+        # custom SQL has been removed. The only test data should come from
+        # test fixtures, or autogenerated from post_syncdb triggers.
+        # This has the side effect of loading initial data (which was
+        # intentionally skipped in the syncdb).
+        call_command('flush',
+            verbosity=max(verbosity - 1, 0),
+            interactive=False,
+            database=self.connection.alias)
+        
+        # One effect of calling syncdb followed by flush is that the id of the
+        # default site may or may not be 1, depending on how the sequence was
+        # reset.  If the sites app is loaded, then we report a mismatch.
+        from django.db.models import get_model
+        if 'django.contrib.sites' in settings.INSTALLED_APPS:
+            Site = get_model('sites', 'Site')
+            if Site is not None and Site.objects.using(self.connection.alias).count() == 1:
+                site_id = Site.objects.using(self.connection.alias).all().values_list('id', flat=True)[0]
+                
+                if site_id != settings.SITE_ID:
+                     print "settings.SITE_ID does not match Site object. This may cause some tests to fail."
+
+        from django.core.cache import get_cache
+        from django.core.cache.backends.db import BaseDatabaseCache
+        for cache_alias in settings.CACHES:
+            cache = get_cache(cache_alias)
+            if isinstance(cache, BaseDatabaseCache):
+                from django.db import router
+                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
+                    call_command('createcachetable', cache._table, database=self.connection.alias)
+
+        # Get a cursor (even though we don't need one yet). This has
+        # the side effect of initializing the test database.
+        cursor = self.connection.cursor()
+
+        return test_database_name
+
+
     def _create_test_db(self, verbosity=1, autoclobber=False):
         test_database_name = self._test_database_name(settings)
         
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.