Skip to content

Commit 2134f97

Browse files
committed
Support model inheritance
1 parent 4be91e2 commit 2134f97

File tree

8 files changed

+48
-7
lines changed

8 files changed

+48
-7
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ env:
1919
- TOXENV=py36-django20-sqlite
2020
- TOXENV=py36-django20-mysql
2121
- TOXENV=py36-django20-postgresql
22-
- TOXENV=checkqa
22+
- TOXENV=qa
2323
install:
2424
- pip install -U pip
2525
- pip install tox codecov

dbdiff/sequence.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def pk_sequence_get(model):
99
continue
1010
if not isinstance(field, models.AutoField):
1111
continue
12-
return field.db_column or field.column
12+
return (field.db_column or field.column, field.model._meta.db_table)
13+
return (None, None)
1314

1415

1516
def sequence_reset(model):
@@ -21,7 +22,7 @@ def sequence_reset(model):
2122
supporting pre-existing models which could have been created by a
2223
migration.
2324
"""
24-
pk_field = pk_sequence_get(model)
25+
pk_field, table = pk_sequence_get(model)
2526
if not pk_field:
2627
return
2728

@@ -45,12 +46,12 @@ def sequence_reset(model):
4546
cursor = connection.cursor()
4647
cursor.execute(
4748
'SELECT MAX({column}) + 1 FROM {table}'.format(
48-
column=pk_field, table=model._meta.db_table
49+
column=pk_field, table=table
4950
)
5051
)
5152
result = cursor.fetchone()[0] or 0
5253
reset = 'ALTER TABLE {table} AUTO_INCREMENT = %s' % result
5354

5455
connection.cursor().execute(
55-
reset.format(column=pk_field, table=model._meta.db_table)
56+
reset.format(column=pk_field, table=table)
5657
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test app for model inheritance."""

dbdiff/tests/inheritance/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.db import models
2+
3+
4+
class Parent(models.Model):
5+
pass
6+
7+
8+
class Child(Parent):
9+
name = models.CharField(max_length=50)

dbdiff/tests/project/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
'dbdiff.tests.decimal_test',
4343
'dbdiff.tests.nonintpk',
44+
'dbdiff.tests.inheritance',
4445
)
4546

4647
MIDDLEWARE_CLASSES = (

dbdiff/tests/test_mixin.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,28 @@
6363
"model": "contenttypes.contenttype",
6464
"pk": 8
6565
},
66+
{
67+
"fields": {
68+
"app_label": "inheritance",
69+
"model": "parent"
70+
},
71+
"model": "contenttypes.contenttype",
72+
"pk": 9
73+
},
74+
{
75+
"fields": {
76+
"app_label": "inheritance",
77+
"model": "child"
78+
},
79+
"model": "contenttypes.contenttype",
80+
"pk": 10
81+
},
6682
{
6783
"fields": {
6884
"app_label": "",
6985
"model": ""
7086
},
7187
"model": "contenttypes.contenttype",
72-
"pk": 9
88+
"pk": 11
7389
}
7490
]

dbdiff/tests/test_plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dbdiff.tests.decimal_test.models import TestModel as DecimalModel
2+
from dbdiff.tests.inheritance.models import Child, Parent
23
from dbdiff.tests.nonintpk.models import Nonintpk
34

45
import pytest
@@ -19,3 +20,15 @@ def test_still_first_pk():
1920
@pytest.mark.dbdiff(models=[DecimalModel, Nonintpk])
2021
def test_doesnt_reset_nonintpk_which_would_fail():
2122
assert DecimalModel.objects.count() == 0
23+
24+
25+
@pytest.mark.dbdiff(models=[Child])
26+
def test_inheritance_sequence_reset():
27+
assert Child.objects.count() == 0
28+
assert Child.objects.create(name='1').pk == 1
29+
30+
31+
@pytest.mark.dbdiff(models=[Child])
32+
def test_inheritance_sequence_reset_again():
33+
assert Parent.objects.count() == 0
34+
assert Child.objects.create(name='1').pk == 1

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ setenv =
3030
mysql: DJANGO_SETTINGS_MODULE=dbdiff.tests.project.settings_mysql
3131
passenv = TEST_* DBDIFF_*
3232

33-
[testenv:checkqa]
33+
[testenv:qa]
3434
basepython = python2.7
3535
commands =
3636
flake8 --show-source --exclude tests --max-complexity=7 --ignore=D203 dbdiff

0 commit comments

Comments
 (0)