Skip to content

Commit 36dd049

Browse files
author
Ross Mechanic
authored
Fix non-backward compatible issue from 2.7.0 from GH-507 (#547)
* Fix non-backward compatible issue from 2.7.0 from GH-507 * Add this PR to changes * Fixed kyle's comments
1 parent f928c81 commit 36dd049

File tree

5 files changed

+81
-50
lines changed

5 files changed

+81
-50
lines changed

CHANGES.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
Changes
22
=======
33

4-
- remove reference to vendored library ``django.utils.six`` in favor of ``six`` (gh-526)
5-
- Fixed hardcoded history manager (gh-540)
6-
74
Unreleased
8-
----------
9-
- Added the possibility to create a relation to the original model
10-
11-
2.7.1 (2019-03-26)
125
------------------
13-
- Added routing for saving historical records to separate databases if necessary.
6+
- Added the possibility to create a relation to the original model (gh-536)
7+
- Fix router backward-compatibility issue with 2.7.0 (gh-539, gh-547)
8+
- Fix hardcoded history manager (gh-542)
9+
- Replace deprecated `django.utils.six` with `six` (gh-526)
1410

1511
2.7.0 (2019-01-16)
1612
------------------
17-
- Add support for ``using`` chained manager method and save/delete keyword argument (gh-507)
13+
- \* Add support for ``using`` chained manager method and save/delete keyword argument (gh-507)
1814
- Added management command ``clean_duplicate_history`` to remove duplicate history entries (gh-483)
1915
- Updated most_recent to work with excluded_fields (gh-477)
2016
- Fixed bug that prevented self-referential foreign key from using ``'self'`` (gh-513)
2117
- Added ability to track custom user with explicit custom ``history_user_id_field`` (gh-511)
2218
- Don't resolve relationships for history objects (gh-479)
2319
- Reorganization of docs (gh-510)
2420

21+
\* NOTE: This change was not backward compatible for users using routers to write
22+
history tables to a separate database from their base tables. This issue is fixed in
23+
2.7.1.
24+
2525
2.6.0 (2018-12-12)
2626
------------------
2727
- Add ``app`` parameter to the constructor of ``HistoricalRecords`` (gh-486)

docs/multiple_dbs.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ class MyModel(models.Model):
5353

5454
If set to `True`, migrations and audit
5555
events will be sent to the same database as the base model. If `False`, they
56-
will be sent to the place specified by the database router.
56+
will be sent to the place specified by the database router. The default value is `False`.

simple_history/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(
7979
history_user_getter=_history_user_getter,
8080
history_user_setter=_history_user_setter,
8181
related_name=None,
82-
use_base_model_db=True,
82+
use_base_model_db=False,
8383
):
8484
self.user_set_verbose_name = verbose_name
8585
self.user_related_name = user_related_name

simple_history/tests/models.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,19 @@ class ModelWithHistoryInDifferentApp(models.Model):
364364

365365
class ModelWithHistoryInDifferentDb(models.Model):
366366
name = models.CharField(max_length=30)
367-
history = HistoricalRecords(use_base_model_db=False)
367+
history = HistoricalRecords()
368+
369+
370+
class ModelWithHistoryUsingBaseModelDb(models.Model):
371+
name = models.CharField(max_length=30)
372+
history = HistoricalRecords(use_base_model_db=True)
373+
374+
375+
class ModelWithFkToModelWithHistoryUsingBaseModelDb(models.Model):
376+
fk = models.ForeignKey(
377+
ModelWithHistoryUsingBaseModelDb, on_delete=models.CASCADE, null=True
378+
)
379+
history = HistoricalRecords(use_base_model_db=True)
368380

369381

370382
###############################################################################

simple_history/tests/tests/test_models.py

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from __future__ import unicode_literals
22

33
import unittest
4-
5-
import django
64
import uuid
75
import warnings
86
from datetime import datetime, timedelta
7+
8+
import django
99
from django.apps import apps
1010
from django.contrib.auth import get_user_model
1111
from django.core.exceptions import ObjectDoesNotExist
@@ -18,22 +18,19 @@
1818
from simple_history import register
1919
from simple_history.exceptions import RelatedNameConflictError
2020
from simple_history.models import HistoricalRecords, ModelChange
21-
from simple_history.signals import (
22-
pre_create_historical_record,
23-
post_create_historical_record,
24-
)
21+
from simple_history.signals import pre_create_historical_record
2522
from simple_history.tests.custom_user.models import CustomUser
2623
from simple_history.tests.tests.utils import (
2724
database_router_override_settings,
28-
middleware_override_settings,
2925
database_router_override_settings_history_in_diff_db,
26+
middleware_override_settings,
3027
)
3128
from simple_history.utils import get_history_model_for_model
3229
from simple_history.utils import update_change_reason
3330
from ..external.models import (
3431
ExternalModel,
35-
ExternalModelWithCustomUserIdField,
3632
ExternalModelRegistered,
33+
ExternalModelWithCustomUserIdField,
3734
)
3835
from ..models import (
3936
AbstractBase,
@@ -66,7 +63,9 @@
6663
HistoricalPollWithHistoricalIPAddress,
6764
HistoricalState,
6865
Library,
66+
ModelWithFkToModelWithHistoryUsingBaseModelDb,
6967
ModelWithHistoryInDifferentDb,
68+
ModelWithHistoryUsingBaseModelDb,
7069
MultiOneToOne,
7170
Person,
7271
Place,
@@ -1262,78 +1261,98 @@ class MultiDBWithUsingTest(TestCase):
12621261
db_name = "other"
12631262

12641263
def test_multidb_with_using_not_on_default(self):
1265-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1266-
self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1")
1264+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1265+
name="1-84356-028-1"
1266+
)
1267+
self.assertRaises(ObjectDoesNotExist, model.history.get, name="1-84356-028-1")
12671268

12681269
def test_multidb_with_using_is_on_dbtwo(self):
1269-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1270+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1271+
name="1-84356-028-1"
1272+
)
12701273
try:
1271-
book.history.using(self.db_name).get(isbn="1-84356-028-1")
1274+
model.history.using(self.db_name).get(name="1-84356-028-1")
12721275
except ObjectDoesNotExist:
12731276
self.fail("ObjectDoesNotExist unexpectedly raised.")
12741277

12751278
def test_multidb_with_using_and_fk_not_on_default(self):
1276-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1277-
library = Library.objects.using(self.db_name).create(book=book)
1278-
self.assertRaises(ObjectDoesNotExist, library.history.get, book=book)
1279+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1280+
name="1-84356-028-1"
1281+
)
1282+
parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb.objects.using(
1283+
self.db_name
1284+
).create(fk=model)
1285+
self.assertRaises(ObjectDoesNotExist, parent_model.history.get, fk=model)
12791286

12801287
def test_multidb_with_using_and_fk_on_dbtwo(self):
1281-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1282-
library = Library.objects.using(self.db_name).create(book=book)
1288+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1289+
name="1-84356-028-1"
1290+
)
1291+
parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb.objects.using(
1292+
self.db_name
1293+
).create(fk=model)
12831294
try:
1284-
library.history.using(self.db_name).get(book=book)
1295+
parent_model.history.using(self.db_name).get(fk=model)
12851296
except ObjectDoesNotExist:
12861297
self.fail("ObjectDoesNotExist unexpectedly raised.")
12871298

12881299
def test_multidb_with_using_keyword_in_save_not_on_default(self):
1289-
book = Book(isbn="1-84356-028-1")
1290-
book.save(using=self.db_name)
1291-
self.assertRaises(ObjectDoesNotExist, book.history.get, isbn="1-84356-028-1")
1300+
model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1")
1301+
model.save(using=self.db_name)
1302+
self.assertRaises(ObjectDoesNotExist, model.history.get, name="1-84356-028-1")
12921303

12931304
def test_multidb_with_using_keyword_in_save_on_dbtwo(self):
1294-
book = Book(isbn="1-84356-028-1")
1295-
book.save(using=self.db_name)
1305+
model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1")
1306+
model.save(using=self.db_name)
12961307
try:
1297-
book.history.using(self.db_name).get(isbn="1-84356-028-1")
1308+
model.history.using(self.db_name).get(name="1-84356-028-1")
12981309
except ObjectDoesNotExist:
12991310
self.fail("ObjectDoesNotExist unexpectedly raised.")
13001311

13011312
def test_multidb_with_using_keyword_in_save_with_fk(self):
1302-
book = Book(isbn="1-84356-028-1")
1303-
book.save(using=self.db_name)
1304-
library = Library(book=book)
1305-
library.save(using=self.db_name)
1313+
model = ModelWithHistoryUsingBaseModelDb(name="1-84356-028-1")
1314+
model.save(using=self.db_name)
1315+
parent_model = ModelWithFkToModelWithHistoryUsingBaseModelDb(fk=model)
1316+
parent_model.save(using=self.db_name)
13061317
# assert not created on default
1307-
self.assertRaises(ObjectDoesNotExist, library.history.get, book=book)
1318+
self.assertRaises(ObjectDoesNotExist, parent_model.history.get, fk=model)
13081319
# assert created on dbtwo
13091320
try:
1310-
library.history.using(self.db_name).get(book=book)
1321+
parent_model.history.using(self.db_name).get(fk=model)
13111322
except ObjectDoesNotExist:
13121323
self.fail("ObjectDoesNotExist unexpectedly raised.")
13131324

13141325
def test_multidb_with_using_keyword_in_save_and_update(self):
1315-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1316-
book.save(using=self.db_name)
1326+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1327+
name="1-84356-028-1"
1328+
)
1329+
model.save(using=self.db_name)
13171330
self.assertEqual(
13181331
["+", "~"],
13191332
[
13201333
obj.history_type
1321-
for obj in book.history.using(self.db_name)
1334+
for obj in model.history.using(self.db_name)
13221335
.all()
13231336
.order_by("history_date")
13241337
],
13251338
)
13261339

13271340
def test_multidb_with_using_keyword_in_save_and_delete(self):
1328-
HistoricalBook = get_history_model_for_model(Book)
1329-
book = Book.objects.using(self.db_name).create(isbn="1-84356-028-1")
1330-
book.save(using=self.db_name)
1331-
book.delete(using=self.db_name)
1341+
HistoricalModelWithHistoryUseBaseModelDb = get_history_model_for_model(
1342+
ModelWithHistoryUsingBaseModelDb
1343+
)
1344+
model = ModelWithHistoryUsingBaseModelDb.objects.using(self.db_name).create(
1345+
name="1-84356-028-1"
1346+
)
1347+
model.save(using=self.db_name)
1348+
model.delete(using=self.db_name)
13321349
self.assertEqual(
13331350
["+", "~", "-"],
13341351
[
13351352
obj.history_type
1336-
for obj in HistoricalBook.objects.using(self.db_name)
1353+
for obj in HistoricalModelWithHistoryUseBaseModelDb.objects.using(
1354+
self.db_name
1355+
)
13371356
.all()
13381357
.order_by("history_date")
13391358
],

0 commit comments

Comments
 (0)