Skip to content

Commit 39dcbcd

Browse files
migration
1 parent 769c2f1 commit 39dcbcd

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

dojo/db_migrations/0243_pghistory_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import django.contrib.auth.validators
44
import django.core.validators
55
import django.db.models.deletion
6+
import django.db.models.manager
67
import django.utils.timezone
78
import django_extensions.db.fields
89
import dojo.models
@@ -919,4 +920,19 @@ class Migration(migrations.Migration):
919920
model_name='testevent',
920921
index=models.Index(fields=['pgh_context_id'], name='dojo_testev_pgh_con_e18502_idx'),
921922
),
923+
# DojoEvents proxy model for structured context field access
924+
migrations.CreateModel(
925+
name='DojoEvents',
926+
fields=[
927+
],
928+
options={
929+
'proxy': True,
930+
'indexes': [],
931+
'constraints': [],
932+
},
933+
bases=('pghistory.events',),
934+
managers=[
935+
('no_objects', django.db.models.manager.Manager()),
936+
],
937+
),
922938
]

dojo/management/commands/pghistory_backfill.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def handle(self, *args, **options):
149149
if not dry_run:
150150
# Create events with preserved timestamps from original instances
151151
event_records = []
152+
failed_records = []
152153
for instance in filtered_batch:
153154
try:
154155
# Create event record with all model fields
@@ -184,10 +185,12 @@ def handle(self, *args, **options):
184185
event_records.append(EventModel(**event_data))
185186

186187
except Exception as e:
188+
failed_records.append(instance.id)
187189
logger.error(
188190
f"Failed to prepare event for {model_name} "
189191
f"ID {instance.id}: {e}",
190192
)
193+
# Continue processing other records in the batch
191194

192195
# Bulk create all events in this batch
193196
if event_records:
@@ -218,34 +221,49 @@ def handle(self, *args, **options):
218221
# Re-raise the exception instead of falling back
219222
raise
220223

221-
processed += len(filtered_batch)
224+
# Count only successfully processed records
225+
successful_in_batch = len(event_records)
226+
failed_in_batch = len(failed_records)
227+
processed += successful_in_batch
228+
229+
if failed_in_batch > 0:
230+
self.stdout.write(
231+
self.style.WARNING(
232+
f" Batch {start + 1}-{end}: {successful_in_batch} successful, "
233+
f"{failed_in_batch} failed (IDs: {failed_records[:5]}{'...' if failed_in_batch > 5 else ''})",
234+
),
235+
)
236+
222237
self.stdout.write(
223238
f" Processed {processed:,}/{backfill_count:,} records needing backfill "
224239
f"({processed / backfill_count * 100:.1f}%)",
225240
)
226241

227242
total_processed += processed
228-
self.stdout.write(
229-
self.style.SUCCESS(
230-
f" ✓ Completed {model_name}: {processed:,} records",
231-
),
232-
)
243+
244+
# Show completion summary
245+
if processed < backfill_count:
246+
self.stdout.write(
247+
self.style.WARNING(
248+
f" ⚠ Completed {model_name}: {processed:,}/{backfill_count:,} records "
249+
f"({backfill_count - processed:,} records failed and will need to be retried)",
250+
),
251+
)
252+
else:
253+
self.stdout.write(
254+
self.style.SUCCESS(
255+
f" ✓ Completed {model_name}: {processed:,} records",
256+
),
257+
)
233258

234259
except Exception as e:
235260
self.stdout.write(
236261
self.style.ERROR(f" ✗ Failed to process {model_name}: {e}"),
237262
)
238263
logger.error(f"Error processing {model_name}: {e}")
239264

240-
if dry_run:
241-
self.stdout.write(
242-
self.style.SUCCESS(
243-
f"\nDRY RUN COMPLETE: Would have processed {total_processed:,} records",
244-
),
245-
)
246-
else:
247-
self.stdout.write(
248-
self.style.SUCCESS(
249-
f"\nBACKFILL COMPLETE: Processed {total_processed:,} records",
250-
),
251-
)
265+
self.stdout.write(
266+
self.style.SUCCESS(
267+
f"\nBACKFILL COMPLETE: Processed {total_processed:,} records",
268+
),
269+
)

dojo/pghistory_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
to expose context fields as structured fields.
66
77
Note: Performance indexes for the pghistory_context table are managed
8-
via Django migration 0245_add_pghistory_context_indexes.py rather than
8+
via Django migration 0244_pghistory_indices.py rather than
99
through model Meta classes, since the context table is managed by
1010
the pghistory library itself.
1111
"""
@@ -28,3 +28,4 @@ class DojoEvents(pghistory.models.Events):
2828

2929
class Meta:
3030
proxy = True
31+
app_label = "dojo"

0 commit comments

Comments
 (0)