Update modules/database/database_init.py
Browse files
modules/database/database_init.py
CHANGED
@@ -36,116 +36,20 @@ 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 |
-
# 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": application_user_feedback 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 |
-
"user_feedback": user_feedback_container
|
61 |
}
|
62 |
|
63 |
-
|
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 |
###################################################################
|
75 |
-
'''
|
76 |
-
def get_container(container_name):
|
77 |
-
"""Obtiene un contenedor espec铆fico"""
|
78 |
-
if not initialize_cosmos_sql_connection():
|
79 |
-
logger.error("No se pudo inicializar la conexi贸n")
|
80 |
-
return None
|
81 |
-
|
82 |
-
# Loggear los contenedores disponibles para debug
|
83 |
-
available_containers = {
|
84 |
-
"users": user_container is not None,
|
85 |
-
"users_sessions": user_sessions_container is not None,
|
86 |
-
"application_requests": application_requests_container is not None,
|
87 |
-
"user_feedback": user_feedback_container is not None
|
88 |
-
}
|
89 |
-
logger.debug(f"Contenedores disponibles: {available_containers}")
|
90 |
-
|
91 |
-
containers = {
|
92 |
-
"users": user_container,
|
93 |
-
"users_sessions": user_sessions_container,
|
94 |
-
"application_requests": application_requests_container,
|
95 |
-
"user_feedback": user_feedback_container
|
96 |
-
}
|
97 |
-
|
98 |
-
container = containers.get(container_name)
|
99 |
-
if not container:
|
100 |
-
logger.error(f"Contenedor no encontrado: {container_name}")
|
101 |
-
logger.error(f"Contenedores disponibles: {list(containers.keys())}")
|
102 |
-
return None
|
103 |
-
|
104 |
-
return container
|
105 |
-
'''
|
106 |
-
|
107 |
-
###################################################################
|
108 |
-
|
109 |
-
def initialize_cosmos_sql_connection():
|
110 |
-
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
111 |
-
global cosmos_client, user_database, user_container, user_sessions_container, application_requests_container, user_feedback_container
|
112 |
-
|
113 |
-
try:
|
114 |
-
# Verificar conexiones existentes
|
115 |
-
if all([
|
116 |
-
cosmos_client,
|
117 |
-
user_database,
|
118 |
-
user_container,
|
119 |
-
user_sessions_container,
|
120 |
-
application_requests_container,
|
121 |
-
user_feedback_container
|
122 |
-
]):
|
123 |
-
logger.debug("Todas las conexiones ya est谩n inicializadas")
|
124 |
-
return True
|
125 |
-
|
126 |
-
# ... c贸digo existente ...
|
127 |
-
|
128 |
-
# Inicializar contenedores
|
129 |
-
user_container = user_database.get_container_client("users")
|
130 |
-
user_sessions_container = user_database.get_container_client("users_sessions")
|
131 |
-
application_requests_container = user_database.get_container_client("application_requests")
|
132 |
-
user_feedback_container = user_database.get_container_client("user_feedback")
|
133 |
-
|
134 |
-
logger.info("Contenedores inicializados:")
|
135 |
-
logger.info(f"- users: {user_container is not None}")
|
136 |
-
logger.info(f"- users_sessions: {user_sessions_container is not None}")
|
137 |
-
logger.info(f"- application_requests: {application_requests_container is not None}")
|
138 |
-
logger.info(f"- user_feedback: {user_feedback_container is not None}")
|
139 |
-
|
140 |
-
return True
|
141 |
-
|
142 |
-
except Exception as e:
|
143 |
-
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
144 |
-
return False
|
145 |
-
|
146 |
-
######################################################
|
147 |
-
|
148 |
-
'''
|
149 |
def initialize_cosmos_sql_connection():
|
150 |
"""Inicializa la conexi贸n a Cosmos DB SQL API"""
|
151 |
global cosmos_client, user_database, user_container, user_sessions_container
|
@@ -163,11 +67,9 @@ def initialize_cosmos_sql_connection():
|
|
163 |
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
164 |
user_database = cosmos_client.get_database_client("user_database")
|
165 |
|
166 |
-
|
167 |
# Inicializar contenedores manteniendo la estructura existente
|
168 |
user_container = user_database.get_container_client("users")
|
169 |
user_sessions_container = user_database.get_container_client("users_sessions")
|
170 |
-
application_requests_container = user_database.get_container_client("application_requests")
|
171 |
|
172 |
logger.info("Conexi贸n a Cosmos DB SQL API exitosa")
|
173 |
return True
|
@@ -175,7 +77,6 @@ def initialize_cosmos_sql_connection():
|
|
175 |
except Exception as e:
|
176 |
logger.error(f"Error al conectar con Cosmos DB SQL API: {str(e)}")
|
177 |
return False
|
178 |
-
'''
|
179 |
|
180 |
###################################################################
|
181 |
def initialize_mongodb_connection():
|
|
|
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
|
|
|
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
|
|
|
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():
|