Update modules/database/database_init.py
Browse files
modules/database/database_init.py
CHANGED
@@ -36,48 +36,120 @@ 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 contenedores
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
|
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 |
+
"user_feedback": user_feedback_container is not None # A帽adido
|
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 |
+
"user_feedback": user_feedback_container # A帽adido
|
61 |
}
|
62 |
|
63 |
+
container = containers.get(container_name)
|
64 |
+
|
65 |
+
if container is None:
|
66 |
+
logger.error(f"Contenedor '{container_name}' no encontrado o no inicializado")
|
67 |
+
logger.error(f"Contenedores disponibles: {[k for k, v in containers_status.items() if v]}")
|
68 |
+
return None
|
69 |
+
|
70 |
+
logger.info(f"Contenedor '{container_name}' obtenido exitosamente")
|
71 |
+
return container
|
72 |
###################################################################
|
73 |
+
|
74 |
def initialize_cosmos_sql_connection():
|
75 |
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
76 |
+
global cosmos_client, user_database, user_container, user_sessions_container, application_requests_container, user_feedback_container # A帽adida aqu铆 user_feedback_container
|
77 |
|
78 |
try:
|
79 |
+
# Verificar conexi贸n existente
|
80 |
+
if all([
|
81 |
+
cosmos_client,
|
82 |
+
user_database,
|
83 |
+
user_container,
|
84 |
+
user_sessions_container,
|
85 |
+
application_requests_container,
|
86 |
+
user_feedback_container
|
87 |
+
]):
|
88 |
+
logger.debug("Todas las conexiones ya est谩n inicializadas")
|
89 |
return True
|
90 |
|
91 |
+
# Obtener credenciales
|
92 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
93 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
94 |
|
95 |
if not cosmos_endpoint or not cosmos_key:
|
96 |
raise ValueError("COSMOS_ENDPOINT y COSMOS_KEY deben estar configurados")
|
97 |
|
98 |
+
# Inicializar cliente y base de datos
|
99 |
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
100 |
user_database = cosmos_client.get_database_client("user_database")
|
101 |
|
102 |
+
# Inicializar contenedores
|
103 |
+
try:
|
104 |
+
user_container = user_database.get_container_client("users")
|
105 |
+
logger.info("Contenedor 'users' inicializado correctamente")
|
106 |
+
except Exception as e:
|
107 |
+
logger.error(f"Error inicializando contenedor 'users': {str(e)}")
|
108 |
+
user_container = None
|
109 |
+
|
110 |
+
try:
|
111 |
+
user_sessions_container = user_database.get_container_client("users_sessions")
|
112 |
+
logger.info("Contenedor 'users_sessions' inicializado correctamente")
|
113 |
+
except Exception as e:
|
114 |
+
logger.error(f"Error inicializando contenedor 'users_sessions': {str(e)}")
|
115 |
+
user_sessions_container = None
|
116 |
+
|
117 |
+
try:
|
118 |
+
application_requests_container = user_database.get_container_client("application_requests")
|
119 |
+
logger.info("Contenedor 'application_requests' inicializado correctamente")
|
120 |
+
except Exception as e:
|
121 |
+
logger.error(f"Error inicializando contenedor 'application_requests': {str(e)}")
|
122 |
+
application_requests_container = None
|
123 |
+
|
124 |
+
try:
|
125 |
+
user_feedback_container = user_database.get_container_client("user_feedback")
|
126 |
+
logger.info("Contenedor 'user_feedback' inicializado correctamente")
|
127 |
+
except Exception as e:
|
128 |
+
logger.error(f"Error inicializando contenedor 'user_feedback': {str(e)}")
|
129 |
+
user_feedback_container = None
|
130 |
+
|
131 |
+
# Verificar el estado de los contenedores
|
132 |
+
containers_status = {
|
133 |
+
'users': user_container is not None,
|
134 |
+
'users_sessions': user_sessions_container is not None,
|
135 |
+
'application_requests': application_requests_container is not None,
|
136 |
+
'user_feedback': user_feedback_container is not None
|
137 |
+
}
|
138 |
+
|
139 |
+
logger.info(f"Estado de los contenedores: {containers_status}")
|
140 |
|
141 |
+
if all(containers_status.values()):
|
142 |
+
logger.info("Todos los contenedores inicializados correctamente")
|
143 |
+
return True
|
144 |
+
else:
|
145 |
+
logger.error("No se pudieron inicializar todos los contenedores")
|
146 |
+
return False
|
147 |
|
148 |
except Exception as e:
|
149 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
150 |
return False
|
151 |
+
|
152 |
+
|
153 |
###################################################################
|
154 |
def initialize_mongodb_connection():
|
155 |
"""Inicializa la conexi贸n a MongoDB"""
|