-
Notifications
You must be signed in to change notification settings - Fork 0
fix: prevent duplicate label creation from Android sync engine #310
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
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
108 changes: 108 additions & 0 deletions
108
apiserver/internal/migrations/008_unique_label_names.go
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,108 @@ | ||
| package migrations | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func init() { | ||
| Register(&UniqueLabelNamesMigration{}) | ||
| } | ||
|
|
||
| type UniqueLabelNamesMigration struct{} | ||
|
|
||
| func (m *UniqueLabelNamesMigration) Version() int { | ||
| return 8 | ||
| } | ||
|
|
||
| func (m *UniqueLabelNamesMigration) Name() string { | ||
| return "unique_label_names" | ||
| } | ||
|
|
||
| func (m *UniqueLabelNamesMigration) Up(ctx context.Context, db *gorm.DB) error { | ||
| dbCtx := db.WithContext(ctx) | ||
|
|
||
| dedup := []string{ | ||
| // Reassign task_labels from duplicate labels to the kept (lowest ID) label, | ||
| // skipping rows that would collide with an existing (task_id, keep_id) pair | ||
| `INSERT OR IGNORE INTO task_labels (task_id, label_id) | ||
| SELECT tl.task_id, keep.id | ||
| FROM task_labels tl | ||
| JOIN labels dup ON tl.label_id = dup.id | ||
| JOIN ( | ||
| SELECT MIN(id) AS id, name, created_by | ||
| FROM labels | ||
| GROUP BY created_by, name | ||
| ) keep ON dup.created_by = keep.created_by AND dup.name = keep.name | ||
| WHERE dup.id != keep.id`, | ||
|
|
||
| // Remove task_labels pointing to duplicate labels | ||
| `DELETE FROM task_labels WHERE label_id IN ( | ||
| SELECT l.id FROM labels l | ||
| JOIN ( | ||
| SELECT MIN(id) AS keep_id, name, created_by | ||
| FROM labels | ||
| GROUP BY created_by, name | ||
| ) k ON l.created_by = k.created_by AND l.name = k.name | ||
| WHERE l.id != k.keep_id | ||
| )`, | ||
|
|
||
| // Delete the duplicate labels themselves | ||
| `DELETE FROM labels WHERE id NOT IN ( | ||
| SELECT MIN(id) FROM labels GROUP BY created_by, name | ||
| )`, | ||
| } | ||
|
|
||
| dialect := db.Dialector.Name() | ||
| if dialect == "mysql" { | ||
| dedup = []string{ | ||
| `INSERT IGNORE INTO task_labels (task_id, label_id) | ||
| SELECT tl.task_id, keep_tbl.keep_id | ||
| FROM task_labels tl | ||
| JOIN labels dup ON tl.label_id = dup.id | ||
| JOIN ( | ||
| SELECT MIN(id) AS keep_id, name, created_by | ||
| FROM labels | ||
| GROUP BY created_by, name | ||
| ) keep_tbl ON dup.created_by = keep_tbl.created_by AND dup.name = keep_tbl.name | ||
| WHERE dup.id != keep_tbl.keep_id`, | ||
|
|
||
| `DELETE tl FROM task_labels tl | ||
| JOIN labels l ON tl.label_id = l.id | ||
| JOIN ( | ||
| SELECT MIN(id) AS keep_id, name, created_by | ||
| FROM labels | ||
| GROUP BY created_by, name | ||
| ) k ON l.created_by = k.created_by AND l.name = k.name | ||
| WHERE l.id != k.keep_id`, | ||
|
|
||
| `DELETE l FROM labels l | ||
| JOIN ( | ||
| SELECT MIN(id) AS keep_id, name, created_by | ||
| FROM labels | ||
| GROUP BY created_by, name | ||
| ) k ON l.created_by = k.created_by AND l.name = k.name | ||
| WHERE l.id != k.keep_id`, | ||
| } | ||
| } | ||
|
|
||
| for _, stmt := range dedup { | ||
| if err := dbCtx.Exec(stmt).Error; err != nil { | ||
| return fmt.Errorf("failed to deduplicate labels: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return dbCtx.Exec("CREATE UNIQUE INDEX idx_labels_created_by_name ON labels(created_by, name)").Error | ||
| } | ||
|
|
||
| func (m *UniqueLabelNamesMigration) Down(ctx context.Context, db *gorm.DB) error { | ||
| dbCtx := db.WithContext(ctx) | ||
|
|
||
| if db.Dialector.Name() == "mysql" { | ||
| return dbCtx.Exec("DROP INDEX idx_labels_created_by_name ON labels").Error | ||
| } | ||
|
|
||
| return dbCtx.Exec("DROP INDEX IF EXISTS idx_labels_created_by_name").Error | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.