From c0ee21d6fa8d5d61c029bd9334a7674fce99f729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elisi=C3=A1rio=20Couto?= Date: Wed, 10 Sep 2025 23:16:42 +0100 Subject: [PATCH] fix: correct composite key migration check - Fix _check_composite_key_migration_needed to properly check if internalTransactionId is the primary key - Use PRAGMA table_info pk flag instead of just checking column existence - This ensures migration only runs when internalTransactionId is actually the primary key --- leggend/services/database_service.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/leggend/services/database_service.py b/leggend/services/database_service.py index 902d6e5..9408195 100644 --- a/leggend/services/database_service.py +++ b/leggend/services/database_service.py @@ -551,10 +551,15 @@ class DatabaseService: # Check if transactions table has the old primary key structure cursor.execute("PRAGMA table_info(transactions)") columns = cursor.fetchall() - column_names = [col[1] for col in columns] - # If we have internalTransactionId as primary key, migration is needed - if "internalTransactionId" in column_names: + # Check if internalTransactionId is the primary key (old structure) + internal_transaction_id_is_pk = any( + col[1] == "internalTransactionId" and col[5] == 1 # col[5] is pk flag + for col in columns + ) + + # If internalTransactionId is still the primary key, migration is needed + if internal_transaction_id_is_pk: # Check if there are duplicate (accountId, transactionId) pairs cursor.execute(""" SELECT COUNT(*) as duplicates @@ -570,6 +575,7 @@ class DatabaseService: conn.close() return duplicates > 0 else: + # Migration already completed conn.close() return False