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.
This commit is contained in:
Hasan Ramezani 2019-11-14 21:19:18 +01:00 committed by Mariusz Felisiak
parent 24b9f50823
commit d0c86a1df4
1 changed files with 10 additions and 8 deletions

View File

@ -330,7 +330,7 @@ class MigrationExecutor:
found_create_model_migration = False found_create_model_migration = False
found_add_field_migration = False found_add_field_migration = False
with self.connection.cursor() as cursor: 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 # Make sure all create model and add field operations are done
for operation in migration.operations: for operation in migration.operations:
if isinstance(operation, migrations.CreateModel): if isinstance(operation, migrations.CreateModel):
@ -363,14 +363,16 @@ class MigrationExecutor:
else: else:
found_add_field_migration = True found_add_field_migration = True
continue continue
columns = self.connection.introspection.get_table_description(
column_names = [ self.connection.cursor(),
column.name for column in table,
self.connection.introspection.get_table_description(self.connection.cursor(), table) )
] for column in columns:
if field.column not in column_names: if column.name == field.column:
found_add_field_migration = True
break
else:
return False, project_state return False, project_state
found_add_field_migration = True
# If we get this far and we found at least one CreateModel or AddField migration, # If we get this far and we found at least one CreateModel or AddField migration,
# the migration is considered implicitly applied. # the migration is considered implicitly applied.
return (found_create_model_migration or found_add_field_migration), after_state return (found_create_model_migration or found_add_field_migration), after_state