Update modules/database/database_init.py
Browse files
modules/database/database_init.py
CHANGED
@@ -35,6 +35,43 @@ def verify_container_partition_key(container, expected_path):
|
|
35 |
|
36 |
###################################################################
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def get_container(container_name):
|
39 |
"""Obtiene un contenedor espec铆fico"""
|
40 |
if not initialize_cosmos_sql_connection():
|
@@ -64,8 +101,85 @@ def get_container(container_name):
|
|
64 |
return None
|
65 |
|
66 |
return container
|
|
|
67 |
|
68 |
###################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def initialize_cosmos_sql_connection():
|
70 |
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
71 |
global cosmos_client, user_database, user_container, user_sessions_container
|
@@ -95,6 +209,7 @@ def initialize_cosmos_sql_connection():
|
|
95 |
except Exception as e:
|
96 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
97 |
return False
|
|
|
98 |
|
99 |
###################################################################
|
100 |
def initialize_mongodb_connection():
|
|
|
35 |
|
36 |
###################################################################
|
37 |
|
38 |
+
def get_container(container_name):
|
39 |
+
"""Obtiene un contenedor espec铆fico"""
|
40 |
+
logger.info(f"Solicitando contenedor: {container_name}")
|
41 |
+
|
42 |
+
if not initialize_cosmos_sql_connection():
|
43 |
+
logger.error("No se pudo inicializar la conexi贸n")
|
44 |
+
return None
|
45 |
+
|
46 |
+
# Verificar estado de los contenedores
|
47 |
+
containers_status = {
|
48 |
+
"users": user_container is not None,
|
49 |
+
"users_sessions": user_sessions_container is not None,
|
50 |
+
"application_requests": application_requests_container is not None
|
51 |
+
}
|
52 |
+
|
53 |
+
logger.info(f"Estado actual de los contenedores: {containers_status}")
|
54 |
+
|
55 |
+
# Mapear nombres a contenedores
|
56 |
+
containers = {
|
57 |
+
"users": user_container,
|
58 |
+
"users_sessions": user_sessions_container,
|
59 |
+
"application_requests": application_requests_container
|
60 |
+
}
|
61 |
+
|
62 |
+
container = containers.get(container_name)
|
63 |
+
|
64 |
+
if container is None:
|
65 |
+
logger.error(f"Contenedor '{container_name}' no encontrado o no inicializado")
|
66 |
+
logger.error(f"Contenedores disponibles: {[k for k, v in containers_status.items() if v]}")
|
67 |
+
return None
|
68 |
+
|
69 |
+
logger.info(f"Contenedor '{container_name}' obtenido exitosamente")
|
70 |
+
return container
|
71 |
+
|
72 |
+
|
73 |
+
###################################################################
|
74 |
+
'''
|
75 |
def get_container(container_name):
|
76 |
"""Obtiene un contenedor espec铆fico"""
|
77 |
if not initialize_cosmos_sql_connection():
|
|
|
101 |
return None
|
102 |
|
103 |
return container
|
104 |
+
'''
|
105 |
|
106 |
###################################################################
|
107 |
+
|
108 |
+
def initialize_cosmos_sql_connection():
|
109 |
+
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
110 |
+
global cosmos_client, user_database, user_container, user_sessions_container, application_requests_container
|
111 |
+
|
112 |
+
try:
|
113 |
+
# Verificar conexi贸n existente
|
114 |
+
if all([
|
115 |
+
cosmos_client,
|
116 |
+
user_database,
|
117 |
+
user_container,
|
118 |
+
user_sessions_container,
|
119 |
+
application_requests_container
|
120 |
+
]):
|
121 |
+
logger.debug("Todas las conexiones ya est谩n inicializadas")
|
122 |
+
return True
|
123 |
+
|
124 |
+
# Obtener credenciales
|
125 |
+
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
126 |
+
cosmos_key = os.environ.get("COSMOS_KEY")
|
127 |
+
|
128 |
+
if not cosmos_endpoint or not cosmos_key:
|
129 |
+
raise ValueError("COSMOS_ENDPOINT y COSMOS_KEY deben estar configurados")
|
130 |
+
|
131 |
+
# Inicializar cliente y base de datos
|
132 |
+
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
133 |
+
user_database = cosmos_client.get_database_client("user_database")
|
134 |
+
|
135 |
+
# Inicializar cada contenedor individualmente con verificaci贸n
|
136 |
+
try:
|
137 |
+
user_container = user_database.get_container_client("users")
|
138 |
+
logger.info("Contenedor 'users' inicializado correctamente")
|
139 |
+
except Exception as e:
|
140 |
+
logger.error(f"Error inicializando contenedor 'users': {str(e)}")
|
141 |
+
user_container = None
|
142 |
+
|
143 |
+
try:
|
144 |
+
user_sessions_container = user_database.get_container_client("users_sessions")
|
145 |
+
logger.info("Contenedor 'users_sessions' inicializado correctamente")
|
146 |
+
except Exception as e:
|
147 |
+
logger.error(f"Error inicializando contenedor 'users_sessions': {str(e)}")
|
148 |
+
user_sessions_container = None
|
149 |
+
|
150 |
+
try:
|
151 |
+
application_requests_container = user_database.get_container_client("application_requests")
|
152 |
+
logger.info("Contenedor 'application_requests' inicializado correctamente")
|
153 |
+
except Exception as e:
|
154 |
+
logger.error(f"Error inicializando contenedor 'application_requests': {str(e)}")
|
155 |
+
application_requests_container = None
|
156 |
+
|
157 |
+
# Verificar el estado de los contenedores
|
158 |
+
containers_status = {
|
159 |
+
'users': user_container is not None,
|
160 |
+
'users_sessions': user_sessions_container is not None,
|
161 |
+
'application_requests': application_requests_container is not None
|
162 |
+
}
|
163 |
+
|
164 |
+
logger.info(f"Estado de los contenedores: {containers_status}")
|
165 |
+
|
166 |
+
# Solo retornar True si todos los contenedores se inicializaron correctamente
|
167 |
+
if all(containers_status.values()):
|
168 |
+
logger.info("Todos los contenedores inicializados correctamente")
|
169 |
+
return True
|
170 |
+
else:
|
171 |
+
logger.error("No se pudieron inicializar todos los contenedores")
|
172 |
+
return False
|
173 |
+
|
174 |
+
except Exception as e:
|
175 |
+
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
176 |
+
return False
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
######################################################
|
181 |
+
|
182 |
+
'''
|
183 |
def initialize_cosmos_sql_connection():
|
184 |
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
185 |
global cosmos_client, user_database, user_container, user_sessions_container
|
|
|
209 |
except Exception as e:
|
210 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
211 |
return False
|
212 |
+
'''
|
213 |
|
214 |
###################################################################
|
215 |
def initialize_mongodb_connection():
|