Skip to content
Snippets Groups Projects
Commit de103046 authored by Michael Zikeli's avatar Michael Zikeli
Browse files

Enhance schema update logic in insert.py to check for existing columns before...

Enhance schema update logic in insert.py to check for existing columns before adding new ones, preventing duplicate column name warnings.
parent 5a335dfe
Branches
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment