db fixed import
This commit is contained in:
parent
1a1128cc8c
commit
6b5e386469
1 changed files with 31 additions and 20 deletions
|
|
@ -742,18 +742,18 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st
|
||||||
|
|
||||||
conn = _getConnection(dbName)
|
conn = _getConnection(dbName)
|
||||||
try:
|
try:
|
||||||
conn.autocommit = False
|
|
||||||
existingTables = set(_listTables(conn))
|
existingTables = set(_listTables(conn))
|
||||||
|
|
||||||
# Pre-create missing tables so FK ordering can discover them
|
# Ensure all import tables exist (create missing ones from export schema)
|
||||||
for tableName, rows in tables.items():
|
for tableName, rows in tables.items():
|
||||||
if tableName in excluded or not isinstance(rows, list):
|
if tableName in excluded or not isinstance(rows, list) or not rows:
|
||||||
continue
|
continue
|
||||||
if tableName not in existingTables:
|
if tableName not in existingTables:
|
||||||
|
conn.autocommit = True
|
||||||
_createTableFromExport(conn, tableName, rows)
|
_createTableFromExport(conn, tableName, rows)
|
||||||
conn.commit()
|
|
||||||
conn.autocommit = False
|
conn.autocommit = False
|
||||||
existingTables.add(tableName)
|
existingTables.add(tableName)
|
||||||
|
logger.info("Pre-created missing table %s.%s", dbName, tableName)
|
||||||
|
|
||||||
# Build importable table list and sort by FK dependencies
|
# Build importable table list and sort by FK dependencies
|
||||||
importable = [t for t in tables
|
importable = [t for t in tables
|
||||||
|
|
@ -771,10 +771,19 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st
|
||||||
# Phase 1 (replace only): DELETE children first (reverse topological order)
|
# Phase 1 (replace only): DELETE children first (reverse topological order)
|
||||||
if mode == "replace":
|
if mode == "replace":
|
||||||
for tableName in reversed(importOrder):
|
for tableName in reversed(importOrder):
|
||||||
|
try:
|
||||||
|
conn.autocommit = False
|
||||||
_deleteNonProtected(conn, tableName, protectedIdSet)
|
_deleteNonProtected(conn, tableName, protectedIdSet)
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
warnings.append(f"DELETE from {dbName}.{tableName} failed: {e}")
|
||||||
|
logger.warning("DELETE from %s.%s failed: %s", dbName, tableName, e)
|
||||||
|
|
||||||
# Phase 2: INSERT parents first (topological order)
|
# Phase 2: INSERT parents first (topological order)
|
||||||
for tableName in importOrder:
|
for tableName in importOrder:
|
||||||
|
try:
|
||||||
|
conn.autocommit = False
|
||||||
rows = tables[tableName]
|
rows = tables[tableName]
|
||||||
physicalCols = _getPhysicalColumns(conn, tableName)
|
physicalCols = _getPhysicalColumns(conn, tableName)
|
||||||
if not physicalCols:
|
if not physicalCols:
|
||||||
|
|
@ -789,11 +798,13 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st
|
||||||
filteredRows.append(row)
|
filteredRows.append(row)
|
||||||
|
|
||||||
insertedCount = _insertRows(conn, tableName, filteredRows, physicalCols, mode)
|
insertedCount = _insertRows(conn, tableName, filteredRows, physicalCols, mode)
|
||||||
dbResult[tableName] = insertedCount
|
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
dbResult[tableName] = insertedCount
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
warnings.append(f"INSERT into {dbName}.{tableName} failed: {e}")
|
||||||
|
logger.warning("INSERT into %s.%s failed: %s", dbName, tableName, e)
|
||||||
|
except Exception as e:
|
||||||
logger.error("Import failed for database %s: %s", dbName, e)
|
logger.error("Import failed for database %s: %s", dbName, e)
|
||||||
return {"database": dbName, "tables": {}, "recordCount": 0,
|
return {"database": dbName, "tables": {}, "recordCount": 0,
|
||||||
"warnings": [f"Import fuer '{dbName}' fehlgeschlagen: {e}"]}
|
"warnings": [f"Import fuer '{dbName}' fehlgeschlagen: {e}"]}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue