diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 681cf1422a..3384a1275a 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -63,6 +63,7 @@ class Permission(models.Model): Three basic permissions -- add, change and delete -- are automatically created for each Django model. """ + id = models.NativeAutoField(primary_key=True) name = models.CharField(_('name'), max_length=50) content_type = models.ForeignKey(ContentType) codename = models.CharField(_('codename'), max_length=100) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index af3abf249f..862284ac0c 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -72,6 +72,7 @@ class ContentTypeManager(models.Manager): self.__class__._cache.setdefault(using, {})[ct.id] = ct class ContentType(models.Model): + id = models.NativeAutoField(primary_key=100) name = models.CharField(max_length=100) app_label = models.CharField(max_length=100) model = models.CharField(_('python model class name'), max_length=100) diff --git a/django/contrib/mongodb/base.py b/django/contrib/mongodb/base.py index 1fc8a26770..f1bacee812 100644 --- a/django/contrib/mongodb/base.py +++ b/django/contrib/mongodb/base.py @@ -39,7 +39,7 @@ class DatabaseOperations(object): ) return self._cache[compiler_name] - def flush(self, only_django=False): + def flush(self, style, only_django=False): if only_django: tables = self.connection.introspection.django_table_names(only_existing=True) else: @@ -68,11 +68,30 @@ class DatabaseWrapper(BaseDatabaseWrapper): @property def db(self): return self.connection[self.settings_dict["NAME"]] + + def close(self): + self._connection.disconnect() + self._connection = None - def _rollback(self): - # TODO: ??? + + ########################### + # TODO: Transaction stuff # + ########################### + + def _enter_transaction_management(self, managed): + pass + + def _leave_transaction_management(self, managed): pass def _commit(self): - # TODO: ??? + pass + + def _rollback(self): + pass + + def _savepoint(self, sid): + pass + + def _savepoint_commit(self, sid): pass diff --git a/django/contrib/mongodb/compiler.py b/django/contrib/mongodb/compiler.py index 045f47504d..83464be76e 100644 --- a/django/contrib/mongodb/compiler.py +++ b/django/contrib/mongodb/compiler.py @@ -34,7 +34,7 @@ class SQLCompiler(object): def build_query(self): assert not self.query.aggregates - assert len(self.query.alias_map) == 1 + assert len([a for a in self.query.alias_map if self.query.alias_refcount[a]]) == 1 assert self.query.default_cols assert not self.query.distinct assert not self.query.extra diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index 94c12490ce..43279c1a13 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -47,25 +47,20 @@ Are you sure you want to do this? confirm = 'yes' if confirm == 'yes': - # TODO: HACK, make this more OO. - if not getattr(connection.ops, "sql_ddl", True): - connection.ops.flush(only_django=True) - return - sql_list = sql_flush(self.style, connection, only_django=True) try: - cursor = connection.cursor() - for sql in sql_list: - cursor.execute(sql) + connection.ops.flush(self.style, only_django=True) except Exception, e: transaction.rollback_unless_managed(using=db) + raise raise CommandError("""Database %s couldn't be flushed. Possible reasons: - * The database isn't running or isn't configured correctly. - * At least one of the expected database tables doesn't exist. - * The SQL was invalid. -Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. -The full error: %s""" % (connection.settings_dict['NAME'], e)) - transaction.commit_unless_managed(using=db) - + * The database isn't running or isn't configured correctly. + * At least one of the expected database tables doesn't exist. + * The SQL was invalid. + Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. + The full error: %s""" % (connection.settings_dict['NAME'], e)) + else: + transaction.commit_unless_managed(using=db) + # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index caf3b11b85..5db043e468 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -55,7 +55,8 @@ class Command(BaseCommand): # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database (if # it isn't already initialized). - cursor = connection.cursor() + if hasattr(connection, "cursor"): + cursor = connection.cursor() # Start transaction management. All fixtures are installed in a # single transaction to ensure that all references are resolved. diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index cbd3307f04..289f807507 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -111,7 +111,8 @@ class BaseDatabaseOperations(object): """ compiler_module = "django.db.models.sql.compiler" - def __init__(self): + def __init__(self, connection): + self.connection = connection self._cache = {} def autoinc_sql(self, table, column): @@ -473,6 +474,14 @@ class BaseDatabaseOperations(object): """ conn = ' %s ' % connector return conn.join(sub_expressions) + + def flush(self, style, only_django=False): + from django.core.management.sql import sql_flush + sql_list = sql_flush(style, self.connection, only_django=True) + cursor = self.connection.cursor() + for sql in sql_list: + cursor.execute(sql) + class BaseDatabaseIntrospection(object): """ diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index 2cda04e528..8774cbddd2 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -43,7 +43,7 @@ class DatabaseWrapper(object): def __init__(self, settings_dict, alias, *args, **kwargs): self.features = BaseDatabaseFeatures() - self.ops = DatabaseOperations() + self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = BaseDatabaseCreation(self) self.introspection = DatabaseIntrospection(self) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 09d5ca04a7..d818c18304 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -254,7 +254,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper): self.server_version = None self.features = DatabaseFeatures() - self.ops = DatabaseOperations() + self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index b44681c8c8..0b4ca86716 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -332,7 +332,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() - self.ops = DatabaseOperations() + self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 3ef3fab72a..6929a1b7ca 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -154,7 +154,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() - self.ops = DatabaseOperations() + self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self)