Skip to content

fix(#891): Replace TimeStampedModel inheritance with explicit fields and indexes#951

Draft
vjpixel wants to merge 1 commit into
developfrom
fix/891-remove-timestampedmodel-add-indexes
Draft

fix(#891): Replace TimeStampedModel inheritance with explicit fields and indexes#951
vjpixel wants to merge 1 commit into
developfrom
fix/891-remove-timestampedmodel-add-indexes

Conversation

@vjpixel
Copy link
Copy Markdown
Member

@vjpixel vjpixel commented May 5, 2026

Summary

Alternative implementation of #891 addressing @pablodiegoss's review on #921.

Instead of layering Meta.indexes on top of TimeStampedModel inheritance (the #921 approach), this PR removes the inheritance entirely and declares created / modified manually on each affected model. This frees the project from a forever-dependency on TimeStampedModel's shape while still adding the indexes the issue asks for.

Changes

  • Removes TimeStampedModel from 8 models: PostImage, Clipping, Post (blog), Sound, Marker, Object, Artwork, Exhibit (core).
  • Adds created = DateTimeField(auto_now_add=True) and modified = DateTimeField(auto_now=True) to each.
  • Adds Meta.get_latest_by = "modified" (preserves migration history).
  • Adds descending Meta.indexes on -created and -modified for all 8 models.
  • Adds composite (exhibit_type, -created) index on Exhibit covering users/views.py:109,112 and similar queries in core/views/views.py.
  • Keeps django-extensions installed (pyproject.toml, INSTALLED_APPS) so historical migrations (blog/0008, core/0013, 0016, 0027) still replay on fresh local environments.

Migrations

  • src/blog/migrations/0010_* — 7 AlterField + 6 AddIndex.
  • src/core/migrations/0028_* — 20 AlterField (10 main models + 10 pghistory event mirrors) + 11 AddIndex (10 single-column + 1 composite).

No AlterModelOptions, no RemoveField/AddField. The display_date AlterField in the blog migration is a benign side-effect — pre-existing model/migration drift (model declares db_index=True, but migration 0009 had dropped it) that makemigrations legitimately catches up.

Why this over #921

Just creating the indexes as done in #921 will keep us dependent on TimeStampedModel forever, with the band-aid of manually adding indexes to the only fields the TimeStampedModel adds. We should manually create the fields and remove the inheritance.
@pablodiegoss, #891 comment

Test plan

  • make migrations generates expected operations only
  • makemigrations --dry-run --check → "No changes detected" (idempotent)
  • Full pytest suite passes (233 tests on SQLite)
  • TODO before merge: sqlmigrate core 0028 against Postgres confirms AlterField is metadata-only (no table rewrite). Both CreationDateTimeField and DateTimeField deconstruct to timestamp with time zone, so this should be a no-op.
  • TODO before merge: EXPLAIN ANALYZE SELECT * FROM core_exhibit WHERE exhibit_type='AR' ORDER BY created DESC LIMIT 10; against staging confirms Index Scan using exhibit_type_created_idx.
  • TODO before merge: Admin smoke test — /admin/blog/post/, /admin/core/exhibit/, /admin/core/sound/ render with created/modified columns and -created ordering.
  • Run the migration in a low-traffic window (11 new indexes; brief AddIndex locks).

Related

🤖 Generated with Claude Code

…and indexes

Remove TimeStampedModel inheritance from PostImage, Clipping, Post, Sound,
Marker, Object, Artwork, and Exhibit. Declare created/modified manually with
descending Meta.indexes on each model, plus a composite (exhibit_type, -created)
index on Exhibit covering the filter+order pattern in users/views.py:109,112.

Keeps django-extensions installed so historical migrations (blog/0008,
core/0013, 0016, 0027) still replay on fresh local environments.

Addresses @pablodiegoss's review on PR #921: indexing on top of the inheritance
was a band-aid that locked the project into TimeStampedModel forever.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: Missing Database Indexes

1 participant