Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/sentry/migrations/1008_add_organization_contributors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Generated by Django 5.2.8 on 2025-11-21 00:00

import django.db.models.deletion
from django.db import migrations, models

import sentry.db.models.fields.bounded
import sentry.db.models.fields.foreignkey
import sentry.db.models.fields.hybrid_cloud_foreign_key
from sentry.new_migrations.migrations import CheckedMigration


class Migration(CheckedMigration):
# This flag is used to mark that a migration shouldn't be automatically run in production.
# This should only be used for operations where it's safe to run the migration after your
# code has deployed. So this should not be used for most operations that alter the schema
# of a table.
# Here are some things that make sense to mark as post deployment:
# - Large data migrations. Typically we want these to be run manually so that they can be
# monitored and not block the deploy for a long period of time while they run.
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
# run this outside deployments so that we don't block them. Note that while adding an index
# is a schema change, it's completely safe to run the operation after the code has deployed.
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment

is_post_deployment = False

dependencies = [
("sentry", "1007_cleanup_failed_safe_deletes"),
]

operations = [
migrations.CreateModel(
name="OrganizationContributors",
fields=[
(
"id",
sentry.db.models.fields.bounded.BoundedBigAutoField(
primary_key=True, serialize=False
),
),
("external_identifier", models.CharField(db_index=True, max_length=255)),
("alias", models.CharField(blank=True, max_length=255, null=True)),
("num_actions", sentry.db.models.fields.bounded.BoundedIntegerField(default=0)),
("date_added", models.DateTimeField(auto_now_add=True)),
("date_updated", models.DateTimeField(auto_now=True)),
(
"organization",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="sentry.organization",
),
),
(
"integration_id",
sentry.db.models.fields.hybrid_cloud_foreign_key.HybridCloudForeignKey(
"sentry.Integration",
on_delete="CASCADE",
),
),
],
options={
"db_table": "sentry_organizationcontributors",
"constraints": [
models.UniqueConstraint(
fields=("organization_id", "integration_id", "external_identifier"),
name="sentry_organizationcontributors_unique_org_contributor",
)
],
"indexes": [
models.Index(fields=("date_updated",)),
],
},
),
]
42 changes: 42 additions & 0 deletions src/sentry/models/organizationcontributors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

from django.db import models

from sentry.backup.scopes import RelocationScope
from sentry.db.models import BoundedIntegerField, FlexibleForeignKey, Model, region_silo_model
from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey


@region_silo_model
class OrganizationContributors(Model):
"""
Tracks external contributors and their activity for an organization.
This model stores information about contributors associated with an
integration for a specific organization, including their external identity
and how many actions they have taken.
"""

__relocation_scope__ = RelocationScope.Excluded

organization = FlexibleForeignKey("sentry.Organization", on_delete=models.CASCADE)

integration_id = HybridCloudForeignKey("sentry.Integration", on_delete="CASCADE")

external_identifier = models.CharField(max_length=255)

Choose a reason for hiding this comment

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

[BestPractice]

The migration defines external_identifier with db_index=True (line 41), but the model definition (line 25 in organizationcontributors.py) doesn't include this parameter. This inconsistency means the model and migration are out of sync.

The external_identifier field is part of a unique constraint, so it will be indexed as part of that constraint. However, if you need a separate index for queries that filter only by external_identifier, you should add db_index=True to the model:

external_identifier = models.CharField(max_length=255, db_index=True)

Or if the unique constraint index is sufficient, remove db_index=True from the migration.

Context for Agents
The migration defines `external_identifier` with `db_index=True` (line 41), but the model definition (line 25 in `organizationcontributors.py`) doesn't include this parameter. This inconsistency means the model and migration are out of sync.

The `external_identifier` field is part of a unique constraint, so it will be indexed as part of that constraint. However, if you need a separate index for queries that filter only by `external_identifier`, you should add `db_index=True` to the model:

```python
external_identifier = models.CharField(max_length=255, db_index=True)
```

Or if the unique constraint index is sufficient, remove `db_index=True` from the migration.

File: src/sentry/models/organizationcontributors.py
Line: 25

alias = models.CharField(max_length=255, null=True, blank=True)
num_actions = BoundedIntegerField(default=0)
date_added = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)

class Meta:
app_label = "sentry"
db_table = "sentry_organizationcontributors"
constraints = [
models.UniqueConstraint(
fields=["organization_id", "integration_id", "external_identifier"],
name="sentry_organizationcontributors_unique_org_contributor",
),
]
indexes = [
models.Index(fields=["date_updated"]),
Comment on lines +40 to +41

Choose a reason for hiding this comment

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

[PerformanceOptimization]

Missing index on num_actions field could cause performance issues when resetting action counts at billing period boundaries. Based on the PR description, num_actions gets cleared at the start of every billing period, which likely requires querying contributors by this field.

Consider adding an index if you'll be running queries like:

  • Finding all contributors with actions to reset
  • Filtering contributors by action count thresholds

If such queries are needed, add to the model's Meta:

indexes = [
    models.Index(fields=["date_updated"]),
    models.Index(fields=["num_actions"]),
]
Context for Agents
Missing index on `num_actions` field could cause performance issues when resetting action counts at billing period boundaries. Based on the PR description, `num_actions` gets cleared at the start of every billing period, which likely requires querying contributors by this field.

Consider adding an index if you'll be running queries like:
- Finding all contributors with actions to reset
- Filtering contributors by action count thresholds

If such queries are needed, add to the model's Meta:
```python
indexes = [
    models.Index(fields=["date_updated"]),
    models.Index(fields=["num_actions"]),
]
```

File: src/sentry/models/organizationcontributors.py
Line: 41

]