1
1
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
2
2
3
3
import logging
4
+ from contextlib import ExitStack
4
5
5
6
from django .db import close_old_connections , transaction
6
7
7
8
from dj_cqrs .constants import SignalType
8
9
from dj_cqrs .registries import ReplicaRegistry
9
10
10
-
11
11
logger = logging .getLogger ('django-cqrs' )
12
12
13
13
@@ -29,26 +29,26 @@ def route_signal_to_replica_model(signal_type, cqrs_id, instance_data, previous_
29
29
:param str cqrs_id: Replica model CQRS unique identifier.
30
30
:param dict instance_data: Master model data.
31
31
"""
32
- model_cls = ReplicaRegistry .get_model_by_cqrs_id (cqrs_id )
32
+ if signal_type not in (SignalType .DELETE , SignalType .SAVE , SignalType .SYNC ):
33
+ logger .error ('Bad signal type "{}" for CQRS_ID "{}".' .format (signal_type , cqrs_id ))
34
+ return
33
35
36
+ model_cls = ReplicaRegistry .get_model_by_cqrs_id (cqrs_id )
34
37
if model_cls :
35
- close_old_connections ()
38
+ db_is_needed = not model_cls .CQRS_NO_DB_OPERATIONS
39
+ if db_is_needed :
40
+ close_old_connections ()
36
41
37
- if signal_type == SignalType . DELETE :
38
- with transaction . atomic ( savepoint = False ) :
42
+ with transaction . atomic ( savepoint = False ) if db_is_needed else ExitStack () :
43
+ if signal_type == SignalType . DELETE :
39
44
return model_cls .cqrs_delete (instance_data )
40
45
41
- elif signal_type == SignalType .SAVE :
42
- with transaction .atomic (savepoint = False ):
46
+ elif signal_type == SignalType .SAVE :
43
47
return model_cls .cqrs_save (instance_data , previous_data = previous_data )
44
48
45
- elif signal_type == SignalType .SYNC :
46
- with transaction .atomic (savepoint = False ):
49
+ elif signal_type == SignalType .SYNC :
47
50
return model_cls .cqrs_save (
48
51
instance_data ,
49
52
previous_data = previous_data ,
50
53
sync = True ,
51
54
)
52
-
53
- else :
54
- logger .error ('Bad signal type "{}" for CQRS_ID "{}".' .format (signal_type , cqrs_id ))
0 commit comments