Skip to content

Commit

Permalink
Fix schema editor interaction with new transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Apr 19, 2013
1 parent 7f3678d commit 6e21a59
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions django/db/backends/schema.py
Expand Up @@ -64,7 +64,9 @@ def start(self):
Marks the start of a schema-altering run. Marks the start of a schema-altering run.
""" """
self.deferred_sql = [] self.deferred_sql = []
self.connection.set_autocommit(False) self.old_autocommit = self.connection.autocommit
if self.connection.autocommit:
self.connection.set_autocommit(False)


def commit(self): def commit(self):
""" """
Expand All @@ -73,7 +75,7 @@ def commit(self):
for sql in self.deferred_sql: for sql in self.deferred_sql:
self.execute(sql) self.execute(sql)
self.connection.commit() self.connection.commit()
self.connection.set_autocommit(True) self.connection.set_autocommit(self.old_autocommit)


def rollback(self): def rollback(self):
""" """
Expand All @@ -82,7 +84,7 @@ def rollback(self):
if not self.connection.features.can_rollback_ddl: if not self.connection.features.can_rollback_ddl:
raise RuntimeError("Cannot rollback schema changes on this backend") raise RuntimeError("Cannot rollback schema changes on this backend")
self.connection.rollback() self.connection.rollback()
self.connection.set_autocommit(True) self.connection.set_autocommit(self.old_autocommit)


# Core utility functions # Core utility functions


Expand Down
2 changes: 1 addition & 1 deletion tests/schema/tests.py
Expand Up @@ -453,7 +453,7 @@ def test_unique(self):
Tag.objects.create(title="foo", slug="foo") Tag.objects.create(title="foo", slug="foo")
Tag.objects.create(title="bar", slug="foo") Tag.objects.create(title="bar", slug="foo")
connection.rollback() connection.rollback()
# Alter the slug field to be non-unique # Alter the slug field to be unique
new_new_field = SlugField(unique=True) new_new_field = SlugField(unique=True)
new_new_field.set_attributes_from_name("slug") new_new_field.set_attributes_from_name("slug")
editor = connection.schema_editor() editor = connection.schema_editor()
Expand Down

0 comments on commit 6e21a59

Please sign in to comment.