]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/db/DB.java
methods for deleting all DB tables
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / DB.java
index 173d730ec99286eff98a75152bc0ea2ae503ebdf..482628888b1b021c9b78d53eb159ab974c479d2e 100644 (file)
 package net.ktnx.mobileledger.db;
 
 import android.content.res.Resources;
+import android.database.Cursor;
 import android.database.SQLException;
 
 import androidx.annotation.NonNull;
+import androidx.lifecycle.MutableLiveData;
 import androidx.room.Database;
 import androidx.room.Room;
 import androidx.room.RoomDatabase;
@@ -29,15 +31,24 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
 
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.dao.AccountValueDAO;
 import net.ktnx.mobileledger.dao.CurrencyDAO;
+import net.ktnx.mobileledger.dao.OptionDAO;
+import net.ktnx.mobileledger.dao.ProfileDAO;
 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
+import net.ktnx.mobileledger.dao.TransactionAccountDAO;
+import net.ktnx.mobileledger.dao.TransactionDAO;
+import net.ktnx.mobileledger.utils.Logger;
+
+import org.jetbrains.annotations.NotNull;
 
 import java.io.BufferedReader;
 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;
 
@@ -45,12 +56,13 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 @Database(version = DB.REVISION,
           entities = {TemplateHeader.class, TemplateAccount.class, Currency.class, Account.class,
-                      Profile.class, Option.class, AccountValue.class, DescriptionHistory.class,
-                      Transaction.class, TransactionAccount.class
+                      Profile.class, Option.class, AccountValue.class, Transaction.class,
+                      TransactionAccount.class
           })
 abstract public class DB extends RoomDatabase {
-    public static final int REVISION = 58;
+    public static final int REVISION = 65;
     public static final String DB_NAME = "MoLe.db";
+    public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
     private static DB instance;
     public static DB get() {
         if (instance != null)
@@ -59,29 +71,50 @@ abstract public class DB extends RoomDatabase {
             if (instance != null)
                 return instance;
 
-            return instance = Room.databaseBuilder(App.instance, DB.class, DB_NAME)
-                                  .addMigrations(new Migration[]{singleVersionMigration(17),
-                                                                 singleVersionMigration(18),
-                                                                 singleVersionMigration(19),
-                                                                 singleVersionMigration(20),
-                                                                 multiVersionMigration(20, 22),
-                                                                 multiVersionMigration(22, 30),
-                                                                 multiVersionMigration(30, 32),
-                                                                 multiVersionMigration(32, 34),
-                                                                 multiVersionMigration(34, 40),
-                                                                 singleVersionMigration(41),
-                                                                 multiVersionMigration(41, 58),
-                                                                 })
-                                  .addCallback(new Callback() {
-                                      @Override
-                                      public void onOpen(@NonNull SupportSQLiteDatabase db) {
-                                          super.onOpen(db);
-                                          db.execSQL("PRAGMA foreign_keys = ON");
-                                          db.execSQL("pragma case_sensitive_like=ON;");
-
-                                      }
-                                  })
-                                  .build();
+            RoomDatabase.Builder<DB> builder =
+                    Room.databaseBuilder(App.instance, DB.class, DB_NAME);
+            builder.addMigrations(
+                    new Migration[]{singleVersionMigration(17), singleVersionMigration(18),
+                                    singleVersionMigration(19), singleVersionMigration(20),
+                                    multiVersionMigration(20, 22), multiVersionMigration(22, 30),
+                                    multiVersionMigration(30, 32), multiVersionMigration(32, 34),
+                                    multiVersionMigration(34, 40), singleVersionMigration(41),
+                                    multiVersionMigration(41, 58), singleVersionMigration(59),
+                                    singleVersionMigration(60), singleVersionMigration(61),
+                                    singleVersionMigration(62), singleVersionMigration(63),
+                                    singleVersionMigration(64), new Migration(64, 65) {
+                        @Override
+                        public void migrate(@NonNull @NotNull SupportSQLiteDatabase database) {
+                            try (Cursor c = database.query(
+                                    "SELECT id, description FROM transactions"))
+                            {
+                                while (c.moveToNext()) {
+                                    final long id = c.getLong(0);
+                                    final String description = c.getString(1);
+                                    database.execSQL(
+                                            "UPDATE transactions SET description_uc=? WHERE id=?",
+                                            new Object[]{description.toUpperCase(), id
+                                            });
+                                }
+                            }
+                        }
+                    }
+                    })
+                   .addCallback(new Callback() {
+                       @Override
+                       public void onOpen(@NonNull SupportSQLiteDatabase db) {
+                           super.onOpen(db);
+                           db.execSQL("PRAGMA foreign_keys = ON");
+                           db.execSQL("pragma case_sensitive_like" + "=ON;");
+
+                       }
+                   });
+
+//            if (BuildConfig.DEBUG)
+//                builder.setQueryCallback(((sqlQuery, bindArgs) -> Logger.debug("room", sqlQuery)),
+//                        Executors.newSingleThreadExecutor());
+
+            return instance = builder.build();
         }
     }
     private static Migration singleVersionMigration(int toVersion) {
@@ -91,6 +124,42 @@ abstract public class DB extends RoomDatabase {
                 String fileName = String.format(Locale.US, "db_%d", toVersion);
 
                 applyRevisionFile(db, fileName);
+
+                // when migrating to version 59, migrate profile/theme options to the
+                // SharedPreferences
+                if (toVersion == 59) {
+                    try (Cursor c = db.query(
+                            "SELECT p.id, p.theme FROM profiles p WHERE p.id=(SELECT o.value " +
+                            "FROM options o WHERE o.profile_id=0 AND o.name=?)",
+                            new Object[]{"profile_id"}))
+                    {
+                        if (c.moveToFirst()) {
+                            long currentProfileId = c.getLong(0);
+                            int currentTheme = c.getInt(1);
+
+                            if (currentProfileId >= 0 && currentTheme >= 0) {
+                                App.storeStartupProfileAndTheme(currentProfileId, currentTheme);
+                            }
+                        }
+                    }
+                }
+                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)});
+                        }
+                    }
+                }
+            }
+        };
+    }
+    private static Migration dummyVersionMigration(int toVersion) {
+        return new Migration(toVersion - 1, toVersion) {
+            @Override
+            public void migrate(@NonNull SupportSQLiteDatabase db) {
+                Logger.debug("db",
+                        String.format(Locale.ROOT, "Dummy DB migration to version %d", toVersion));
             }
         };
     }
@@ -165,4 +234,27 @@ abstract public class DB extends RoomDatabase {
     public abstract CurrencyDAO getCurrencyDAO();
 
     public abstract AccountDAO getAccountDAO();
+
+    public abstract AccountValueDAO getAccountValueDAO();
+
+    public abstract TransactionDAO getTransactionDAO();
+
+    public abstract TransactionAccountDAO getTransactionAccountDAO();
+
+    public abstract OptionDAO getOptionDAO();
+
+    public abstract ProfileDAO getProfileDAO();
+
+    @androidx.room.Transaction
+    public void deleteAllSync() {
+        getTransactionAccountDAO().deleteAllSync();
+        getTransactionDAO().deleteAllSync();
+        getAccountValueDAO().deleteAllSync();
+        getAccountDAO().deleteAllSync();
+        getTemplateAccountDAO().deleteAllSync();
+        getTemplateDAO().deleteAllSync();
+        getCurrencyDAO().deleteAllSync();
+        getOptionDAO().deleteAllSync();
+        getProfileDAO().deleteAllSync();
+    }
 }