Skip to content

Commit ac5b272

Browse files
committed
add ability to customise TriggerFactory
1 parent 81d1553 commit ac5b272

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Release 2.x (dev)
22
-------------
33
* Removes code producing DeprecationError
44
* add `AUTO_CREATE_TRIGGERS` and deprecate `MANUAL_TRIGGERS`
5+
* add support for Postgres 13
6+
57

68
Release 2.2
79
-------------

src/concurrency/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
from django.core.exceptions import ImproperlyConfigured
44
from django.test.signals import setting_changed
5+
from django.utils.module_loading import import_string
56

67
from .compat import get_callable
78

89
# List Editable Policy
910
# 0 do not save updated records, save others, show message to the user
1011
# 1 abort whole transaction
12+
from . import triggers
13+
from .utils import fqn
1114

1215
CONCURRENCY_LIST_EDITABLE_POLICY_SILENT = 1
1316
CONCURRENCY_LIST_EDITABLE_POLICY_ABORT_ALL = 2
@@ -27,6 +30,11 @@ class AppSettings(object):
2730
'CALLBACK': 'concurrency.views.callback',
2831
'HANDLER409': 'concurrency.views.conflict',
2932
'VERSION_FIELD_REQUIRED': True,
33+
'TRIGGER_FACTORIES': {'postgresql': fqn(triggers.PostgreSQL),
34+
'mysql': fqn(triggers.MySQL),
35+
'sqlite3': fqn(triggers.Sqlite3),
36+
'sqlite': fqn(triggers.Sqlite3),
37+
}
3038
}
3139

3240
def __init__(self, prefix):
@@ -65,6 +73,13 @@ def _set_attr(self, prefix_name, value):
6573
warnings.warn("MANUAL_TRIGGERS is deprecated and will be removed in 2.5. Use AUTO_CREATE_TRIGGERS",
6674
category=DeprecationWarning)
6775
self.AUTO_CREATE_TRIGGERS = not value
76+
elif name == "TRIGGER_FACTORIES":
77+
self.TRIGGER_FACTORIES = {}
78+
for k, v in value.items():
79+
try:
80+
self.TRIGGER_FACTORIES[k] = import_string(v)
81+
except ImportError:
82+
raise ImproperlyConfigured(f"Unable to load {k} TriggerFactory. Invalid fqn {v}")
6883

6984
setattr(self, name, value)
7085

src/concurrency/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __contains__(self, field):
7676

7777
_TRIGGERS = TriggerRegistry()
7878

79-
if not conf.MANUAL_TRIGGERS:
79+
if conf.AUTO_CREATE_TRIGGERS:
8080
post_migrate.connect(post_syncdb_concurrency_handler, dispatch_uid='post_syncdb_concurrency_handler')
8181

8282

src/concurrency/triggers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.db import connections, router
55
from django.db.utils import DatabaseError
66

7+
from .config import conf
78
from .fields import _TRIGGERS # noqa
89

910

@@ -163,10 +164,7 @@ class MySQL(TriggerFactory):
163164

164165
def factory(conn):
165166
try:
166-
return {'postgresql': PostgreSQL,
167-
'mysql': MySQL,
168-
'sqlite3': Sqlite3,
169-
'sqlite': Sqlite3,
170-
}[conn.vendor](conn)
167+
mapping = conf.TRIGGER_FACTORIES
168+
return mapping[conn.vendor](conn)
171169
except KeyError: # pragma: no cover
172170
raise ValueError('{} is not supported by TriggerVersionField'.format(conn))

0 commit comments

Comments
 (0)