Cleaned up use of context manager in SQLite schema editor

This commit is contained in:
Alex Hill 2015-04-02 00:57:53 +08:00 committed by Tim Graham
parent a1842e3299
commit 07ba148d9e
1 changed files with 15 additions and 17 deletions

View File

@ -168,27 +168,25 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
yield
model._meta.db_table = original_table_name
# Rename the old table to something temporary
old_table_name = model._meta.db_table + "__old"
with altered_table_name(model, old_table_name):
with altered_table_name(model, model._meta.db_table + "__old"):
# Rename the old table to make way for the new
self.alter_db_table(model, temp_model._meta.db_table, model._meta.db_table)
# Create a new table with that format. We remove things from the
# deferred SQL that match our table name, too
self.deferred_sql = [x for x in self.deferred_sql if temp_model._meta.db_table not in x]
self.create_model(temp_model)
# Create a new table with the updated schema. We remove things
# from the deferred SQL that match our table name, too
self.deferred_sql = [x for x in self.deferred_sql if temp_model._meta.db_table not in x]
self.create_model(temp_model)
# Copy data from the old table
field_maps = list(mapping.items())
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
self.quote_name(temp_model._meta.db_table),
', '.join(self.quote_name(x) for x, y in field_maps),
', '.join(y for x, y in field_maps),
self.quote_name(old_table_name),
))
# Copy data from the old table into the new table
field_maps = list(mapping.items())
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
self.quote_name(temp_model._meta.db_table),
', '.join(self.quote_name(x) for x, y in field_maps),
', '.join(y for x, y in field_maps),
self.quote_name(model._meta.db_table),
))
# Delete the old table
with altered_table_name(model, old_table_name):
# Delete the old table
self.delete_model(model, handle_autom2m=False)
# Run deferred SQL on correct table