Skip to content

[backend] Fix simulation to scenario grants #3055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025

Conversation

RomuDeuxfois
Copy link
Member

error → "could not execute statement [ERROR: duplicate key value violates unique constraint "grant"\n Detail: Key (grant_group, grant_exercise, grant_name)=(d5d8c183-e6d9-4d47-b041-25e5f3bdbc62, 375280f3-7207-4f6b-95f8-2b1aaa480697, PLANNER) already exists.] [insert into grants (grant_exercise,grant_group,grant_name,grant_scenario,grant_id) values (?,?,?,?,?)]"

Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 41.22%. Comparing base (62b0a78) to head (702642d).
Report is 26 commits behind head on release/current.

Additional details and impacted files
@@                  Coverage Diff                  @@
##             release/current    #3055      +/-   ##
=====================================================
+ Coverage              41.20%   41.22%   +0.01%     
- Complexity              2286     2288       +2     
=====================================================
  Files                    682      683       +1     
  Lines                  20938    20944       +6     
  Branches                1424     1424              
=====================================================
+ Hits                    8628     8634       +6     
  Misses                 11824    11824              
  Partials                 486      486              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

.getGrants()
.forEach(
scenarioGrant -> {
String key = scenarioGrant.getName() + "|" + scenarioGrant.getGroup();
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 concerned we are reimplementing the table constraint in the service layer: I understand this is to avoid triggering a constraint violation while inserting new grants.

I'd be more concerned with findijng where in the app we are adding a duplicate grant that ultimately conflicts; is this about default grants?

Somehow this should perhaps be handled in the repository if this is not possible to reconcile in code: https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT

This is a Postgres-specific clause (i.e. not standard SQL) which can ignore an insert upon a constraint violation (by name), which would fit the bill here.
Since we reassign the saved grants after the inserts we would have deduplicated objects eventually in the java collection.

Copy link
Member Author

Choose a reason for hiding this comment

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

I totally agree with you. I looked through the code and I don't understand how we got to this situation.
I had a recurring scenario, and the problem occurred at the end of the X recurrence.

However, I wasn't aware of SQL ON CONFLICT, which is much more appropriate. I'll look into it, thanks for the resource!

Copy link
Member Author

Choose a reason for hiding this comment

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

@antoinemzs I found the root cause. I made some changes.

@RomuDeuxfois RomuDeuxfois force-pushed the bugfix/simulation-to-scenario branch from 90e717d to 702642d Compare April 28, 2025 12:17
Copy link
Contributor

@antoinemzs antoinemzs left a comment

Choose a reason for hiding this comment

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

Cheers for finding a hole in our constraints !

@RomuDeuxfois RomuDeuxfois merged commit 9558572 into release/current Apr 30, 2025
8 checks passed
@RomuDeuxfois RomuDeuxfois deleted the bugfix/simulation-to-scenario branch April 30, 2025 09:29
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.

2 participants