diff --git a/modules/system/databaseMigration.py b/modules/system/databaseMigration.py index d255bde2..645fcab7 100644 --- a/modules/system/databaseMigration.py +++ b/modules/system/databaseMigration.py @@ -743,15 +743,15 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st conn = _getConnection(dbName) try: existingTables = set(_listTables(conn)) + conn.rollback() # Ensure all import tables exist (create missing ones from export schema) + conn.autocommit = True for tableName, rows in tables.items(): if tableName in excluded or not isinstance(rows, list) or not rows: continue if tableName not in existingTables: - conn.autocommit = True _createTableFromExport(conn, tableName, rows) - conn.autocommit = False existingTables.add(tableName) logger.info("Pre-created missing table %s.%s", dbName, tableName) @@ -770,9 +770,9 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st # Phase 1 (replace only): DELETE children first (reverse topological order) if mode == "replace": + conn.autocommit = False for tableName in reversed(importOrder): try: - conn.autocommit = False _deleteNonProtected(conn, tableName, protectedIdSet) conn.commit() except Exception as e: @@ -781,12 +781,13 @@ def _importSingleDb(payload: dict, dbName: str, mode: str, protectedIds: List[st logger.warning("DELETE from %s.%s failed: %s", dbName, tableName, e) # Phase 2: INSERT parents first (topological order) + conn.autocommit = False for tableName in importOrder: try: - conn.autocommit = False rows = tables[tableName] physicalCols = _getPhysicalColumns(conn, tableName) if not physicalCols: + conn.rollback() continue filteredRows = []