stvnnnnnn commited on
Commit
2d4eabc
verified
1 Parent(s): 72acd30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -4
app.py CHANGED
@@ -181,6 +181,14 @@ class PostgresManager:
181
  )
182
  )
183
 
 
 
 
 
 
 
 
 
184
  in_copy = False
185
  copy_sql = ""
186
  copy_rows: list[str] = []
@@ -391,7 +399,7 @@ class PostgresManager:
391
 
392
  def get_schema(self, connection_id: str) -> Dict[str, Any]:
393
  info = self._get_info(connection_id)
394
- schema = info["schema"]
395
 
396
  conn = self._get_conn()
397
  try:
@@ -399,7 +407,7 @@ class PostgresManager:
399
  foreign_keys: List[Dict[str, Any]] = []
400
 
401
  with conn.cursor() as cur:
402
- # Tablas b谩sicas
403
  cur.execute(
404
  """
405
  SELECT table_name
@@ -412,7 +420,56 @@ class PostgresManager:
412
  )
413
  tables = [r[0] for r in cur.fetchall()]
414
 
415
- # Columnas por tabla
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  for t in tables:
417
  cur.execute(
418
  """
@@ -427,7 +484,7 @@ class PostgresManager:
427
  cols = [r[0] for r in cur.fetchall()]
428
  tables_info[t] = {"columns": cols}
429
 
430
- # Foreign keys
431
  cur.execute(
432
  """
433
  SELECT
 
181
  )
182
  )
183
 
184
+ # 馃攣 Reescribir referencias a esquemas globales (public, pagila, etc.)
185
+ # para que todo termine dentro de nuestro schema aislado de sesi贸n.
186
+ pattern_public = re.compile(r'\bpublic\.', flags=re.IGNORECASE)
187
+ pattern_pagila = re.compile(r'\bpagila\.', flags=re.IGNORECASE)
188
+
189
+ sql_text = pattern_public.sub(f'{schema_name}.', sql_text)
190
+ sql_text = pattern_pagila.sub(f'{schema_name}.', sql_text)
191
+
192
  in_copy = False
193
  copy_sql = ""
194
  copy_rows: list[str] = []
 
399
 
400
  def get_schema(self, connection_id: str) -> Dict[str, Any]:
401
  info = self._get_info(connection_id)
402
+ schema = info["schema"] # schema "ideal" que registramos
403
 
404
  conn = self._get_conn()
405
  try:
 
407
  foreign_keys: List[Dict[str, Any]] = []
408
 
409
  with conn.cursor() as cur:
410
+ # 1) Intentamos solo con el schema registrado
411
  cur.execute(
412
  """
413
  SELECT table_name
 
420
  )
421
  tables = [r[0] for r in cur.fetchall()]
422
 
423
+ # 2) 馃攣 Fallback: si no hay tablas en ese schema,
424
+ # buscamos en TODOS los schemas de usuario
425
+ if not tables:
426
+ cur.execute(
427
+ """
428
+ SELECT table_schema, table_name
429
+ FROM information_schema.tables
430
+ WHERE table_type = 'BASE TABLE'
431
+ AND table_schema NOT IN ('pg_catalog','information_schema')
432
+ ORDER BY table_schema, table_name;
433
+ """
434
+ )
435
+ rows = cur.fetchall()
436
+
437
+ if not rows:
438
+ # No hay tablas en ning煤n schema de usuario
439
+ return {
440
+ "tables": {},
441
+ "foreign_keys": [],
442
+ }
443
+
444
+ # Schemas candidatos que s铆 tienen tablas
445
+ schemas = sorted({s for (s, _) in rows})
446
+
447
+ # Preferimos:
448
+ # 1) el schema ya registrado (si por alguna raz贸n tiene tablas)
449
+ # 2) 'pagila'
450
+ # 3) 'public'
451
+ # 4) el primero que aparezca
452
+ target_schema = None
453
+ if schema in schemas:
454
+ target_schema = schema
455
+ elif "pagila" in schemas:
456
+ target_schema = "pagila"
457
+ elif "public" in schemas:
458
+ target_schema = "public"
459
+ else:
460
+ target_schema = schemas[0]
461
+
462
+ print(
463
+ f"[WARN] Schema '{schema}' sin tablas; usando schema real '{target_schema}'"
464
+ )
465
+
466
+ # Actualizamos el schema asociado a esta conexi贸n
467
+ schema = target_schema
468
+ info["schema"] = schema
469
+
470
+ tables = [t for (s, t) in rows if s == schema]
471
+
472
+ # 3) Columnas por tabla del schema final seleccionado
473
  for t in tables:
474
  cur.execute(
475
  """
 
484
  cols = [r[0] for r in cur.fetchall()]
485
  tables_info[t] = {"columns": cols}
486
 
487
+ # 4) Foreign keys del schema final
488
  cur.execute(
489
  """
490
  SELECT