Spaces:
Sleeping
Sleeping
Update modules/database/database_init.py
Browse files
modules/database/database_init.py
CHANGED
|
@@ -21,6 +21,7 @@ user_sessions_container = None
|
|
| 21 |
mongo_client = None
|
| 22 |
mongo_db = None
|
| 23 |
|
|
|
|
| 24 |
def verify_container_partition_key(container, expected_path):
|
| 25 |
"""Verifica la configuraci贸n de partition key de un contenedor"""
|
| 26 |
try:
|
|
@@ -32,88 +33,62 @@ def verify_container_partition_key(container, expected_path):
|
|
| 32 |
logger.error(f"Error verificando partition key en {container.id}: {str(e)}")
|
| 33 |
return False
|
| 34 |
|
|
|
|
| 35 |
def get_container(container_name):
|
| 36 |
"""Obtiene un contenedor espec铆fico"""
|
| 37 |
-
global
|
| 38 |
|
| 39 |
-
container_config = {
|
| 40 |
-
"users": "/id",
|
| 41 |
-
"application_requests": "/email",
|
| 42 |
-
"user_feedback": "/username",
|
| 43 |
-
"users_sessions": "/username"
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
if container_name not in container_config:
|
| 47 |
-
logger.error(f"Contenedor no v谩lido: {container_name}")
|
| 48 |
-
return None
|
| 49 |
-
|
| 50 |
-
# Si el contenedor ya est谩 en cach茅, devolverlo
|
| 51 |
-
if container_name in containers and containers[container_name] is not None:
|
| 52 |
-
return containers[container_name]
|
| 53 |
-
|
| 54 |
-
# Si no est谩 en cach茅, intentar inicializar la conexi贸n
|
| 55 |
if not initialize_cosmos_sql_connection():
|
| 56 |
logger.error("No se pudo inicializar la conexi贸n")
|
| 57 |
return None
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return containers.get(container_name)
|
| 60 |
|
|
|
|
| 61 |
def initialize_cosmos_sql_connection():
|
| 62 |
-
"""Inicializa
|
| 63 |
-
global cosmos_client, user_database,
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
if cosmos_client and user_database and containers:
|
| 68 |
return True
|
| 69 |
|
| 70 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
| 71 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
| 72 |
|
| 73 |
if not cosmos_endpoint or not cosmos_key:
|
| 74 |
-
raise ValueError("COSMOS_ENDPOINT
|
| 75 |
|
| 76 |
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
| 77 |
user_database = cosmos_client.get_database_client("user_database")
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"user_feedback": "/username",
|
| 84 |
-
"users_sessions": "/username"
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
# Inicializar contenedores
|
| 88 |
-
for container_id, partition_path in container_config.items():
|
| 89 |
-
try:
|
| 90 |
-
container = user_database.get_container_client(container_id)
|
| 91 |
-
if not verify_container_partition_key(container, partition_path):
|
| 92 |
-
logger.error(f"Verificaci贸n de partition key fall贸 para {container_id}")
|
| 93 |
-
return False
|
| 94 |
-
containers[container_id] = container
|
| 95 |
-
logger.info(f"Contenedor {container_id} inicializado correctamente")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
logger.error(f"Error inicializando contenedor {container_id}: {str(e)}")
|
| 98 |
-
return False
|
| 99 |
-
|
| 100 |
logger.info("Conexi贸n a Cosmos DB SQL API exitosa")
|
| 101 |
return True
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
| 105 |
return False
|
| 106 |
-
|
|
|
|
| 107 |
def initialize_mongodb_connection():
|
| 108 |
-
"""Inicializa la conexi贸n a
|
| 109 |
global mongo_client, mongo_db
|
| 110 |
try:
|
| 111 |
-
|
| 112 |
-
if not
|
| 113 |
-
raise ValueError("MONGODB_CONNECTION_STRING
|
| 114 |
|
| 115 |
mongo_client = MongoClient(
|
| 116 |
-
|
| 117 |
tls=True,
|
| 118 |
tlsCAFile=certifi.where(),
|
| 119 |
retryWrites=False,
|
|
@@ -122,19 +97,18 @@ def initialize_mongodb_connection():
|
|
| 122 |
socketTimeoutMS=10000
|
| 123 |
)
|
| 124 |
|
| 125 |
-
mongo_client.admin.command('ping')
|
| 126 |
mongo_db = mongo_client['aideatext_db']
|
| 127 |
-
logger.info("Conexi贸n a Cosmos DB MongoDB API exitosa")
|
| 128 |
return True
|
| 129 |
-
|
| 130 |
except Exception as e:
|
| 131 |
-
logger.error(f"Error
|
| 132 |
return False
|
| 133 |
|
|
|
|
| 134 |
def initialize_database_connections():
|
| 135 |
-
"""Inicializa todas las conexiones
|
| 136 |
return initialize_cosmos_sql_connection() and initialize_mongodb_connection()
|
| 137 |
|
|
|
|
| 138 |
def get_mongodb():
|
| 139 |
"""Obtiene la conexi贸n MongoDB"""
|
| 140 |
if mongo_db is None:
|
|
|
|
| 21 |
mongo_client = None
|
| 22 |
mongo_db = None
|
| 23 |
|
| 24 |
+
###################################################################
|
| 25 |
def verify_container_partition_key(container, expected_path):
|
| 26 |
"""Verifica la configuraci贸n de partition key de un contenedor"""
|
| 27 |
try:
|
|
|
|
| 33 |
logger.error(f"Error verificando partition key en {container.id}: {str(e)}")
|
| 34 |
return False
|
| 35 |
|
| 36 |
+
###################################################################
|
| 37 |
def get_container(container_name):
|
| 38 |
"""Obtiene un contenedor espec铆fico"""
|
| 39 |
+
global user_container, user_sessions_container
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if not initialize_cosmos_sql_connection():
|
| 42 |
logger.error("No se pudo inicializar la conexi贸n")
|
| 43 |
return None
|
| 44 |
+
|
| 45 |
+
containers = {
|
| 46 |
+
"users": user_container,
|
| 47 |
+
"users_sessions": user_sessions_container
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
return containers.get(container_name)
|
| 51 |
|
| 52 |
+
###################################################################
|
| 53 |
def initialize_cosmos_sql_connection():
|
| 54 |
+
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
| 55 |
+
global cosmos_client, user_database, user_container, user_sessions_container
|
| 56 |
|
| 57 |
try:
|
| 58 |
+
if cosmos_client and user_database and user_container and user_sessions_container:
|
|
|
|
| 59 |
return True
|
| 60 |
|
| 61 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
| 62 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
| 63 |
|
| 64 |
if not cosmos_endpoint or not cosmos_key:
|
| 65 |
+
raise ValueError("COSMOS_ENDPOINT y COSMOS_KEY deben estar configurados")
|
| 66 |
|
| 67 |
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
| 68 |
user_database = cosmos_client.get_database_client("user_database")
|
| 69 |
|
| 70 |
+
# Inicializar contenedores manteniendo la estructura existente
|
| 71 |
+
user_container = user_database.get_container_client("users")
|
| 72 |
+
user_sessions_container = user_database.get_container_client("users_sessions")
|
| 73 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
logger.info("Conexi贸n a Cosmos DB SQL API exitosa")
|
| 75 |
return True
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
| 79 |
return False
|
| 80 |
+
|
| 81 |
+
###################################################################
|
| 82 |
def initialize_mongodb_connection():
|
| 83 |
+
"""Inicializa la conexi贸n a MongoDB"""
|
| 84 |
global mongo_client, mongo_db
|
| 85 |
try:
|
| 86 |
+
connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
| 87 |
+
if not connection_string:
|
| 88 |
+
raise ValueError("MONGODB_CONNECTION_STRING debe estar configurado")
|
| 89 |
|
| 90 |
mongo_client = MongoClient(
|
| 91 |
+
connection_string,
|
| 92 |
tls=True,
|
| 93 |
tlsCAFile=certifi.where(),
|
| 94 |
retryWrites=False,
|
|
|
|
| 97 |
socketTimeoutMS=10000
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
mongo_db = mongo_client['aideatext_db']
|
|
|
|
| 101 |
return True
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
+
logger.error(f"Error conectando a MongoDB: {str(e)}")
|
| 104 |
return False
|
| 105 |
|
| 106 |
+
###################################################################
|
| 107 |
def initialize_database_connections():
|
| 108 |
+
"""Inicializa todas las conexiones"""
|
| 109 |
return initialize_cosmos_sql_connection() and initialize_mongodb_connection()
|
| 110 |
|
| 111 |
+
###################################################################
|
| 112 |
def get_mongodb():
|
| 113 |
"""Obtiene la conexi贸n MongoDB"""
|
| 114 |
if mongo_db is None:
|