From 07ba148d9e4c98d0e5a0415a6e805af6431da277 Mon Sep 17 00:00:00 2001 From: Alex Hill Date: Thu, 2 Apr 2015 00:57:53 +0800 Subject: [PATCH] Cleaned up use of context manager in SQLite schema editor --- django/db/backends/sqlite3/schema.py | 32 +++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 0664d31e7a0..5ce7a964e67 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -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