Skip to content

OSIDB-4920: Fix JiraProjectFields unique constraint dupliate names#1248

Draft
JakubFrejlach wants to merge 1 commit intomasterfrom
OSIDB-4920-fix-jira-metadata-collector
Draft

OSIDB-4920: Fix JiraProjectFields unique constraint dupliate names#1248
JakubFrejlach wants to merge 1 commit intomasterfrom
OSIDB-4920-fix-jira-metadata-collector

Conversation

@JakubFrejlach
Copy link
Copy Markdown
Contributor

This PR fixes issue with multiple fields in the same project having the same name which violates unique constraint.

@JakubFrejlach JakubFrejlach requested a review from a team April 16, 2026 13:38
@JakubFrejlach JakubFrejlach self-assigned this Apr 16, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 16, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where projects could not have duplicate Jira field names due to an incorrect uniqueness constraint. The system now validates field uniqueness by using field identifiers instead of field names, which enables projects to successfully maintain multiple fields with identical names without triggering validation conflicts, errors, or data issues.

Walkthrough

The pull request introduces a Django migration that alters the JiraProjectFields model's unique_together constraint from (project_key, field_name) to (project_key, field_id). The model definition is updated accordingly, and a changelog entry documents the fix.

Changes

Cohort / File(s) Summary
Database Schema & Model
apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py, apps/trackers/models.py
Updated JiraProjectFields unique constraint from (project_key, field_name) to (project_key, field_id) through migration and model meta configuration, enabling projects with duplicate field names.
Documentation
docs/CHANGELOG.md
Added changelog entry documenting the constraint change to allow duplicate Jira project field names.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing the JiraProjectFields unique constraint to use field_id instead of field_name to handle duplicate field names.
Description check ✅ Passed The description is directly related to the changeset, explaining the issue of multiple fields with the same name violating the unique constraint.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch OSIDB-4920-fix-jira-metadata-collector

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py`:
- Around line 13-16: Migration 0003
(apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py)
changes the unique_together on model JiraProjectFields from (project_key,
field_name) to (project_key, field_id) and needs a pre-migration
cleanup/validation step to avoid constraint violations; add a RunPython
operation in that migration (or an accompanying pre-deploy script) that queries
trackers_jiraprojectfields for duplicate (project_key, field_id) pairs, logs or
resolves duplicates (e.g., merge/remove extras or set a canonical field_id), and
only then applies migrations.AlterUniqueTogether to ensure the DB has no rows
that would break the new constraint.

In `@apps/trackers/models.py`:
- Line 15: The change to unique_together = ("project_key", "field_id") makes
lookups by field_name ambiguous; update either the storage constraint or the
query logic: either restore/expand the uniqueness (e.g., include "field_name" or
include issue-type scope) in the model where unique_together is defined, or
modify the lookup paths in apps/trackers/jira/query.py (the functions that call
.first() when resolving field_name) to detect multiple matches and fail fast
(raise a clear error) or disambiguate using issue-type context; ensure the
chosen fix references the unique_together tuple in models.py and the specific
field_name resolution code in apps/trackers/jira/query.py so lookups return a
single, deterministic field_id/allowed_values.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 13063364-53aa-4b8b-b5bb-396ea060a20f

📥 Commits

Reviewing files that changed from the base of the PR and between 3034401 and f41e28c.

📒 Files selected for processing (3)
  • apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py
  • apps/trackers/models.py
  • docs/CHANGELOG.md

Comment on lines +13 to +16
migrations.AlterUniqueTogether(
name='jiraprojectfields',
unique_together={('project_key', 'field_id')},
),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Run on a DB snapshot before applying migration 0003.
# Expected result: zero rows returned.
psql "$DATABASE_URL" -c "
SELECT project_key, field_id, COUNT(*) AS dup_count
FROM trackers_jiraprojectfields
GROUP BY project_key, field_id
HAVING COUNT(*) > 1
ORDER BY dup_count DESC, project_key, field_id;
"

Repository: RedHatProductSecurity/osidb

Length of output: 115


🏁 Script executed:

find . -name "*jiraprojectfields*" -o -name "*migration*" | grep -E "(migration|jiraproject)" | head -20

Repository: RedHatProductSecurity/osidb

Length of output: 522


🏁 Script executed:

cd apps/trackers/migrations && ls -la | grep -E "^-.*\.py$" | head -20

Repository: RedHatProductSecurity/osidb

Length of output: 363


🏁 Script executed:

cat -n apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py

Repository: RedHatProductSecurity/osidb

Length of output: 562


🏁 Script executed:

cat -n apps/trackers/migrations/0001_initial.py

Repository: RedHatProductSecurity/osidb

Length of output: 1289


🏁 Script executed:

cat -n apps/trackers/migrations/0002_jirabugissuetype.py

Repository: RedHatProductSecurity/osidb

Length of output: 760


🏁 Script executed:

find . -name "models.py" -path "*/trackers/*" -exec cat -n {} \;

Repository: RedHatProductSecurity/osidb

Length of output: 1066


Add a pre-migration check or cleanup step for the constraint change.

Migration 0003 changes the unique constraint from (project_key, field_name) to (project_key, field_id) without safeguards. Since the initial schema did not enforce uniqueness on field_id alone, existing duplicate pairs could cause deployment failure. Either add a RunPython step in this migration to validate/clean duplicates, or provide an explicit pre-deployment verification command for operators:

SELECT project_key, field_id, COUNT(*) AS dup_count
FROM trackers_jiraprojectfields
GROUP BY project_key, field_id
HAVING COUNT(*) > 1;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py`
around lines 13 - 16, Migration 0003
(apps/trackers/migrations/0003_alter_jiraprojectfields_unique_together.py)
changes the unique_together on model JiraProjectFields from (project_key,
field_name) to (project_key, field_id) and needs a pre-migration
cleanup/validation step to avoid constraint violations; add a RunPython
operation in that migration (or an accompanying pre-deploy script) that queries
trackers_jiraprojectfields for duplicate (project_key, field_id) pairs, logs or
resolves duplicates (e.g., merge/remove extras or set a canonical field_id), and
only then applies migrations.AlterUniqueTogether to ensure the DB has no rows
that would break the new constraint.

Comment thread apps/trackers/models.py

class Meta:
unique_together = ("project_key", "field_name")
unique_together = ("project_key", "field_id")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Constraint change makes existing field_name lookups ambiguous.

Line 15 allows duplicate (project_key, field_name) rows, but query paths still resolve by field_name and take .first() (see apps/trackers/jira/query.py Lines 762-770, 530-545, and 382-390). That can return the wrong field_id/allowed_values and produce incorrect Jira payload fields.

Please add disambiguation (for example, include issue-type scope in storage/lookups) or fail fast when multiple rows match a field_name.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/trackers/models.py` at line 15, The change to unique_together =
("project_key", "field_id") makes lookups by field_name ambiguous; update either
the storage constraint or the query logic: either restore/expand the uniqueness
(e.g., include "field_name" or include issue-type scope) in the model where
unique_together is defined, or modify the lookup paths in
apps/trackers/jira/query.py (the functions that call .first() when resolving
field_name) to detect multiple matches and fail fast (raise a clear error) or
disambiguate using issue-type context; ensure the chosen fix references the
unique_together tuple in models.py and the specific field_name resolution code
in apps/trackers/jira/query.py so lookups return a single, deterministic
field_id/allowed_values.

@JakubFrejlach JakubFrejlach removed the request for review from a team April 16, 2026 14:15
@JakubFrejlach JakubFrejlach marked this pull request as draft April 16, 2026 14:15
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.

1 participant