+ }
+ }
+ 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));
+ }
+ };
+ }
+ private static Migration multiVersionMigration(int fromVersion, int toVersion) {
+ return new Migration(fromVersion, toVersion) {
+ @Override
+ public void migrate(@NonNull SupportSQLiteDatabase db) {
+ String fileName = String.format(Locale.US, "db_%d_%d", fromVersion, toVersion);
+
+ applyRevisionFile(db, fileName);
+ }
+ };
+ }
+ public static void applyRevisionFile(@NonNull SupportSQLiteDatabase db, String fileName) {
+ final Resources rm = App.instance.getResources();
+ int res_id = rm.getIdentifier(fileName, "raw", App.instance.getPackageName());
+ if (res_id == 0)
+ throw new SQLException(String.format(Locale.US, "No resource for %s", fileName));
+
+ try (InputStream res = rm.openRawResource(res_id)) {
+ debug("db", "Applying " + fileName);
+ InputStreamReader isr = new InputStreamReader(res);
+ BufferedReader reader = new BufferedReader(isr);
+
+ Pattern endOfStatement = Pattern.compile(";\\s*(?:--.*)?$");
+
+ String line;
+ String sqlStatement = null;
+ int lineNo = 0;
+ while ((line = reader.readLine()) != null) {
+ lineNo++;
+ if (line.startsWith("--"))
+ continue;
+ if (line.isEmpty())
+ continue;
+
+ if (sqlStatement == null)
+ sqlStatement = line;
+ else
+ sqlStatement = sqlStatement.concat(" " + line);
+
+ Matcher m = endOfStatement.matcher(line);
+ if (!m.find())
+ continue;
+
+ try {
+ db.execSQL(sqlStatement);
+ sqlStatement = null;
+ }
+ catch (Exception e) {
+ throw new RuntimeException(
+ String.format("Error applying %s, line %d, statement: %s", fileName,
+ lineNo, sqlStatement), e);
+ }
+ }
+
+ if (sqlStatement != null)
+ throw new RuntimeException(String.format(
+ "Error applying %s: EOF after continuation. Line %s, Incomplete " +
+ "statement: %s", fileName, lineNo, sqlStatement));
+
+ }
+ catch (IOException e) {
+ throw new RuntimeException(String.format("Error opening raw resource for %s", fileName),
+ e);