]> git.ktnx.net Git - mobile-ledger.git/commitdiff
(yet) untested/unused methods for applying incremental revisions to the database...
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 6 Dec 2018 20:41:03 +0000 (20:41 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 6 Dec 2018 20:41:03 +0000 (20:41 +0000)
app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java

index d8b84fe15372eb52f6b8c4bf378af8b093e3af93..1f5801a7018ffbb2a181b6c77bcc0bc82caa9a0f 100644 (file)
@@ -1,7 +1,13 @@
 package net.ktnx.mobileledger;
 
+import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 
 class MobileLedgerDB {
     static final String DATABASE_NAME = "accounts";
@@ -25,6 +31,35 @@ class MobileLedgerDB {
         db.execSQL("create unique index if not exists idx_options_name on options(name);");
     }
 
+    static void applyRevisions(Resources rm) {
+        int next_ver = Integer.parseInt(get_option_value("db_version", "0")) + 1;
+
+        while (applyRevision(rm, next_ver)) {
+            next_ver++;
+        }
+    }
+    private static boolean applyRevision(Resources rm, int next_ver) {
+        try (InputStream res = rm.openRawResource(next_ver)) {
+            Log.d("db", "Applying revision " + String.valueOf(next_ver));
+            InputStreamReader isr = new InputStreamReader(res);
+            BufferedReader reader = new BufferedReader(isr);
+
+            String line;
+            while ((line = reader.readLine()) != null) {
+                db.execSQL(line);
+            }
+
+            db.setTransactionSuccessful();
+        } catch (Exception e) {
+            return false;
+        }
+        finally {
+            db.endTransaction();
+        }
+
+        return true;
+    }
+
     static int get_option_value(String name, int default_value) {
         String s = get_option_value(name, String.valueOf(default_value));
         try {