Skip to content

Commit 39f327b

Browse files
authored
Merge pull request #56 from cuappdev/master
Fix index duplicate for game collection
2 parents b0cf9d7 + 33316be commit 39f327b

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

src/database.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def keep_connection_alive():
5353

5454
def setup_database_indexes():
5555
"""Set up MongoDB indexes for optimal query performance"""
56+
from pymongo.errors import DuplicateKeyError, OperationFailure
57+
5658
try:
5759
game_collection = db["game"]
5860

@@ -66,20 +68,24 @@ def setup_database_indexes():
6668

6769
# Index for sorting operations
6870
game_collection.create_index([("date", -1)], background=True)
69-
70-
# Index to have unique games so we won't add duplicates
71-
game_collection.create_index(
72-
[
73-
("sport", 1),
74-
("gender", 1),
75-
("date", 1),
76-
("opponent_id", 1),
77-
("state", 1),
78-
],
79-
unique=True,
80-
background=True
81-
)
82-
71+
72+
try:
73+
game_collection.create_index(
74+
[
75+
("sport", 1),
76+
("gender", 1),
77+
("date", 1),
78+
("opponent_id", 1),
79+
("city", 1),
80+
("state", 1),
81+
("location", 1),
82+
],
83+
unique=True,
84+
background=True
85+
)
86+
except (DuplicateKeyError, OperationFailure) as e:
87+
print(f"Warning: Could not create unique index due to existing duplicates: {e}")
88+
8389
# Additional index for tournament games (without opponent_id)
8490
game_collection.create_index(
8591
[

0 commit comments

Comments
 (0)