Update modules/database/database_init.py
Browse files
modules/database/database_init.py
CHANGED
@@ -36,48 +36,109 @@ def verify_container_partition_key(container, expected_path):
|
|
36 |
###################################################################
|
37 |
def get_container(container_name):
|
38 |
"""Obtiene un contenedor espec铆fico"""
|
39 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
logger.info("
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"""
|
|
|
36 |
###################################################################
|
37 |
def get_container(container_name):
|
38 |
"""Obtiene un contenedor espec铆fico"""
|
39 |
+
logger.info(f"Solicitando contenedor: {container_name}")
|
40 |
|
41 |
if not initialize_cosmos_sql_connection():
|
42 |
logger.error("No se pudo inicializar la conexi贸n")
|
43 |
return None
|
44 |
|
45 |
+
# Verificar estado de los contenedores
|
46 |
+
containers_status = {
|
47 |
+
"users": user_container is not None,
|
48 |
+
"users_sessions": user_sessions_container is not None,
|
49 |
+
"application_requests": application_requests_container is not None
|
50 |
+
}
|
51 |
+
|
52 |
+
logger.info(f"Estado actual de los contenedores: {containers_status}")
|
53 |
+
|
54 |
+
# Mapear nombres a contenedores
|
55 |
containers = {
|
56 |
"users": user_container,
|
57 |
+
"users_sessions": user_sessions_container,
|
58 |
+
"application_requests": application_requests_container
|
59 |
}
|
60 |
|
61 |
+
container = containers.get(container_name)
|
62 |
+
|
63 |
+
if container is None:
|
64 |
+
logger.error(f"Contenedor '{container_name}' no encontrado o no inicializado")
|
65 |
+
logger.error(f"Contenedores disponibles: {[k for k, v in containers_status.items() if v]}")
|
66 |
+
return None
|
67 |
+
|
68 |
+
logger.info(f"Contenedor '{container_name}' obtenido exitosamente")
|
69 |
+
return container
|
70 |
###################################################################
|
71 |
+
|
72 |
def initialize_cosmos_sql_connection():
|
73 |
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
74 |
+
global cosmos_client, user_database, user_container, user_sessions_container, application_requests_container
|
75 |
|
76 |
try:
|
77 |
+
# Verificar conexi贸n existente
|
78 |
+
if all([
|
79 |
+
cosmos_client,
|
80 |
+
user_database,
|
81 |
+
user_container,
|
82 |
+
user_sessions_container,
|
83 |
+
application_requests_container
|
84 |
+
]):
|
85 |
+
logger.debug("Todas las conexiones ya est谩n inicializadas")
|
86 |
return True
|
87 |
|
88 |
+
# Obtener credenciales
|
89 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
90 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
91 |
|
92 |
if not cosmos_endpoint or not cosmos_key:
|
93 |
raise ValueError("COSMOS_ENDPOINT y COSMOS_KEY deben estar configurados")
|
94 |
|
95 |
+
# Inicializar cliente y base de datos
|
96 |
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
97 |
user_database = cosmos_client.get_database_client("user_database")
|
98 |
|
99 |
+
# Inicializar cada contenedor individualmente con verificaci贸n
|
100 |
+
try:
|
101 |
+
user_container = user_database.get_container_client("users")
|
102 |
+
logger.info("Contenedor 'users' inicializado correctamente")
|
103 |
+
except Exception as e:
|
104 |
+
logger.error(f"Error inicializando contenedor 'users': {str(e)}")
|
105 |
+
user_container = None
|
106 |
+
|
107 |
+
try:
|
108 |
+
user_sessions_container = user_database.get_container_client("users_sessions")
|
109 |
+
logger.info("Contenedor 'users_sessions' inicializado correctamente")
|
110 |
+
except Exception as e:
|
111 |
+
logger.error(f"Error inicializando contenedor 'users_sessions': {str(e)}")
|
112 |
+
user_sessions_container = None
|
113 |
+
|
114 |
+
try:
|
115 |
+
application_requests_container = user_database.get_container_client("application_requests")
|
116 |
+
logger.info("Contenedor 'application_requests' inicializado correctamente")
|
117 |
+
except Exception as e:
|
118 |
+
logger.error(f"Error inicializando contenedor 'application_requests': {str(e)}")
|
119 |
+
application_requests_container = None
|
120 |
+
|
121 |
+
# Verificar el estado de los contenedores
|
122 |
+
containers_status = {
|
123 |
+
'users': user_container is not None,
|
124 |
+
'users_sessions': user_sessions_container is not None,
|
125 |
+
'application_requests': application_requests_container is not None
|
126 |
+
}
|
127 |
|
128 |
+
logger.info(f"Estado de los contenedores: {containers_status}")
|
129 |
+
|
130 |
+
# Solo retornar True si todos los contenedores se inicializaron correctamente
|
131 |
+
if all(containers_status.values()):
|
132 |
+
logger.info("Todos los contenedores inicializados correctamente")
|
133 |
+
return True
|
134 |
+
else:
|
135 |
+
logger.error("No se pudieron inicializar todos los contenedores")
|
136 |
+
return False
|
137 |
|
138 |
except Exception as e:
|
139 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
140 |
return False
|
141 |
+
|
142 |
###################################################################
|
143 |
def initialize_mongodb_connection():
|
144 |
"""Inicializa la conexi贸n a MongoDB"""
|