]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
initialize Room after all legacy upgrading is done
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
index e0a0a421c58ca3e22495b17c6218e07bbe88c92e..78c325e1317ef7673fcd6e20119ec3254eb2e98e 100644 (file)
@@ -22,11 +22,11 @@ import android.content.res.Resources;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
-import android.util.Log;
 
 import androidx.lifecycle.MutableLiveData;
 
 import net.ktnx.mobileledger.BuildConfig;
+import net.ktnx.mobileledger.db.DB;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -41,10 +41,17 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 public class MobileLedgerDatabase extends SQLiteOpenHelper {
     public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
     public static final String DB_NAME = "MoLe.db";
-    private static final int LATEST_REVISION = 55;
+    private static final int LATEST_REVISION = 58;
     private static final String CREATE_DB_SQL = "create_db";
     private final Application mContext;
-
+    @Override
+    public void onOpen(SQLiteDatabase db) {
+        super.onOpen(db);
+        // force a check by Room to ensure everything is OK
+        // TODO: remove when all DB structure manipulation is via Room
+        DB.get()
+          .compileStatement("SELECT COUNT(*) FROM profiles");
+    }
     public MobileLedgerDatabase(Application context) {
         super(context, DB_NAME, null, LATEST_REVISION);
         debug("db", "creating helper instance");
@@ -89,11 +96,10 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper {
             InputStreamReader isr = new InputStreamReader(res);
             BufferedReader reader = new BufferedReader(isr);
 
-            Pattern continuation = Pattern.compile("\\\\\\s*$");
+            Pattern endOfStatement = Pattern.compile(";\\s*(?:--.*)?$");
 
             String line;
             String sqlStatement = null;
-            boolean eolPending;
             int lineNo = 0;
             while ((line = reader.readLine()) != null) {
                 lineNo++;
@@ -102,19 +108,13 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper {
                 if (line.isEmpty())
                     continue;
 
-                Matcher m = continuation.matcher(line);
-                eolPending = false;
-                if (m.find()) {
-                    line = m.replaceFirst("");
-                    eolPending = true;
-                }
-
                 if (sqlStatement == null)
                     sqlStatement = line;
                 else
                     sqlStatement = sqlStatement.concat(line);
 
-                if (eolPending)
+                Matcher m = endOfStatement.matcher(line);
+                if (!m.find())
                     continue;
 
                 try {
@@ -129,8 +129,9 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper {
             }
 
             if (sqlStatement != null)
-                throw new RuntimeException(
-                        String.format("Error applying %s: EOF after continuation", revFile));
+                throw new RuntimeException(String.format(
+                        "Error applying %s: EOF after continuation. Line %s, Incomplete " +
+                        "statement: %s", revFile, lineNo, sqlStatement));
 
         }
         catch (IOException e) {