-
Notifications
You must be signed in to change notification settings - Fork 4
[PB-5670] fix/add migration to clean up duplicate backup folders with batching #852
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
jzunigax2
wants to merge
3
commits into
master
Choose a base branch
from
fix/cleanup-duplicate-folders
base: master
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.
+143
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ea983ee
fix: add migration to clean up duplicate backup folders with batching
jzunigax2 189fb5f
fix: update migration to refine duplicate backup folder cleanup logic…
jzunigax2 84025d3
fix: refine cleanup logic in migration to exclude removed folders fro…
jzunigax2 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
115 changes: 115 additions & 0 deletions
115
migrations/20260122030036-cleanup-duplicate-backup-folders.js
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,115 @@ | ||
| 'use strict'; | ||
|
|
||
| const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
| const MAX_ATTEMPTS = 10; | ||
| const BATCH_SIZE = 100; | ||
| const SLEEP_TIME_MS = 5000; | ||
|
|
||
| /** @type {import('sequelize-cli').Migration} */ | ||
| module.exports = { | ||
| async up(queryInterface) { | ||
| let totalDeleted = 0; | ||
| let batchCount = 0; | ||
| let attempts = 0; | ||
|
|
||
| console.info(`Batch size: ${BATCH_SIZE} duplicate groups per batch`); | ||
|
|
||
| console.info('Starting cleanup of duplicate backup folders...'); | ||
|
|
||
| const deleteQuery = ` | ||
| WITH duplicate_groups AS ( | ||
| SELECT | ||
| plain_name, | ||
| bucket, | ||
| user_id, | ||
| MIN(id) as id_to_keep | ||
| FROM folders | ||
| WHERE | ||
| created_at >= '2025-12-17 14:16:00' | ||
| AND created_at <= '2026-01-05 21:50:00' | ||
| AND parent_id IS NULL | ||
| AND parent_uuid IS NULL | ||
| AND deleted = false | ||
| AND removed = false | ||
| AND plain_name IS NOT NULL | ||
| GROUP BY plain_name, bucket, user_id | ||
| HAVING COUNT(*) > 1 | ||
| LIMIT ${BATCH_SIZE} | ||
| ), | ||
| folders_to_delete AS ( | ||
| SELECT f.id | ||
| FROM folders f | ||
| INNER JOIN duplicate_groups dg | ||
| ON f.plain_name = dg.plain_name | ||
| AND f.bucket = dg.bucket | ||
| AND f.user_id = dg.user_id | ||
| WHERE | ||
| f.id != dg.id_to_keep | ||
| AND NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM files | ||
| WHERE folder_id = f.id | ||
| AND status != 'DELETED' | ||
| ) | ||
| AND NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM folders child | ||
| WHERE child.parent_uuid = f.uuid | ||
| AND child.deleted = false | ||
| ) | ||
sg-gs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
Comment on lines
39
to
60
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this joins all folders back to their duplicate group by matching (plain_name, bucket, user_id). So if a group has 3 duplicates, the JOIN produces 3 rows — each with that group's id_to_keep. Then we filter to only keep folders where:
this should get 5863 folders |
||
| UPDATE folders | ||
| SET | ||
| deleted = true, | ||
| deleted_at = NOW(), | ||
| removed = true, | ||
| removed_at = NOW() | ||
| FROM folders_to_delete | ||
| WHERE folders.id = folders_to_delete.id | ||
| AND folders.deleted = false | ||
sg-gs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| AND folders.removed = false | ||
| RETURNING folders.id; | ||
| `; | ||
|
|
||
| let hasMore = true; | ||
|
|
||
| while (hasMore) { | ||
| try { | ||
| const [results] = await queryInterface.sequelize.query(deleteQuery); | ||
| const deletedInBatch = results.length; | ||
| batchCount++; | ||
| totalDeleted += deletedInBatch; | ||
| attempts = 0; | ||
|
|
||
| console.info( | ||
| `Batch ${batchCount}: Deleted ${deletedInBatch} folders (Total: ${totalDeleted})`, | ||
| ); | ||
|
|
||
| hasMore = deletedInBatch > 0; | ||
|
|
||
| if (hasMore) { | ||
| await sleep(SLEEP_TIME_MS); | ||
| } | ||
| } catch (err) { | ||
| attempts++; | ||
| console.error( | ||
| `[ERROR]: Error in batch ${batchCount} (attempt ${attempts}/${MAX_ATTEMPTS}): ${err.message}`, | ||
| ); | ||
|
|
||
| if (attempts >= MAX_ATTEMPTS) { | ||
| console.error( | ||
| '[ERROR]: Maximum retry attempts reached, exiting migration.', | ||
| ); | ||
| break; | ||
| } | ||
|
|
||
| await sleep(SLEEP_TIME_MS); | ||
| } | ||
| } | ||
|
|
||
| console.info('\n=== Cleanup Complete ==='); | ||
| console.info(`Total batches processed: ${batchCount}`); | ||
| console.info(`Total folders deleted: ${totalDeleted}`); | ||
| }, | ||
| async down() {}, | ||
| }; | ||
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.
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.
This gets the oldest folder from each duplicated group. So we should be getting 905 folders from this cte