]> git.ktnx.net Git - mobile-ledger.git/commitdiff
add uuid to templates
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 21 Aug 2021 13:25:34 +0000 (16:25 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 21 Aug 2021 13:25:34 +0000 (16:25 +0300)
will be used to distinguish new templates when restoring configuration
from JSON

app/src/main/java/net/ktnx/mobileledger/db/DB.java
app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java
app/src/main/res/raw/db_63.sql [new file with mode: 0644]

index 2e35e9108c689dd953e894995fdff8f0790196f0..b03faa62fbb1d19b9e75a2c8ce1a2b33a49c54a4 100644 (file)
@@ -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<Boolean> 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)});
+                        }
+                    }
+                }
             }
         };
     }
index 9d355d09cfc46de8309d573bf59eb3f20813248a..87ea4fd84050ca741f3acdfe013718cbff3c5d7a 100644 (file)
@@ -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 (file)
index 0000000..4b11f7f
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+
+-- 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