Spaces:
Runtime error
Runtime error
Update database/connection.py
Browse files- database/connection.py +13 -23
database/connection.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# filename: database/connection.py
|
| 2 |
|
| 3 |
import logging
|
| 4 |
from motor.motor_asyncio import AsyncIOMotorClient
|
|
@@ -8,51 +8,41 @@ logger = logging.getLogger(__name__)
|
|
| 8 |
|
| 9 |
class Database:
|
| 10 |
"""
|
| 11 |
-
Manages connections to one or more MongoDB
|
| 12 |
"""
|
| 13 |
-
def __init__(self, uris: list[str]):
|
| 14 |
if not uris:
|
| 15 |
raise ValueError("At least one MongoDB URI is required.")
|
|
|
|
|
|
|
| 16 |
|
| 17 |
self.clients = [AsyncIOMotorClient(uri) for uri in uris]
|
| 18 |
-
self.databases = [client.get_default_database() for client in self.clients]
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
self._user_db_round_robin_counter = 0
|
| 25 |
|
| 26 |
-
logger.info(f"Successfully connected to {len(self.clients)} MongoDB
|
| 27 |
|
| 28 |
async def get_user_db(self, user_id: int):
|
| 29 |
-
"""
|
| 30 |
-
Gets the database assigned to a specific user.
|
| 31 |
-
This ensures a user's data always stays in the same database.
|
| 32 |
-
We use a simple hashing method to distribute users.
|
| 33 |
-
"""
|
| 34 |
db_index = user_id % len(self.databases)
|
| 35 |
return self.databases[db_index]
|
| 36 |
|
| 37 |
async def find_user_db(self, user_id: int):
|
| 38 |
-
"""
|
| 39 |
-
Searches across all databases to find which one contains the user.
|
| 40 |
-
Returns the database object if found, otherwise None.
|
| 41 |
-
"""
|
| 42 |
for db in self.databases:
|
| 43 |
if await db.users.find_one({"user_id": user_id}):
|
| 44 |
return db
|
| 45 |
return None
|
| 46 |
|
| 47 |
def get_next_db_for_new_user(self):
|
| 48 |
-
"""
|
| 49 |
-
Picks a database for a new user using a round-robin strategy.
|
| 50 |
-
This helps distribute new users evenly.
|
| 51 |
-
"""
|
| 52 |
db = self.databases[self._user_db_round_robin_counter]
|
| 53 |
self._user_db_round_robin_counter = (self._user_db_round_robin_counter + 1) % len(self.databases)
|
| 54 |
return db
|
| 55 |
|
| 56 |
# --- Initialize the Database Connection ---
|
| 57 |
-
#
|
| 58 |
-
db = Database(config.MONGODB_URIS)
|
|
|
|
| 1 |
+
# filename: database/connection.py (Corrected Version)
|
| 2 |
|
| 3 |
import logging
|
| 4 |
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
|
| 8 |
|
| 9 |
class Database:
|
| 10 |
"""
|
| 11 |
+
Manages connections to one or more MongoDB clusters.
|
| 12 |
"""
|
| 13 |
+
def __init__(self, uris: list[str], db_name: str):
|
| 14 |
if not uris:
|
| 15 |
raise ValueError("At least one MongoDB URI is required.")
|
| 16 |
+
if not db_name:
|
| 17 |
+
raise ValueError("A database name is required.")
|
| 18 |
|
| 19 |
self.clients = [AsyncIOMotorClient(uri) for uri in uris]
|
|
|
|
| 20 |
|
| 21 |
+
# --- THE FIX IS HERE ---
|
| 22 |
+
# Instead of getting the "default" database, we explicitly get the one
|
| 23 |
+
# you defined in the config.
|
| 24 |
+
self.databases = [client[db_name] for client in self.clients]
|
| 25 |
|
| 26 |
+
self.primary_db = self.databases[0]
|
| 27 |
self._user_db_round_robin_counter = 0
|
| 28 |
|
| 29 |
+
logger.info(f"Successfully connected to {len(self.clients)} MongoDB cluster(s). Using database: '{db_name}'")
|
| 30 |
|
| 31 |
async def get_user_db(self, user_id: int):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
db_index = user_id % len(self.databases)
|
| 33 |
return self.databases[db_index]
|
| 34 |
|
| 35 |
async def find_user_db(self, user_id: int):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
for db in self.databases:
|
| 37 |
if await db.users.find_one({"user_id": user_id}):
|
| 38 |
return db
|
| 39 |
return None
|
| 40 |
|
| 41 |
def get_next_db_for_new_user(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
db = self.databases[self._user_db_round_robin_counter]
|
| 43 |
self._user_db_round_robin_counter = (self._user_db_round_robin_counter + 1) % len(self.databases)
|
| 44 |
return db
|
| 45 |
|
| 46 |
# --- Initialize the Database Connection ---
|
| 47 |
+
# ** We now pass the DATABASE_NAME from the config **
|
| 48 |
+
db = Database(config.MONGODB_URIS, db_name=config.DATABASE_NAME)
|