Skip to content

Conversation

jurajmajerik
Copy link
Contributor

Problem

BehavioralCohortAnalysis was added as a proxy model without a migration. Whenever we run makemigrations, Django will attempt to add it alongside new model changes.

Changes

Add a migration file. A table won't be created since managed = false.

@jurajmajerik jurajmajerik enabled auto-merge (squash) October 16, 2025 15:08
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

Migration SQL Changes

Hey 👋, we've detected some migrations on this PR. Here's the SQL output for each migration, make sure they make sense:

posthog/migrations/0886_behavioralcohortanalysis.py

BEGIN;
--
-- Create model BehavioralCohortAnalysis
--
-- (no-op)
COMMIT;

Copy link
Contributor

🔍 Migration Risk Analysis

We've analyzed your migrations for potential risks.

Summary: 1 Safe | 0 Needs Review | 0 Blocked

✅ Safe

No locks, backwards compatible

posthog.0886_behavioralcohortanalysis
  └─ #1 ✅ CreateModel
     Creating new table is safe
     model: BehavioralCohortAnalysis
  │
  └──> ℹ️  INFO:
       ℹ️  Skipped operations on newly created tables (empty tables
       don't cause lock contention).

Original approach created a fake model with managed=False just to get an admin interface. This caused Django to track it in migrations, requiring a migration file that did nothing but add noise.

Better approach: Custom admin view without any model. Just a form that triggers management commands. Registered directly in ee/urls.py.

Changes:
- Remove BehavioralCohortAnalysis model from behavioral_cohorts_admin.py
- Remove 0886_behavioralcohortanalysis.py migration
- Create analyze_behavioral_cohorts_view as standalone view function
- Register URL in ee/urls.py under admin path
- Update max_migration.txt to 0885
Without a registered model, the behavioral cohort analysis view doesn't appear in the Django admin sidebar automatically. Add custom link to admin index page so users can find it.

Changes:
- Add custom app.html template for behavioral cohort analysis section
- Include in admin index.html
- Add named URL pattern for reverse URL lookup
)


class BehavioralCohortAnalysis(models.Model):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm removing the model and add a custom view. This should be better and get rid of the migration problem.

Prevent duplicate submissions on page refresh by redirecting after successful form submission.
Copy link
Contributor

@webjunkie webjunkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the fake model approach with a proper custom admin view. The original implementation used a managed=False model just to get an admin interface, which forced Django to track it in migrations despite not creating any tables.

New approach: Direct URL registration in ee/urls.py with a standalone view function. No model = no migration needed = no CI failures. Added a custom link on the admin index page for discoverability.

@jurajmajerik
Copy link
Contributor Author

Amazing, thanks @webjunkie !!

@jurajmajerik jurajmajerik merged commit 1bc7c6b into master Oct 16, 2025
162 checks passed
@jurajmajerik jurajmajerik deleted the behavioral-cohorts-migration branch October 16, 2025 19:28
@meikelmosby
Copy link
Contributor

man thanks a lot!! thanks for the explanation! good to know for next time

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.

3 participants