Skip to content

Commit 8f441f0

Browse files
committed
[soc2010/query-refactor] Cleaned up a TODO in the flush management command, and the resulting landslide of bugs this unveiled in the MongoDB backend.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13352 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 4f395e7 commit 8f441f0

File tree

11 files changed

+52
-26
lines changed

11 files changed

+52
-26
lines changed

django/contrib/auth/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Permission(models.Model):
6363
6464
Three basic permissions -- add, change and delete -- are automatically created for each Django model.
6565
"""
66+
id = models.NativeAutoField(primary_key=True)
6667
name = models.CharField(_('name'), max_length=50)
6768
content_type = models.ForeignKey(ContentType)
6869
codename = models.CharField(_('codename'), max_length=100)

django/contrib/contenttypes/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _add_to_cache(self, using, ct):
7272
self.__class__._cache.setdefault(using, {})[ct.id] = ct
7373

7474
class ContentType(models.Model):
75+
id = models.NativeAutoField(primary_key=100)
7576
name = models.CharField(max_length=100)
7677
app_label = models.CharField(max_length=100)
7778
model = models.CharField(_('python model class name'), max_length=100)

django/contrib/mongodb/base.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def compiler(self, compiler_name):
3939
)
4040
return self._cache[compiler_name]
4141

42-
def flush(self, only_django=False):
42+
def flush(self, style, only_django=False):
4343
if only_django:
4444
tables = self.connection.introspection.django_table_names(only_existing=True)
4545
else:
@@ -68,11 +68,30 @@ def connection(self):
6868
@property
6969
def db(self):
7070
return self.connection[self.settings_dict["NAME"]]
71+
72+
def close(self):
73+
self._connection.disconnect()
74+
self._connection = None
7175

72-
def _rollback(self):
73-
# TODO: ???
76+
77+
###########################
78+
# TODO: Transaction stuff #
79+
###########################
80+
81+
def _enter_transaction_management(self, managed):
82+
pass
83+
84+
def _leave_transaction_management(self, managed):
7485
pass
7586

7687
def _commit(self):
77-
# TODO: ???
88+
pass
89+
90+
def _rollback(self):
91+
pass
92+
93+
def _savepoint(self, sid):
94+
pass
95+
96+
def _savepoint_commit(self, sid):
7897
pass

django/contrib/mongodb/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def make_atom(self, lhs, lookup_type, value_annotation, params_or_value):
3434

3535
def build_query(self):
3636
assert not self.query.aggregates
37-
assert len(self.query.alias_map) == 1
37+
assert len([a for a in self.query.alias_map if self.query.alias_refcount[a]]) == 1
3838
assert self.query.default_cols
3939
assert not self.query.distinct
4040
assert not self.query.extra

django/core/management/commands/flush.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,20 @@ def handle_noargs(self, **options):
4747
confirm = 'yes'
4848

4949
if confirm == 'yes':
50-
# TODO: HACK, make this more OO.
51-
if not getattr(connection.ops, "sql_ddl", True):
52-
connection.ops.flush(only_django=True)
53-
return
54-
sql_list = sql_flush(self.style, connection, only_django=True)
5550
try:
56-
cursor = connection.cursor()
57-
for sql in sql_list:
58-
cursor.execute(sql)
51+
connection.ops.flush(self.style, only_django=True)
5952
except Exception, e:
6053
transaction.rollback_unless_managed(using=db)
54+
raise
6155
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
62-
* The database isn't running or isn't configured correctly.
63-
* At least one of the expected database tables doesn't exist.
64-
* The SQL was invalid.
65-
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
66-
The full error: %s""" % (connection.settings_dict['NAME'], e))
67-
transaction.commit_unless_managed(using=db)
68-
56+
* The database isn't running or isn't configured correctly.
57+
* At least one of the expected database tables doesn't exist.
58+
* The SQL was invalid.
59+
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
60+
The full error: %s""" % (connection.settings_dict['NAME'], e))
61+
else:
62+
transaction.commit_unless_managed(using=db)
63+
6964
# Emit the post sync signal. This allows individual
7065
# applications to respond as if the database had been
7166
# sync'd from scratch.

django/core/management/commands/loaddata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def handle(self, *fixture_labels, **options):
5555
# Get a cursor (even though we don't need one yet). This has
5656
# the side effect of initializing the test database (if
5757
# it isn't already initialized).
58-
cursor = connection.cursor()
58+
if hasattr(connection, "cursor"):
59+
cursor = connection.cursor()
5960

6061
# Start transaction management. All fixtures are installed in a
6162
# single transaction to ensure that all references are resolved.

django/db/backends/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class BaseDatabaseOperations(object):
111111
"""
112112
compiler_module = "django.db.models.sql.compiler"
113113

114-
def __init__(self):
114+
def __init__(self, connection):
115+
self.connection = connection
115116
self._cache = {}
116117

117118
def autoinc_sql(self, table, column):
@@ -473,6 +474,14 @@ def combine_expression(self, connector, sub_expressions):
473474
"""
474475
conn = ' %s ' % connector
475476
return conn.join(sub_expressions)
477+
478+
def flush(self, style, only_django=False):
479+
from django.core.management.sql import sql_flush
480+
sql_list = sql_flush(style, self.connection, only_django=True)
481+
cursor = self.connection.cursor()
482+
for sql in sql_list:
483+
cursor.execute(sql)
484+
476485

477486
class BaseDatabaseIntrospection(object):
478487
"""

django/db/backends/dummy/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DatabaseWrapper(object):
4343

4444
def __init__(self, settings_dict, alias, *args, **kwargs):
4545
self.features = BaseDatabaseFeatures()
46-
self.ops = DatabaseOperations()
46+
self.ops = DatabaseOperations(self)
4747
self.client = DatabaseClient(self)
4848
self.creation = BaseDatabaseCreation(self)
4949
self.introspection = DatabaseIntrospection(self)

django/db/backends/mysql/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def __init__(self, *args, **kwargs):
254254

255255
self.server_version = None
256256
self.features = DatabaseFeatures()
257-
self.ops = DatabaseOperations()
257+
self.ops = DatabaseOperations(self)
258258
self.client = DatabaseClient(self)
259259
self.creation = DatabaseCreation(self)
260260
self.introspection = DatabaseIntrospection(self)

django/db/backends/oracle/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def __init__(self, *args, **kwargs):
332332
super(DatabaseWrapper, self).__init__(*args, **kwargs)
333333

334334
self.features = DatabaseFeatures()
335-
self.ops = DatabaseOperations()
335+
self.ops = DatabaseOperations(self)
336336
self.client = DatabaseClient(self)
337337
self.creation = DatabaseCreation(self)
338338
self.introspection = DatabaseIntrospection(self)

django/db/backends/sqlite3/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def __init__(self, *args, **kwargs):
154154
super(DatabaseWrapper, self).__init__(*args, **kwargs)
155155

156156
self.features = DatabaseFeatures()
157-
self.ops = DatabaseOperations()
157+
self.ops = DatabaseOperations(self)
158158
self.client = DatabaseClient(self)
159159
self.creation = DatabaseCreation(self)
160160
self.introspection = DatabaseIntrospection(self)

0 commit comments

Comments
 (0)