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