From d0c86a1df4564c7c29d5112a73a32d369958880d Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Thu, 14 Nov 2019 21:19:18 +0100 Subject: [PATCH] Refs #29808 -- Optimized MigrationExecutor.detect_soft_applied(). Use set() for iterables used only for containment checks. Simplify column checks O(n) instead of O(2n). Thanks Simon Charette for an idea. --- django/db/migrations/executor.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/django/db/migrations/executor.py b/django/db/migrations/executor.py index 7b779819e9..ee275cdaef 100644 --- a/django/db/migrations/executor.py +++ b/django/db/migrations/executor.py @@ -330,7 +330,7 @@ class MigrationExecutor: found_create_model_migration = False found_add_field_migration = False with self.connection.cursor() as cursor: - existing_table_names = self.connection.introspection.table_names(cursor) + existing_table_names = set(self.connection.introspection.table_names(cursor)) # Make sure all create model and add field operations are done for operation in migration.operations: if isinstance(operation, migrations.CreateModel): @@ -363,14 +363,16 @@ class MigrationExecutor: else: found_add_field_migration = True continue - - column_names = [ - column.name for column in - self.connection.introspection.get_table_description(self.connection.cursor(), table) - ] - if field.column not in column_names: + columns = self.connection.introspection.get_table_description( + self.connection.cursor(), + table, + ) + for column in columns: + if column.name == field.column: + found_add_field_migration = True + break + else: return False, project_state - found_add_field_migration = True # If we get this far and we found at least one CreateModel or AddField migration, # the migration is considered implicitly applied. return (found_create_model_migration or found_add_field_migration), after_state