From 4a44ce88b61f00c2710877cf26818db2728b5d91 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 21 Aug 2021 16:25:34 +0300 Subject: [PATCH] add uuid to templates will be used to distinguish new templates when restoring configuration from JSON --- .../java/net/ktnx/mobileledger/db/DB.java | 13 +++- .../ktnx/mobileledger/db/TemplateHeader.java | 19 +++++- app/src/main/res/raw/db_63.sql | 63 +++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/raw/db_63.sql diff --git a/app/src/main/java/net/ktnx/mobileledger/db/DB.java b/app/src/main/java/net/ktnx/mobileledger/db/DB.java index 2e35e910..b03faa62 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/DB.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/DB.java @@ -46,6 +46,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Locale; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -57,7 +58,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug; TransactionAccount.class }) abstract public class DB extends RoomDatabase { - public static final int REVISION = 62; + public static final int REVISION = 63; public static final String DB_NAME = "MoLe.db"; public static final MutableLiveData initComplete = new MutableLiveData<>(false); private static DB instance; @@ -78,7 +79,7 @@ abstract public class DB extends RoomDatabase { multiVersionMigration(34, 40), singleVersionMigration(41), multiVersionMigration(41, 58), singleVersionMigration(59), singleVersionMigration(60), singleVersionMigration(61), - singleVersionMigration(62) + singleVersionMigration(62), singleVersionMigration(63) }) .addCallback(new Callback() { @Override @@ -123,6 +124,14 @@ abstract public class DB extends RoomDatabase { } } } + if (toVersion == 63) { + try (Cursor c = db.query("SELECT id FROM templates")) { + while (c.moveToNext()) { + db.execSQL("UPDATE templates SET uuid=? WHERE id=?", + new Object[]{UUID.randomUUID().toString(), c.getLong(0)}); + } + } + } } }; } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java b/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java index 9d355d09..87ea4fd8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java @@ -21,13 +21,17 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.room.ColumnInfo; import androidx.room.Entity; +import androidx.room.Index; import androidx.room.PrimaryKey; import net.ktnx.mobileledger.utils.Misc; import org.jetbrains.annotations.NotNull; -@Entity(tableName = "templates") +import java.util.UUID; + +@Entity(tableName = "templates", + indices = {@Index(name = "templates_uuid_idx", unique = true, value = "uuid")}) public class TemplateHeader extends TemplateBase { @PrimaryKey(autoGenerate = true) private long id; @@ -35,6 +39,9 @@ public class TemplateHeader extends TemplateBase { @NonNull private String name; @NonNull + @ColumnInfo + private String uuid; + @NonNull @ColumnInfo(name = "regular_expression") private String regularExpression; @ColumnInfo(name = "test_text") @@ -66,10 +73,13 @@ public class TemplateHeader extends TemplateBase { this.id = id; this.name = name; this.regularExpression = regularExpression; + this.uuid = UUID.randomUUID() + .toString(); } public TemplateHeader(TemplateHeader origin) { id = origin.id; name = origin.name; + uuid = origin.uuid; regularExpression = origin.regularExpression; testText = origin.testText; transactionDescription = origin.transactionDescription; @@ -84,6 +94,13 @@ public class TemplateHeader extends TemplateBase { dateDayMatchGroup = origin.dateDayMatchGroup; isFallback = origin.isFallback; } + @NonNull + public String getUuid() { + return uuid; + } + public void setUuid(@NonNull String uuid) { + this.uuid = uuid; + } public boolean isFallback() { return isFallback; } diff --git a/app/src/main/res/raw/db_63.sql b/app/src/main/res/raw/db_63.sql new file mode 100644 index 00000000..4b11f7f7 --- /dev/null +++ b/app/src/main/res/raw/db_63.sql @@ -0,0 +1,63 @@ +-- Copyright © 2021 Damyan Ivanov. +-- This file is part of MoLe. +-- MoLe is free software: you can distribute it and/or modify it +-- under the term of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your opinion), any later version. +-- +-- MoLe is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License terms for details. +-- +-- You should have received a copy of the GNU General Public License +-- along with MoLe. If not, see . + +-- migrate from revision 62 to revision 63 + +-- pragmas need to be outside of transaction control +-- foreign_keys is needed so that foreign key constraints are redirected + +commit transaction; +pragma foreign_keys = off; + +begin transaction; + +CREATE TABLE new_templates ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + name TEXT NOT NULL, + uuid TEXT NOT NULL, + regular_expression TEXT NOT NULL, + test_text TEXT, + transaction_description TEXT, + transaction_description_match_group INTEGER, + transaction_comment TEXT, + transaction_comment_match_group INTEGER, + date_year INTEGER, + date_year_match_group INTEGER, + date_month INTEGER, + date_month_match_group INTEGER, + date_day INTEGER, + date_day_match_group INTEGER, + is_fallback INTEGER NOT NULL DEFAULT 0); + +insert into new_templates(id, name, uuid, regular_expression, test_text, + transaction_description, transaction_description_match_group, + transaction_comment, transaction_comment_match_group, + date_year, date_year_match_group, + date_month, date_month_match_group, + date_day, date_day_match_group, + is_fallback) +select id, name, random(), regular_expression, test_text, + transaction_description, transaction_description_match_group, + transaction_comment, transaction_comment_match_group, + date_year, date_year_match_group, + date_month, date_month_match_group, + date_day, date_day_match_group, + is_fallback +from templates; + +drop table templates; +alter table new_templates rename to templates; + +create unique index templates_uuid_idx on templates(uuid); \ No newline at end of file -- 2.39.5