From: Damyan Ivanov Date: Thu, 6 Dec 2018 20:41:03 +0000 (+0000) Subject: (yet) untested/unused methods for applying incremental revisions to the database... X-Git-Tag: v0.3~318 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=27b1c52d2acdf70888185643d3d4bc04e5ba74f5;p=mobile-ledger.git (yet) untested/unused methods for applying incremental revisions to the database structure --- diff --git a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java index d8b84fe1..1f5801a7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java @@ -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 {