Skip to content

Commit

Permalink
make db reset solution compatible with Django==1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
skarzi committed Mar 15, 2020
1 parent f60a0a2 commit 2b8ea52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
32 changes: 24 additions & 8 deletions django_test_migrations/sql.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# -*- 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
from django.db.backends.base.base import BaseDatabaseWrapper

_Connection = Union[DefaultConnectionProxy, BaseDatabaseWrapper]

DJANGO_MIGRATIONS_TABLE = 'django_migrations'
DJANGO_MIGRATIONS_TABLE_NAME = 'django_migrations'


def drop_models_tables(
Expand Down Expand Up @@ -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]:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b8ea52

Please sign in to comment.