diff --git a/django_test_migrations/sql.py b/django_test_migrations/sql.py index 63c2fbc..266072f 100644 --- a/django_test_migrations/sql.py +++ b/django_test_migrations/sql.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from functools import partial -from typing import Callable, List, Optional, Union +from typing import Callable, Dict, List, Optional, Union from django.core.management.color import Style, no_style from django.db import DefaultConnectionProxy, connections, transaction @@ -9,7 +9,7 @@ _Connection = Union[DefaultConnectionProxy, BaseDatabaseWrapper] -DJANGO_MIGRATIONS_TABLE = 'django_migrations' +DJANGO_MIGRATIONS_TABLE_NAME = 'django_migrations' def drop_models_tables( @@ -42,23 +42,39 @@ def flush_django_migrations_table( """Flush `django_migrations` table.""" style = style or no_style() connection = connections[database_name] - with connection.cursor() as cursor: - django_migrations_sequences = connection.introspection.get_sequences( - cursor, - DJANGO_MIGRATIONS_TABLE, - ) + django_migrations_sequences = get_django_migrations_table_sequences( + connection, + ) execute_sql_flush = get_execute_sql_flush_for(connection) execute_sql_flush( database_name, connection.ops.sql_flush( style, - [DJANGO_MIGRATIONS_TABLE], + [DJANGO_MIGRATIONS_TABLE_NAME], django_migrations_sequences, allow_cascade=False, ), ) +def get_django_migrations_table_sequences( + connection: _Connection, +) -> List[Dict[str, str]]: # pragma: no cover + """`django_migrations` table introspected sequences. + + Returns properly inspected sequences when using `Django>1.11` + and static sequence for `id` column otherwise. + + """ + if hasattr(connection.introspection, 'get_sequences'): # noqa: WPS421 + with connection.cursor() as cursor: # noqa: E800 + return connection.introspection.get_sequences( + cursor, + DJANGO_MIGRATIONS_TABLE_NAME, + ) + return [{'table': DJANGO_MIGRATIONS_TABLE_NAME, 'column': 'id'}] + + def get_execute_sql_flush_for( connection: _Connection, ) -> Callable[[str, List[str]], None]: diff --git a/setup.cfg b/setup.cfg index a893786..f910db7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ exclude = ignore = D100, D104, D401, W504, X100, RST303, RST304, DAR103, DAR203 per-file-ignores = + django_test_migrations/sql.py: E800 django_test_app/main_app/migrations/*.py: N806, WPS102, WPS114 django_test_app/django_test_app/settings.py: S105, WPS226, WPS407 tests/test_*.py: N806, S101, S404, S603, S607, WPS226