AIdeaText commited on
Commit
220a9a0
verified
1 Parent(s): 28038e5

Update modules/database/database_init.py

Browse files
Files changed (1) hide show
  1. modules/database/database_init.py +37 -26
modules/database/database_init.py CHANGED
@@ -34,31 +34,39 @@ def verify_container_partition_key(container, expected_path):
34
 
35
  def get_container(container_name):
36
  """Obtiene un contenedor espec铆fico"""
37
- containers = {
38
- "users": (user_container, "/id"),
39
- "application_requests": (application_requests_container, "/email"),
40
- "user_feedback": (user_feedback_container, "/username"),
41
- "users_sessions": (user_sessions_container, "/username")
 
 
42
  }
43
 
44
- if container_name not in containers:
45
  logger.error(f"Contenedor no v谩lido: {container_name}")
46
  return None
47
 
48
- container, partition_path = containers[container_name]
49
- if container is None:
50
- initialize_cosmos_sql_connection()
51
- container, _ = containers[container_name]
52
 
53
- return container
 
 
 
 
 
54
 
55
  def initialize_cosmos_sql_connection():
56
  """Inicializa y verifica la conexi贸n a Cosmos DB SQL API"""
57
- global cosmos_client, user_database, user_container, \
58
- application_requests_container, user_feedback_container, \
59
- user_sessions_container
60
 
61
  try:
 
 
 
 
62
  cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
63
  cosmos_key = os.environ.get("COSMOS_KEY")
64
 
@@ -69,22 +77,25 @@ def initialize_cosmos_sql_connection():
69
  user_database = cosmos_client.get_database_client("user_database")
70
 
71
  # Configuraci贸n de contenedores
72
- containers_config = {
73
- "users": ("/id", user_container),
74
- "application_requests": ("/email", application_requests_container),
75
- "user_feedback": ("/username", user_feedback_container),
76
- "users_sessions": ("/username", user_sessions_container)
77
  }
78
 
79
  # Inicializar contenedores
80
- for container_id, (expected_path, container_ref) in containers_config.items():
81
- container = user_database.get_container_client(container_id)
82
- if not verify_container_partition_key(container, expected_path):
83
- logger.error(f"Verificaci贸n de partition key fall贸 para {container_id}")
 
 
 
 
 
 
84
  return False
85
-
86
- # Actualizar referencia global
87
- globals()[f"{container_id}_container"] = container
88
 
89
  logger.info("Conexi贸n a Cosmos DB SQL API exitosa")
90
  return True
 
34
 
35
  def get_container(container_name):
36
  """Obtiene un contenedor espec铆fico"""
37
+ global containers
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 y verifica la conexi贸n a Cosmos DB SQL API"""
63
+ global cosmos_client, user_database, containers
 
 
64
 
65
  try:
66
+ # Si ya est谩 inicializado, retornar True
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
 
 
77
  user_database = cosmos_client.get_database_client("user_database")
78
 
79
  # Configuraci贸n de contenedores
80
+ container_config = {
81
+ "users": "/id",
82
+ "application_requests": "/email",
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