-
Notifications
You must be signed in to change notification settings - Fork 1
perf(spp_programs): replace Python uniqueness checks with SQL constraints #116
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
Open
kneckinator
wants to merge
1
commit into
19.0
Choose a base branch
from
ken/optimize_spp_program
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| import logging | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def migrate(cr, version): | ||
| """Deduplicate data before adding SQL UNIQUE constraints. | ||
|
|
||
| Removes duplicate rows using ROW_NUMBER() OVER (PARTITION BY ...), | ||
| keeping the earliest record (lowest id) for each unique combination. | ||
| """ | ||
| if not version: | ||
| return | ||
|
|
||
| _deduplicate_program_memberships(cr) | ||
| _deduplicate_cycle_memberships(cr) | ||
| _deduplicate_entitlement_codes(cr) | ||
|
|
||
|
|
||
| def _deduplicate_program_memberships(cr): | ||
| """Remove duplicate (partner_id, program_id) rows from spp_program_membership.""" | ||
| cr.execute( | ||
| """ | ||
| DELETE FROM spp_program_membership | ||
| WHERE id IN ( | ||
| SELECT id FROM ( | ||
| SELECT id, | ||
| ROW_NUMBER() OVER ( | ||
| PARTITION BY partner_id, program_id | ||
| ORDER BY id | ||
| ) AS rn | ||
| FROM spp_program_membership | ||
| ) sub | ||
| WHERE rn > 1 | ||
| ) | ||
| """ | ||
| ) | ||
| if cr.rowcount: | ||
| _logger.info("Deduplicated %d duplicate program membership rows", cr.rowcount) | ||
|
|
||
|
|
||
| def _deduplicate_cycle_memberships(cr): | ||
| """Remove duplicate (partner_id, cycle_id) rows from spp_cycle_membership.""" | ||
| cr.execute( | ||
| """ | ||
| DELETE FROM spp_cycle_membership | ||
| WHERE id IN ( | ||
| SELECT id FROM ( | ||
| SELECT id, | ||
| ROW_NUMBER() OVER ( | ||
| PARTITION BY partner_id, cycle_id | ||
| ORDER BY id | ||
| ) AS rn | ||
| FROM spp_cycle_membership | ||
| ) sub | ||
| WHERE rn > 1 | ||
| ) | ||
| """ | ||
| ) | ||
| if cr.rowcount: | ||
| _logger.info("Deduplicated %d duplicate cycle membership rows", cr.rowcount) | ||
|
|
||
|
|
||
| def _deduplicate_entitlement_codes(cr): | ||
| """Remove duplicate code values from spp_entitlement. | ||
|
|
||
| For duplicate codes, regenerates codes for the newer records rather | ||
| than deleting them, since entitlements may have financial significance. | ||
| """ | ||
| cr.execute( | ||
| """ | ||
| UPDATE spp_entitlement | ||
| SET code = code || '-' || id::text | ||
| WHERE id IN ( | ||
| SELECT id FROM ( | ||
| SELECT id, | ||
| ROW_NUMBER() OVER ( | ||
| PARTITION BY code | ||
| ORDER BY id | ||
| ) AS rn | ||
| FROM spp_entitlement | ||
| WHERE code IS NOT NULL | ||
| ) sub | ||
| WHERE rn > 1 | ||
| ) | ||
| """ | ||
| ) | ||
| if cr.rowcount: | ||
| _logger.info("Deduplicated %d entitlement rows with duplicate codes", cr.rowcount) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions
_deduplicate_program_membershipsand_deduplicate_cycle_membershipscontain nearly identical SQL logic. To improve maintainability and reduce code duplication, consider refactoring them into a single, generic helper function. This would make the script more concise and provide a reusable pattern for similar deduplication tasks in future migrations.Here is an example of how you could refactor it:
This approach centralizes the deletion logic, making the script easier to read and maintain.