Skip to content

Commit

Permalink
Simplify unique constraint for documents (#1098)
Browse files Browse the repository at this point in the history
This commit refined the project's document indexing strategy to
accommodate unique key constraints while handling soft deletions.
Previously, this was solved by applying partial index and unique
constraint, but partial index could not be used when sharding. By
adding the `removed_at` field into the uniqueness check, we now ensure
only one active document exists at a time.
  • Loading branch information
hackerwins authored Dec 11, 2024
1 parent c9a86db commit 084cf41
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions server/backend/database/mongo/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ var collectionInfos = []collectionInfo{
Keys: bsonx.Doc{
{Key: "project_id", Value: bsonx.Int32(1)}, // shard key
{Key: "key", Value: bsonx.Int32(1)},
{Key: "removed_at", Value: bsonx.Int32(1)},
},
Options: options.Index().SetPartialFilterExpression(
bsonx.Doc{
{Key: "removed_at", Value: bsonx.Null()},
},
).SetUnique(true),
Options: options.Index().SetUnique(true),
}},
}, {
name: ColChanges,
Expand Down

0 comments on commit 084cf41

Please sign in to comment.