1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

[soc2009/multidb] Fixed the usage of using() on QuerySets in conjuction with create(), added tests for this

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10922 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-06-04 16:13:14 +00:00
parent 96cf08295d
commit fbb6fda5f6
4 changed files with 10 additions and 34 deletions

View File

@ -4,10 +4,6 @@ TODO
The follow is a list, more or less in the order I intend to do them of things
that need to be done. I'm trying to be as granular as possible.
***
Immediate TODOs:
Finish refactor of WhereNode so that it just takes connection in as_sql.
***
2) Update all old references to ``settings.DATABASE_*`` to reference
``settings.DATABASES``. This includes the following locations
@ -26,16 +22,8 @@ Finish refactor of WhereNode so that it just takes connection in as_sql.
* ``dumpdata``: By default dump the ``default`` database. Later add a
``--database`` flag.
These items will be fixed pending both community consensus, and the API
that will go in that's actually necessary for these to happen.
flush, reset, and syncdb need to not prompt the user multiple times.
9) Fix transaction support. In practice this means changing all the
dictionaries that currently map thread to a boolean to being a dictionary
mapping thread to a set of the affected DBs, and changing all the functions
that use these dictionaries to handle the changes appropriately.
7) Remove any references to the global ``django.db.connection`` object in the
SQL creation process. This includes(but is probably not limited to):
@ -50,6 +38,7 @@ Finish refactor of WhereNode so that it just takes connection in as_sql.
5) Add the ``using`` Meta option. Tests and docs(these are to be assumed at
each stage from here on out).
8) Implement some way to create a new ``Query`` for a different backend when
we switch. There are several checks against ``self.connection`` prior to
SQL construction, so we either need to defer all these(which will be
@ -67,10 +56,5 @@ Finish refactor of WhereNode so that it just takes connection in as_sql.
* ``DateTimeField.get_db_prep_value``
* ``DecimalField.get_db_prep_save``
* ``TimeField.get_db_prep_value``
* Implementing the command pattern.
* Having the ``QuerySet`` actually store ``Query`` objects for every
database in ``settings.DATABASES`` and do all the operations against
every single one of them, then when it's time excecute the query just
pick the right ``Query`` object to use. This *does* not scale, though it
could probably be done fairly easily.
10) Time permitting add support for a ``DatabaseManager``.

View File

@ -284,7 +284,7 @@ class QuerySet(object):
and returning the created object.
"""
obj = self.model(**kwargs)
obj.save(force_insert=True)
obj.save(force_insert=True, using=self._using)
return obj
def get_or_create(self, **kwargs):

View File

@ -257,8 +257,6 @@ def savepoint_commit(sid, using=None):
# DECORATORS #
##############
# TODO update all of these for multi-db
def autocommit(using_or_func=None):
"""
Decorator that activates commit on save. This is Django's default behavior;

View File

@ -20,17 +20,11 @@ class DatabaseSettingTestCase(TestCase):
class ConnectionTestCase(TestCase):
def test_queries(self):
for connection in connections.all():
qn = connection.ops.quote_name
cursor = connection.cursor()
cursor.execute("""INSERT INTO %(table)s (%(col)s) VALUES (%%s)""" % {
'table': qn(Book._meta.db_table),
'col': qn(Book._meta.get_field_by_name('title')[0].column),
}, ('Dive Into Python',))
for db in connections:
Book.objects.using(db).create(title="Dive into Python")
for connection in connections.all():
qn = connection.ops.quote_name
cursor = connection.cursor()
cursor.execute("""SELECT * FROM %(table)s""" % {'table': qn(Book._meta.db_table)})
data = cursor.fetchall()
self.assertEqual('Dive Into Python', data[0][1])
for db in connections:
books = Book.objects.all().using(db)
self.assertEqual(books.count(), 1)
self.assertEqual(len(books), 1)
self.assertEqual(books[0].title, "Dive into Python")