From de103046c79f9f2e2dab1c50cc806aa02a35094e Mon Sep 17 00:00:00 2001
From: Michael Zikeli <michael.zikeli@fau.de>
Date: Fri, 7 Feb 2025 17:27:49 +0100
Subject: [PATCH] Enhance schema update logic in insert.py to check for
 existing columns before adding new ones, preventing duplicate column name
 warnings.

---
 python/waLBerla/tools/sqlitedb/insert.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/python/waLBerla/tools/sqlitedb/insert.py b/python/waLBerla/tools/sqlitedb/insert.py
index 2e759af07..8549a8bf6 100644
--- a/python/waLBerla/tools/sqlitedb/insert.py
+++ b/python/waLBerla/tools/sqlitedb/insert.py
@@ -139,16 +139,26 @@ def checkAndUpdateSchema(data, tableName, dbFile="database.sqlite", referenceRun
     columns = ["%s %s" % e for e in zip(names, types)]
 
     create_query = "CREATE TABLE IF NOT EXISTS %s ( %s );" % (tableName, ",".join(columns))
-    alter_queries = ["ALTER TABLE %s ADD COLUMN %s %s;" % (tableName, key, typ) for key, typ in zip(names, types)]
 
     conn = sqlite3.connect(dbFile)
     c = conn.cursor()
 
+    # Execute the create table query
     c.execute(create_query)
+
     if alter_table:
-        for q in alter_queries:
+        # Fetch existing columns
+        c.execute(f"PRAGMA table_info({tableName})")
+        existing_columns = [info[1] for info in c.fetchall()]
+
+        # Filter out columns that already exist
+        new_columns = [(key, typ) for key, typ in zip(names, types) if key not in existing_columns]
+
+        # Generate and execute alter table queries for new columns
+        for key, typ in new_columns:
+            alter_query = f"ALTER TABLE {tableName} ADD COLUMN {key} {typ};"
             try:
-                c.execute(q)
+                c.execute(alter_query)
             except sqlite3.OperationalError as e:
                 print(e)
                 pass
-- 
GitLab