|
5 | 5 | import copy |
6 | 6 | import re |
7 | 7 | import sys |
| 8 | +import warnings |
8 | 9 | from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union |
9 | 10 |
|
10 | 11 | import django |
11 | 12 | import pgtrigger |
12 | 13 | import pgtrigger.core |
13 | 14 | from django.apps import apps |
| 15 | +from django.conf import settings |
14 | 16 | from django.db import connections, models |
15 | 17 | from django.db.models import sql |
16 | 18 | from django.db.models.fields.related import RelatedField |
@@ -46,6 +48,26 @@ def quote(self, obj): |
46 | 48 | _registered_trackers = {} |
47 | 49 |
|
48 | 50 |
|
| 51 | +# avoid default trigger name duplication |
| 52 | +def _simplify_trigger_names() -> bool: |
| 53 | + """Return True if simplified trigger names are enabled. |
| 54 | +
|
| 55 | + Reads PGHISTORY_SIMPLIFY_TRIGGER_NAMES from Django settings at call time |
| 56 | + so that override_settings() works correctly in tests and avoids emitting |
| 57 | + a DeprecationWarning at module import time. |
| 58 | + """ |
| 59 | + enabled = getattr(settings, "PGHISTORY_SIMPLIFY_TRIGGER_NAMES", False) |
| 60 | + if not enabled: |
| 61 | + warnings.warn( |
| 62 | + "Trigger names like 'update_update' are deprecated. " |
| 63 | + "Set PGHISTORY_SIMPLIFY_TRIGGER_NAMES=True to enable simplified names. " |
| 64 | + "This will become the default in a future release.", |
| 65 | + DeprecationWarning, |
| 66 | + stacklevel=2, |
| 67 | + ) |
| 68 | + return enabled |
| 69 | + |
| 70 | + |
49 | 71 | class Tracker: |
50 | 72 | """For tracking an event when a condition happens on a model.""" |
51 | 73 |
|
@@ -107,9 +129,20 @@ def __init__( |
107 | 129 | self.condition = condition if condition is not constants.UNSET else self.condition |
108 | 130 | self.operation = operation or self.operation |
109 | 131 | self.row = row or self.row |
110 | | - self.trigger_name = trigger_name or self.trigger_name or f"{self.label}_{self.operation}" |
111 | 132 | self.level = level if level is not None else self.level |
112 | 133 |
|
| 134 | + simplify = _simplify_trigger_names() |
| 135 | + |
| 136 | + if trigger_name: |
| 137 | + self.trigger_name = trigger_name |
| 138 | + elif self.trigger_name: |
| 139 | + self.trigger_name = self.trigger_name |
| 140 | + else: |
| 141 | + if simplify and self.label == self.operation: |
| 142 | + self.trigger_name = str(self.operation) |
| 143 | + else: |
| 144 | + self.trigger_name = f"{self.label}_{self.operation}" |
| 145 | + |
113 | 146 | if self.condition is constants.UNSET: |
114 | 147 | self.condition = pgtrigger.AnyChange() if self.operation == pgtrigger.Update else None |
115 | 148 |
|
|
0 commit comments