django-bulk-hooks 0.1.280__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of django-bulk-hooks might be problematic. Click here for more details.

@@ -1,4 +1,60 @@
1
+ import logging
2
+
1
3
  from django_bulk_hooks.handler import Hook as HookClass
2
4
  from django_bulk_hooks.manager import BulkHookManager
5
+ from django_bulk_hooks.factory import (
6
+ set_hook_factory,
7
+ set_default_hook_factory,
8
+ configure_hook_container,
9
+ configure_nested_container,
10
+ clear_hook_factories,
11
+ create_hook_instance,
12
+ is_container_configured,
13
+ )
14
+ from django_bulk_hooks.constants import DEFAULT_BULK_UPDATE_BATCH_SIZE
15
+ from django_bulk_hooks.changeset import ChangeSet, RecordChange
16
+ from django_bulk_hooks.dispatcher import get_dispatcher, HookDispatcher
17
+ from django_bulk_hooks.helpers import (
18
+ build_changeset_for_create,
19
+ build_changeset_for_update,
20
+ build_changeset_for_delete,
21
+ dispatch_hooks_for_operation,
22
+ )
23
+
24
+ # Service layer (NEW architecture)
25
+ from django_bulk_hooks.operations import (
26
+ BulkOperationCoordinator,
27
+ ModelAnalyzer,
28
+ BulkExecutor,
29
+ MTIHandler,
30
+ )
31
+
32
+ # Add NullHandler to prevent logging messages if the application doesn't configure logging
33
+ logging.getLogger(__name__).addHandler(logging.NullHandler())
3
34
 
4
- __all__ = ["BulkHookManager", "HookClass"]
35
+ __all__ = [
36
+ "BulkHookManager",
37
+ "HookClass",
38
+ "set_hook_factory",
39
+ "set_default_hook_factory",
40
+ "configure_hook_container",
41
+ "configure_nested_container",
42
+ "clear_hook_factories",
43
+ "create_hook_instance",
44
+ "is_container_configured",
45
+ "DEFAULT_BULK_UPDATE_BATCH_SIZE",
46
+ # Dispatcher-centric architecture
47
+ "ChangeSet",
48
+ "RecordChange",
49
+ "get_dispatcher",
50
+ "HookDispatcher",
51
+ "build_changeset_for_create",
52
+ "build_changeset_for_update",
53
+ "build_changeset_for_delete",
54
+ "dispatch_hooks_for_operation",
55
+ # Service layer (composition-based architecture)
56
+ "BulkOperationCoordinator",
57
+ "ModelAnalyzer",
58
+ "BulkExecutor",
59
+ "MTIHandler",
60
+ ]
@@ -0,0 +1,230 @@
1
+ """
2
+ ChangeSet and RecordChange classes for Salesforce-style hook context.
3
+
4
+ Provides a first-class abstraction for tracking changes in bulk operations,
5
+ similar to Salesforce's Hook.new, Hook.old, and Hook.newMap.
6
+ """
7
+
8
+
9
+ class RecordChange:
10
+ """
11
+ Represents a single record change with old/new state.
12
+
13
+ Similar to accessing Hook.newMap.get(id) in Salesforce, but with
14
+ additional conveniences like O(1) field change detection.
15
+ """
16
+
17
+ def __init__(self, new_record, old_record=None, changed_fields=None):
18
+ """
19
+ Initialize a RecordChange.
20
+
21
+ Args:
22
+ new_record: The new/current state of the record
23
+ old_record: The old/previous state of the record (None for creates)
24
+ changed_fields: Optional pre-computed set of changed field names.
25
+ If None, will be computed lazily on first access.
26
+ """
27
+ self.new_record = new_record
28
+ self.old_record = old_record
29
+ self._changed_fields = changed_fields
30
+ self._pk = getattr(new_record, "pk", None) if new_record else None
31
+
32
+ @property
33
+ def pk(self):
34
+ """Primary key of the record."""
35
+ return self._pk
36
+
37
+ @property
38
+ def changed_fields(self):
39
+ """
40
+ Set of field names that have changed.
41
+
42
+ Computed lazily on first access and cached for O(1) subsequent checks.
43
+ """
44
+ if self._changed_fields is None:
45
+ self._changed_fields = self._compute_changed_fields()
46
+ return self._changed_fields
47
+
48
+ def has_changed(self, field_name):
49
+ """
50
+ O(1) check if a specific field has changed.
51
+
52
+ Args:
53
+ field_name: Name of the field to check
54
+
55
+ Returns:
56
+ True if the field value changed, False otherwise
57
+ """
58
+ return field_name in self.changed_fields
59
+
60
+ def get_old_value(self, field_name):
61
+ """
62
+ Get the old value for a field.
63
+
64
+ Args:
65
+ field_name: Name of the field
66
+
67
+ Returns:
68
+ The old value, or None if no old record exists
69
+ """
70
+ if self.old_record is None:
71
+ return None
72
+ return getattr(self.old_record, field_name, None)
73
+
74
+ def get_new_value(self, field_name):
75
+ """
76
+ Get the new value for a field.
77
+
78
+ Args:
79
+ field_name: Name of the field
80
+
81
+ Returns:
82
+ The new value
83
+ """
84
+ return getattr(self.new_record, field_name, None)
85
+
86
+ def _compute_changed_fields(self):
87
+ """
88
+ Compute which fields have changed between old and new records.
89
+
90
+ Uses Django's field.get_prep_value() for proper comparison that
91
+ handles database-level transformations.
92
+
93
+ Returns:
94
+ Set of field names that have changed
95
+ """
96
+ if self.old_record is None:
97
+ return set()
98
+
99
+ changed = set()
100
+ model_cls = self.new_record.__class__
101
+
102
+ for field in model_cls._meta.fields:
103
+ # Skip primary key - it shouldn't change
104
+ if field.primary_key:
105
+ continue
106
+
107
+ old_val = getattr(self.old_record, field.name, None)
108
+ new_val = getattr(self.new_record, field.name, None)
109
+
110
+ # Use field's get_prep_value for proper comparison
111
+ # This handles database-level transformations (e.g., timezone conversions)
112
+ try:
113
+ old_prep = field.get_prep_value(old_val)
114
+ new_prep = field.get_prep_value(new_val)
115
+ if old_prep != new_prep:
116
+ changed.add(field.name)
117
+ except Exception:
118
+ # Fallback to direct comparison if get_prep_value fails
119
+ if old_val != new_val:
120
+ changed.add(field.name)
121
+
122
+ return changed
123
+
124
+
125
+ class ChangeSet:
126
+ """
127
+ Collection of RecordChanges for a bulk operation.
128
+
129
+ Similar to Salesforce's Hook context (Hook.new, Hook.old, Hook.newMap),
130
+ but enhanced for Python's bulk operations paradigm with O(1) lookups and
131
+ additional metadata.
132
+ """
133
+
134
+ def __init__(self, model_cls, changes, operation_type, operation_meta=None):
135
+ """
136
+ Initialize a ChangeSet.
137
+
138
+ Args:
139
+ model_cls: The Django model class
140
+ changes: List of RecordChange instances
141
+ operation_type: Type of operation ('create', 'update', 'delete')
142
+ operation_meta: Optional dict of additional metadata (e.g., update_kwargs)
143
+ """
144
+ self.model_cls = model_cls
145
+ self.changes = changes # List[RecordChange]
146
+ self.operation_type = operation_type
147
+ self.operation_meta = operation_meta or {}
148
+
149
+ # Build PK -> RecordChange map for O(1) lookups (like Hook.newMap)
150
+ self._pk_to_change = {c.pk: c for c in changes if c.pk is not None}
151
+
152
+ @property
153
+ def new_records(self):
154
+ """
155
+ List of new/current record states.
156
+
157
+ Similar to Hook.new in Salesforce.
158
+ """
159
+ return [c.new_record for c in self.changes if c.new_record is not None]
160
+
161
+ @property
162
+ def old_records(self):
163
+ """
164
+ List of old/previous record states.
165
+
166
+ Similar to Hook.old in Salesforce.
167
+ Only includes records that have old states (excludes creates).
168
+ """
169
+ return [c.old_record for c in self.changes if c.old_record is not None]
170
+
171
+ def has_field_changed(self, pk, field_name):
172
+ """
173
+ O(1) check if a field changed for a specific record.
174
+
175
+ Args:
176
+ pk: Primary key of the record
177
+ field_name: Name of the field to check
178
+
179
+ Returns:
180
+ True if the field changed, False otherwise
181
+ """
182
+ change = self._pk_to_change.get(pk)
183
+ return change.has_changed(field_name) if change else False
184
+
185
+ def get_old_value(self, pk, field_name):
186
+ """
187
+ Get the old value for a specific record and field.
188
+
189
+ Args:
190
+ pk: Primary key of the record
191
+ field_name: Name of the field
192
+
193
+ Returns:
194
+ The old value, or None if not found
195
+ """
196
+ change = self._pk_to_change.get(pk)
197
+ return change.get_old_value(field_name) if change else None
198
+
199
+ def get_new_value(self, pk, field_name):
200
+ """
201
+ Get the new value for a specific record and field.
202
+
203
+ Args:
204
+ pk: Primary key of the record
205
+ field_name: Name of the field
206
+
207
+ Returns:
208
+ The new value, or None if not found
209
+ """
210
+ change = self._pk_to_change.get(pk)
211
+ return change.get_new_value(field_name) if change else None
212
+
213
+ def chunk(self, chunk_size):
214
+ """
215
+ Split ChangeSet into smaller chunks for memory-efficient processing.
216
+
217
+ Useful for processing very large bulk operations without loading
218
+ all data into memory at once.
219
+
220
+ Args:
221
+ chunk_size: Number of changes per chunk
222
+
223
+ Yields:
224
+ ChangeSet instances, each with up to chunk_size changes
225
+ """
226
+ for i in range(0, len(self.changes), chunk_size):
227
+ chunk_changes = self.changes[i : i + chunk_size]
228
+ yield ChangeSet(
229
+ self.model_cls, chunk_changes, self.operation_type, self.operation_meta
230
+ )
@@ -6,12 +6,53 @@ logger = logging.getLogger(__name__)
6
6
  def resolve_dotted_attr(instance, dotted_path):
7
7
  """
8
8
  Recursively resolve a dotted attribute path, e.g., "type.category".
9
+
10
+ CRITICAL: For foreign key fields, uses attname to access the ID directly
11
+ to avoid hooking Django's descriptor protocol which causes N+1 queries.
9
12
  """
10
- for attr in dotted_path.split("."):
11
- if instance is None:
13
+ # For simple field access (no dots), use optimized field access
14
+ if "." not in dotted_path:
15
+ try:
16
+ # Get the field from the model's meta to check if it's a foreign key
17
+ field = instance._meta.get_field(dotted_path)
18
+ if field.is_relation and not field.many_to_many:
19
+ # For foreign key fields, use attname to get the ID directly
20
+ # This avoids hooking Django's descriptor protocol
21
+ return getattr(instance, field.attname, None)
22
+ else:
23
+ # For regular fields, use normal getattr
24
+ return getattr(instance, dotted_path, None)
25
+ except Exception:
26
+ # If field lookup fails, fall back to normal getattr
27
+ return getattr(instance, dotted_path, None)
28
+
29
+ # For dotted paths, traverse the relationship chain with FK optimization
30
+ current_instance = instance
31
+ for i, attr in enumerate(dotted_path.split(".")):
32
+ if current_instance is None:
12
33
  return None
13
- instance = getattr(instance, attr, None)
14
- return instance
34
+
35
+ try:
36
+ # Check if this is the last attribute and if it's a FK field
37
+ is_last_attr = i == len(dotted_path.split(".")) - 1
38
+ if is_last_attr and hasattr(current_instance, "_meta"):
39
+ try:
40
+ field = current_instance._meta.get_field(attr)
41
+ if field.is_relation and not field.many_to_many:
42
+ # Use attname for the final FK field access
43
+ current_instance = getattr(
44
+ current_instance, field.attname, None
45
+ )
46
+ continue
47
+ except:
48
+ pass # Fall through to normal getattr
49
+
50
+ # Normal getattr for non-FK fields or when FK optimization fails
51
+ current_instance = getattr(current_instance, attr, None)
52
+ except Exception:
53
+ current_instance = None
54
+
55
+ return current_instance
15
56
 
16
57
 
17
58
  class HookCondition:
@@ -56,6 +97,7 @@ class IsEqual(HookCondition):
56
97
 
57
98
  def check(self, instance, original_instance=None):
58
99
  current = resolve_dotted_attr(instance, self.field)
100
+
59
101
  if self.only_on_change:
60
102
  if original_instance is None:
61
103
  return False
@@ -73,15 +115,11 @@ class HasChanged(HookCondition):
73
115
  def check(self, instance, original_instance=None):
74
116
  if not original_instance:
75
117
  return False
76
-
118
+
77
119
  current = resolve_dotted_attr(instance, self.field)
78
120
  previous = resolve_dotted_attr(original_instance, self.field)
79
-
80
- result = (current != previous) == self.has_changed
81
- # Only log when there's an actual change to reduce noise
82
- if result:
83
- logger.debug(f"HasChanged {self.field} detected change on instance {getattr(instance, 'pk', 'No PK')}")
84
- return result
121
+
122
+ return (current != previous) == self.has_changed
85
123
 
86
124
 
87
125
  class WasEqual(HookCondition):
@@ -7,3 +7,7 @@ AFTER_DELETE = "after_delete"
7
7
  VALIDATE_CREATE = "validate_create"
8
8
  VALIDATE_UPDATE = "validate_update"
9
9
  VALIDATE_DELETE = "validate_delete"
10
+
11
+ # Default batch size for bulk_update operations to prevent massive SQL statements
12
+ # This prevents PostgreSQL from crashing when updating large datasets with hooks
13
+ DEFAULT_BULK_UPDATE_BATCH_SIZE = 1000
@@ -1,15 +1,13 @@
1
- import threading
2
- from collections import deque
3
- from django_bulk_hooks.handler import hook_vars
4
-
1
+ """
2
+ Thread-local context management for bulk operations.
5
3
 
6
- _hook_context = threading.local()
4
+ This module provides thread-safe storage for operation state like
5
+ bypass_hooks flags and bulk update metadata.
6
+ """
7
7
 
8
+ import threading
8
9
 
9
- def get_hook_queue():
10
- if not hasattr(_hook_context, "queue"):
11
- _hook_context.queue = deque()
12
- return _hook_context.queue
10
+ _hook_context = threading.local()
13
11
 
14
12
 
15
13
  def set_bypass_hooks(bypass_hooks):
@@ -19,14 +17,14 @@ def set_bypass_hooks(bypass_hooks):
19
17
 
20
18
  def get_bypass_hooks():
21
19
  """Get the current bypass_hooks state for the current thread."""
22
- return getattr(_hook_context, 'bypass_hooks', False)
20
+ return getattr(_hook_context, "bypass_hooks", False)
23
21
 
24
22
 
25
23
  # Thread-local storage for passing per-object field values from bulk_update -> update
26
24
  def set_bulk_update_value_map(value_map):
27
25
  """Store a mapping of {pk: {field_name: value}} for the current thread.
28
26
 
29
- This allows the internal update() call (triggered by Django's bulk_update)
27
+ This allows the internal update() call (hooked by Django's bulk_update)
30
28
  to populate in-memory instances with the concrete values that will be
31
29
  written to the database, instead of Django expression objects like Case/Cast.
32
30
  """
@@ -35,35 +33,24 @@ def set_bulk_update_value_map(value_map):
35
33
 
36
34
  def get_bulk_update_value_map():
37
35
  """Retrieve the mapping {pk: {field_name: value}} for the current thread, if any."""
38
- return getattr(_hook_context, 'bulk_update_value_map', None)
39
-
40
-
41
- class HookContext:
42
- def __init__(self, model, bypass_hooks=False):
43
- self.model = model
44
- self.bypass_hooks = bypass_hooks
45
- # Set the thread-local bypass state when creating a context
46
- set_bypass_hooks(bypass_hooks)
47
-
48
- @property
49
- def is_executing(self):
50
- """
51
- Check if we're currently in a hook execution context.
52
- Similar to Salesforce's Trigger.isExecuting.
53
- Use this to prevent infinite recursion in hooks.
54
- """
55
- return hasattr(hook_vars, 'event') and hook_vars.event is not None
56
-
57
- @property
58
- def current_event(self):
59
- """
60
- Get the current hook event being executed.
61
- """
62
- return getattr(hook_vars, 'event', None)
63
-
64
- @property
65
- def execution_depth(self):
66
- """
67
- Get the current execution depth to detect deep recursion.
68
- """
69
- return getattr(hook_vars, 'depth', 0)
36
+ return getattr(_hook_context, "bulk_update_value_map", None)
37
+
38
+
39
+ def set_bulk_update_active(active):
40
+ """Set whether we're currently in a bulk_update operation."""
41
+ _hook_context.bulk_update_active = active
42
+
43
+
44
+ def get_bulk_update_active():
45
+ """Get whether we're currently in a bulk_update operation."""
46
+ return getattr(_hook_context, "bulk_update_active", False)
47
+
48
+
49
+ def set_bulk_update_batch_size(batch_size):
50
+ """Store the batch_size for the current bulk_update operation."""
51
+ _hook_context.bulk_update_batch_size = batch_size
52
+
53
+
54
+ def get_bulk_update_batch_size():
55
+ """Get the batch_size for the current bulk_update operation."""
56
+ return getattr(_hook_context, "bulk_update_batch_size", None)
@@ -0,0 +1,145 @@
1
+ """
2
+ Debug utilities for tracking N+1 queries and database performance.
3
+ """
4
+
5
+ import logging
6
+ import time
7
+ from functools import wraps
8
+ from django.db import connection
9
+ from django.conf import settings
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ def track_queries(func):
15
+ """
16
+ Decorator to track database queries during function execution.
17
+ """
18
+
19
+ @wraps(func)
20
+ def wrapper(*args, **kwargs):
21
+ # Reset query count
22
+ initial_queries = len(connection.queries)
23
+ initial_time = time.time()
24
+
25
+ logger.debug(
26
+ f"QUERY DEBUG: Starting {func.__name__} - initial query count: {initial_queries}"
27
+ )
28
+
29
+ try:
30
+ result = func(*args, **kwargs)
31
+
32
+ final_queries = len(connection.queries)
33
+ final_time = time.time()
34
+ query_count = final_queries - initial_queries
35
+ duration = final_time - initial_time
36
+
37
+ logger.debug(
38
+ f"QUERY DEBUG: Completed {func.__name__} - queries executed: {query_count}, duration: {duration:.4f}s"
39
+ )
40
+
41
+ # Log all queries executed during this function
42
+ if query_count > 0:
43
+ logger.debug(f"QUERY DEBUG: Queries executed in {func.__name__}:")
44
+ for i, query in enumerate(connection.queries[initial_queries:], 1):
45
+ logger.debug(
46
+ f"QUERY DEBUG: {i}. {query['sql'][:100]}... (time: {query['time']})"
47
+ )
48
+
49
+ return result
50
+
51
+ except Exception as e:
52
+ final_queries = len(connection.queries)
53
+ query_count = final_queries - initial_queries
54
+ logger.debug(
55
+ f"QUERY DEBUG: Exception in {func.__name__} - queries executed: {query_count}"
56
+ )
57
+ raise
58
+
59
+ return wrapper
60
+
61
+
62
+ def log_query_count(context=""):
63
+ """
64
+ Log the current query count with optional context.
65
+ """
66
+ query_count = len(connection.queries)
67
+ logger.debug(f"QUERY DEBUG: Query count at {context}: {query_count}")
68
+
69
+
70
+ def log_recent_queries(count=5, context=""):
71
+ """
72
+ Log the most recent database queries.
73
+ """
74
+ recent_queries = connection.queries[-count:] if connection.queries else []
75
+ logger.debug(f"QUERY DEBUG: Recent {len(recent_queries)} queries at {context}:")
76
+ for i, query in enumerate(recent_queries, 1):
77
+ logger.debug(
78
+ f"QUERY DEBUG: {i}. {query['sql'][:100]}... (time: {query['time']})"
79
+ )
80
+
81
+
82
+ class QueryTracker:
83
+ """
84
+ Context manager for tracking database queries.
85
+ """
86
+
87
+ def __init__(self, context_name="QueryTracker"):
88
+ self.context_name = context_name
89
+ self.initial_queries = 0
90
+ self.start_time = 0
91
+
92
+ def __enter__(self):
93
+ self.initial_queries = len(connection.queries)
94
+ self.start_time = time.time()
95
+ logger.debug(
96
+ f"QUERY DEBUG: Starting {self.context_name} - initial query count: {self.initial_queries}"
97
+ )
98
+ return self
99
+
100
+ def __exit__(self, exc_type, exc_val, exc_tb):
101
+ final_queries = len(connection.queries)
102
+ final_time = time.time()
103
+ query_count = final_queries - self.initial_queries
104
+ duration = final_time - self.start_time
105
+
106
+ logger.debug(
107
+ f"QUERY DEBUG: Completed {self.context_name} - queries executed: {query_count}, duration: {duration:.4f}s"
108
+ )
109
+
110
+ if query_count > 0:
111
+ logger.debug(f"QUERY DEBUG: Queries executed in {self.context_name}:")
112
+ for i, query in enumerate(connection.queries[self.initial_queries :], 1):
113
+ logger.debug(
114
+ f"QUERY DEBUG: {i}. {query['sql'][:100]}... (time: {query['time']})"
115
+ )
116
+
117
+ return False # Don't suppress exceptions
118
+
119
+
120
+ def enable_django_query_logging():
121
+ """
122
+ Enable Django's built-in query logging.
123
+ """
124
+ if not settings.DEBUG:
125
+ logger.warning("Django query logging can only be enabled in DEBUG mode")
126
+ return
127
+
128
+ # Enable query logging
129
+ settings.LOGGING = {
130
+ "version": 1,
131
+ "disable_existing_loggers": False,
132
+ "handlers": {
133
+ "console": {
134
+ "class": "logging.StreamHandler",
135
+ },
136
+ },
137
+ "loggers": {
138
+ "django.db.backends": {
139
+ "level": "DEBUG",
140
+ "handlers": ["console"],
141
+ },
142
+ },
143
+ }
144
+
145
+ logger.info("Django query logging enabled")