Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -91,36 +91,44 @@ class PostgresManager:
|
|
| 91 |
|
| 92 |
def _rewrite_line_for_schema(self, line: str, schema_name: str) -> str:
|
| 93 |
"""
|
| 94 |
-
Reescribe referencias a public.
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
"""
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
r'\b(public|pagila)\.("?[A-Za-z_][A-Za-z0-9_]*"?)',
|
| 102 |
-
flags=re.IGNORECASE,
|
| 103 |
-
)
|
| 104 |
-
line = pattern_schema.sub(
|
| 105 |
lambda m: f'{schema_name}.{m.group(2)}',
|
| 106 |
line,
|
|
|
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
# 2)
|
| 110 |
-
|
| 111 |
r"'public\.([A-Za-z_][A-Za-z0-9_]*)'::regclass",
|
| 112 |
-
flags=re.IGNORECASE,
|
| 113 |
-
)
|
| 114 |
-
line = pattern_regclass.sub(
|
| 115 |
lambda m: f"'{schema_name}.{m.group(1)}'::regclass",
|
| 116 |
line,
|
|
|
|
| 117 |
)
|
| 118 |
-
|
| 119 |
-
# 3)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
return line
|
| 125 |
|
| 126 |
def _should_skip_statement(self, stmt: str) -> bool:
|
|
@@ -273,8 +281,17 @@ class PostgresManager:
|
|
| 273 |
|
| 274 |
# Detectar inicio de COPY
|
| 275 |
if (
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
):
|
| 279 |
# Ejecutar lo que haya pendiente antes del COPY
|
| 280 |
flush_statement()
|
|
|
|
| 91 |
|
| 92 |
def _rewrite_line_for_schema(self, line: str, schema_name: str) -> str:
|
| 93 |
"""
|
| 94 |
+
Reescribe TODAS las referencias a public.XXX o pagila.XXX:
|
| 95 |
+
- COPY public.table
|
| 96 |
+
- ALTER TABLE ONLY public.table
|
| 97 |
+
- REFERENCES public.table
|
| 98 |
+
- SET search_path
|
| 99 |
+
- Literales 'public.table'::regclass
|
| 100 |
"""
|
| 101 |
+
|
| 102 |
+
original = line
|
| 103 |
+
|
| 104 |
+
# 0) Borrar cualquier SET search_path
|
| 105 |
+
if "search_path" in line.lower():
|
| 106 |
+
return ""
|
| 107 |
+
|
| 108 |
+
# 1) Reemplazar public.xxx y pagila.xxx en cualquier contexto
|
| 109 |
+
line = re.sub(
|
| 110 |
r'\b(public|pagila)\.("?[A-Za-z_][A-Za-z0-9_]*"?)',
|
|
|
|
|
|
|
|
|
|
| 111 |
lambda m: f'{schema_name}.{m.group(2)}',
|
| 112 |
line,
|
| 113 |
+
flags=re.IGNORECASE
|
| 114 |
)
|
| 115 |
+
|
| 116 |
+
# 2) Reemplazar literales 'public.xxx'::regclass
|
| 117 |
+
line = re.sub(
|
| 118 |
r"'public\.([A-Za-z_][A-Za-z0-9_]*)'::regclass",
|
|
|
|
|
|
|
|
|
|
| 119 |
lambda m: f"'{schema_name}.{m.group(1)}'::regclass",
|
| 120 |
line,
|
| 121 |
+
flags=re.IGNORECASE
|
| 122 |
)
|
| 123 |
+
|
| 124 |
+
# 3) Reemplazar SOLO si COPY está antes de reescribir
|
| 125 |
+
if line.strip().upper().startswith("COPY "):
|
| 126 |
+
line = line.replace("public.", f"{schema_name}.")
|
| 127 |
+
|
| 128 |
+
# 4) Igual para ALTER TABLE ONLY
|
| 129 |
+
if "ALTER TABLE ONLY" in line.upper():
|
| 130 |
+
line = line.replace("public.", f"{schema_name}.")
|
| 131 |
+
|
| 132 |
return line
|
| 133 |
|
| 134 |
def _should_skip_statement(self, stmt: str) -> bool:
|
|
|
|
| 281 |
|
| 282 |
# Detectar inicio de COPY
|
| 283 |
if (
|
| 284 |
+
# Primero reescribimos la línea según el schema
|
| 285 |
+
line = self._rewrite_line_for_schema(line, schema_name)
|
| 286 |
+
stripped = line.strip()
|
| 287 |
+
|
| 288 |
+
# SOLO después detectamos COPY
|
| 289 |
+
if stripped.upper().startswith("COPY ") and "FROM stdin" in stripped.upper():
|
| 290 |
+
flush_statement()
|
| 291 |
+
in_copy = True
|
| 292 |
+
copy_sql = stripped
|
| 293 |
+
copy_lines = []
|
| 294 |
+
continue
|
| 295 |
):
|
| 296 |
# Ejecutar lo que haya pendiente antes del COPY
|
| 297 |
flush_statement()
|