From 9c1cc1325929c405e30e36dd7ff28927e14f57c0 Mon Sep 17 00:00:00 2001 From: Michael Totschnig Date: Wed, 28 Aug 2024 22:02:37 +0200 Subject: [PATCH] Fix failing upgrade from old version (older then db version 145): then attribute is already configured on current enum values We forgot to add the condition on minimum oldVersion We now additionally insert with CONFLICT_IGNORE --- .../myexpenses/provider/BaseTransactionDatabase.kt | 6 +++--- .../java/org/totschnig/myexpenses/provider/MoreDbUtils.kt | 7 +++++++ .../totschnig/myexpenses/provider/TransactionDatabase.java | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/BaseTransactionDatabase.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/BaseTransactionDatabase.kt index 1c16491edd..7c3c7c0617 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/BaseTransactionDatabase.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/BaseTransactionDatabase.kt @@ -910,7 +910,7 @@ abstract class BaseTransactionDatabase( } fun SupportSQLiteDatabase.upgradeTo164() { - insert("attributes", ContentValues().apply { + safeInsert("attributes", ContentValues().apply { put("attribute_name", BankingAttribute.BLZ.name) put("context", BankingAttribute.BLZ.context) }) @@ -922,11 +922,11 @@ abstract class BaseTransactionDatabase( } fun SupportSQLiteDatabase.upgradeTo167() { - insert("attributes", ContentValues().apply { + safeInsert("attributes", ContentValues().apply { put("attribute_name", BankingAttribute.NAME.name) put("context", BankingAttribute.NAME.context) }) - insert("attributes", ContentValues().apply { + safeInsert("attributes", ContentValues().apply { put("attribute_name", BankingAttribute.BIC.name) put("context", BankingAttribute.BIC.context) }) diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/MoreDbUtils.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/MoreDbUtils.kt index ee25eec567..058abb6a0c 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/MoreDbUtils.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/MoreDbUtils.kt @@ -602,6 +602,13 @@ fun SupportSQLiteDatabase.update( fun SupportSQLiteDatabase.insert(table: String, values: ContentValues): Long = insert(table, SQLiteDatabase.CONFLICT_NONE, values) +/** + * insert where conflicts are ignored instead of raising exception + */ +fun SupportSQLiteDatabase.safeInsert(table: String, values: ContentValues): Long = + insert(table, SQLiteDatabase.CONFLICT_IGNORE, values) + + fun SupportSQLiteDatabase.query( table: String, columns: Array, diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/TransactionDatabase.java b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/TransactionDatabase.java index 0b41044043..a0e35b0890 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/provider/TransactionDatabase.java +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/provider/TransactionDatabase.java @@ -2092,7 +2092,7 @@ public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int new db.execSQL("UPDATE banks set version = 1"); } - if (oldVersion < 167) { + if (oldVersion >= 145 && oldVersion < 167) { upgradeTo167(db); }