Skip to content

Commit

Permalink
More conversion to a ContextManager schema_editor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed May 18, 2013
1 parent ce5bd42 commit 331546f
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 312 deletions.
33 changes: 7 additions & 26 deletions django/db/backends/schema.py
Expand Up @@ -61,38 +61,19 @@ def __init__(self, connection):

# State-managing methods

def start(self):
"""
Marks the start of a schema-altering run.
"""
self.deferred_sql = []
atomic(self.connection.alias).__enter__()

def commit(self):
"""
Finishes a schema-altering run.
"""
for sql in self.deferred_sql:
self.execute(sql)
atomic(self.connection.alias).__exit__(None, None, None)

def rollback(self):
"""
Tries to roll back a schema-altering run. Call instead of commit().
"""
if not self.connection.features.can_rollback_ddl:
raise RuntimeError("Cannot rollback schema changes on this backend")
atomic(self.connection.alias).__exit__(*sys.exc_info())

def __enter__(self):
self.start()
self.deferred_sql = []
atomic(self.connection.alias, self.connection.features.can_rollback_ddl).__enter__()
return self

def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.commit()
for sql in self.deferred_sql:
self.execute(sql)
atomic(self.connection.alias, self.connection.features.can_rollback_ddl).__exit__(None, None, None)
else:
self.rollback()
# Continue propagating exception
return None

# Core utility functions

Expand Down
6 changes: 6 additions & 0 deletions django/db/migrations/state.py
Expand Up @@ -30,6 +30,12 @@ def render(self):
model.render(self.app_cache)
return self.app_cache

@classmethod
def from_app_cache(cls, app_cache):
"Takes in an AppCache and returns a ProjectState matching it"
for model in app_cache.get_models():
print model


class ModelState(object):
"""
Expand Down

0 comments on commit 331546f

Please sign in to comment.