mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
[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
This commit is contained in:
parent
4f395e7527
commit
8f441f0962
@ -63,6 +63,7 @@ class Permission(models.Model):
|
|||||||
|
|
||||||
Three basic permissions -- add, change and delete -- are automatically created for each Django 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)
|
name = models.CharField(_('name'), max_length=50)
|
||||||
content_type = models.ForeignKey(ContentType)
|
content_type = models.ForeignKey(ContentType)
|
||||||
codename = models.CharField(_('codename'), max_length=100)
|
codename = models.CharField(_('codename'), max_length=100)
|
||||||
|
@ -72,6 +72,7 @@ class ContentTypeManager(models.Manager):
|
|||||||
self.__class__._cache.setdefault(using, {})[ct.id] = ct
|
self.__class__._cache.setdefault(using, {})[ct.id] = ct
|
||||||
|
|
||||||
class ContentType(models.Model):
|
class ContentType(models.Model):
|
||||||
|
id = models.NativeAutoField(primary_key=100)
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
app_label = models.CharField(max_length=100)
|
app_label = models.CharField(max_length=100)
|
||||||
model = models.CharField(_('python model class name'), max_length=100)
|
model = models.CharField(_('python model class name'), max_length=100)
|
||||||
|
@ -39,7 +39,7 @@ class DatabaseOperations(object):
|
|||||||
)
|
)
|
||||||
return self._cache[compiler_name]
|
return self._cache[compiler_name]
|
||||||
|
|
||||||
def flush(self, only_django=False):
|
def flush(self, style, only_django=False):
|
||||||
if only_django:
|
if only_django:
|
||||||
tables = self.connection.introspection.django_table_names(only_existing=True)
|
tables = self.connection.introspection.django_table_names(only_existing=True)
|
||||||
else:
|
else:
|
||||||
@ -69,10 +69,29 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
|||||||
def db(self):
|
def db(self):
|
||||||
return self.connection[self.settings_dict["NAME"]]
|
return self.connection[self.settings_dict["NAME"]]
|
||||||
|
|
||||||
def _rollback(self):
|
def close(self):
|
||||||
# TODO: ???
|
self._connection.disconnect()
|
||||||
|
self._connection = None
|
||||||
|
|
||||||
|
|
||||||
|
###########################
|
||||||
|
# TODO: Transaction stuff #
|
||||||
|
###########################
|
||||||
|
|
||||||
|
def _enter_transaction_management(self, managed):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _leave_transaction_management(self, managed):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _commit(self):
|
def _commit(self):
|
||||||
# TODO: ???
|
pass
|
||||||
|
|
||||||
|
def _rollback(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _savepoint(self, sid):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _savepoint_commit(self, sid):
|
||||||
pass
|
pass
|
||||||
|
@ -34,7 +34,7 @@ class SQLCompiler(object):
|
|||||||
|
|
||||||
def build_query(self):
|
def build_query(self):
|
||||||
assert not self.query.aggregates
|
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 self.query.default_cols
|
||||||
assert not self.query.distinct
|
assert not self.query.distinct
|
||||||
assert not self.query.extra
|
assert not self.query.extra
|
||||||
|
@ -47,23 +47,18 @@ Are you sure you want to do this?
|
|||||||
confirm = 'yes'
|
confirm = 'yes'
|
||||||
|
|
||||||
if 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:
|
try:
|
||||||
cursor = connection.cursor()
|
connection.ops.flush(self.style, only_django=True)
|
||||||
for sql in sql_list:
|
|
||||||
cursor.execute(sql)
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
transaction.rollback_unless_managed(using=db)
|
transaction.rollback_unless_managed(using=db)
|
||||||
|
raise
|
||||||
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
|
raise CommandError("""Database %s couldn't be flushed. Possible reasons:
|
||||||
* The database isn't running or isn't configured correctly.
|
* The database isn't running or isn't configured correctly.
|
||||||
* At least one of the expected database tables doesn't exist.
|
* At least one of the expected database tables doesn't exist.
|
||||||
* The SQL was invalid.
|
* 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.
|
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))
|
The full error: %s""" % (connection.settings_dict['NAME'], e))
|
||||||
|
else:
|
||||||
transaction.commit_unless_managed(using=db)
|
transaction.commit_unless_managed(using=db)
|
||||||
|
|
||||||
# Emit the post sync signal. This allows individual
|
# Emit the post sync signal. This allows individual
|
||||||
|
@ -55,6 +55,7 @@ class Command(BaseCommand):
|
|||||||
# Get a cursor (even though we don't need one yet). This has
|
# Get a cursor (even though we don't need one yet). This has
|
||||||
# the side effect of initializing the test database (if
|
# the side effect of initializing the test database (if
|
||||||
# it isn't already initialized).
|
# it isn't already initialized).
|
||||||
|
if hasattr(connection, "cursor"):
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
# Start transaction management. All fixtures are installed in a
|
# Start transaction management. All fixtures are installed in a
|
||||||
|
@ -111,7 +111,8 @@ class BaseDatabaseOperations(object):
|
|||||||
"""
|
"""
|
||||||
compiler_module = "django.db.models.sql.compiler"
|
compiler_module = "django.db.models.sql.compiler"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, connection):
|
||||||
|
self.connection = connection
|
||||||
self._cache = {}
|
self._cache = {}
|
||||||
|
|
||||||
def autoinc_sql(self, table, column):
|
def autoinc_sql(self, table, column):
|
||||||
@ -474,6 +475,14 @@ class BaseDatabaseOperations(object):
|
|||||||
conn = ' %s ' % connector
|
conn = ' %s ' % connector
|
||||||
return conn.join(sub_expressions)
|
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):
|
class BaseDatabaseIntrospection(object):
|
||||||
"""
|
"""
|
||||||
This class encapsulates all backend-specific introspection utilities
|
This class encapsulates all backend-specific introspection utilities
|
||||||
|
@ -43,7 +43,7 @@ class DatabaseWrapper(object):
|
|||||||
|
|
||||||
def __init__(self, settings_dict, alias, *args, **kwargs):
|
def __init__(self, settings_dict, alias, *args, **kwargs):
|
||||||
self.features = BaseDatabaseFeatures()
|
self.features = BaseDatabaseFeatures()
|
||||||
self.ops = DatabaseOperations()
|
self.ops = DatabaseOperations(self)
|
||||||
self.client = DatabaseClient(self)
|
self.client = DatabaseClient(self)
|
||||||
self.creation = BaseDatabaseCreation(self)
|
self.creation = BaseDatabaseCreation(self)
|
||||||
self.introspection = DatabaseIntrospection(self)
|
self.introspection = DatabaseIntrospection(self)
|
||||||
|
@ -254,7 +254,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper):
|
|||||||
|
|
||||||
self.server_version = None
|
self.server_version = None
|
||||||
self.features = DatabaseFeatures()
|
self.features = DatabaseFeatures()
|
||||||
self.ops = DatabaseOperations()
|
self.ops = DatabaseOperations(self)
|
||||||
self.client = DatabaseClient(self)
|
self.client = DatabaseClient(self)
|
||||||
self.creation = DatabaseCreation(self)
|
self.creation = DatabaseCreation(self)
|
||||||
self.introspection = DatabaseIntrospection(self)
|
self.introspection = DatabaseIntrospection(self)
|
||||||
|
@ -332,7 +332,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper):
|
|||||||
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.features = DatabaseFeatures()
|
self.features = DatabaseFeatures()
|
||||||
self.ops = DatabaseOperations()
|
self.ops = DatabaseOperations(self)
|
||||||
self.client = DatabaseClient(self)
|
self.client = DatabaseClient(self)
|
||||||
self.creation = DatabaseCreation(self)
|
self.creation = DatabaseCreation(self)
|
||||||
self.introspection = DatabaseIntrospection(self)
|
self.introspection = DatabaseIntrospection(self)
|
||||||
|
@ -154,7 +154,7 @@ class DatabaseWrapper(BaseSQLDatabaseWrapper):
|
|||||||
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
super(DatabaseWrapper, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.features = DatabaseFeatures()
|
self.features = DatabaseFeatures()
|
||||||
self.ops = DatabaseOperations()
|
self.ops = DatabaseOperations(self)
|
||||||
self.client = DatabaseClient(self)
|
self.client = DatabaseClient(self)
|
||||||
self.creation = DatabaseCreation(self)
|
self.creation = DatabaseCreation(self)
|
||||||
self.introspection = DatabaseIntrospection(self)
|
self.introspection = DatabaseIntrospection(self)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user