From fa89888228f1a4469eceed99c3a5adf4c5bf2180 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 19 Dec 2022 21:16:31 -0500 Subject: [PATCH] Fix migration again. --- .gitignore | 1 - app/.gitignore | 4 +++- app/src/main/java/com/jerboa/db/AppDB.kt | 22 ++++++++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 31f74f3dc..fefbba64a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ .externalNativeBuild .cxx local.properties -app/release .project .settings .classpath diff --git a/app/.gitignore b/app/.gitignore index 42afabfd2..d9e6f61a7 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1,3 @@ -/build \ No newline at end of file +/build +/release +/schemas diff --git a/app/src/main/java/com/jerboa/db/AppDB.kt b/app/src/main/java/com/jerboa/db/AppDB.kt index bd08fafcd..4d49219f0 100644 --- a/app/src/main/java/com/jerboa/db/AppDB.kt +++ b/app/src/main/java/com/jerboa/db/AppDB.kt @@ -1,6 +1,8 @@ package com.jerboa.db +import android.content.ContentValues import android.content.Context +import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE import androidx.annotation.WorkerThread import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel @@ -10,6 +12,7 @@ import androidx.room.* import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import kotlinx.coroutines.launch +import java.util.concurrent.Executors @Entity data class Account( @@ -147,7 +150,6 @@ val MIGRATION_2_3 = object : Migration(2, 3) { NOT NULL DEFAULT 0) """ ) - database.execSQL("insert into AppSettings default values") } } @@ -177,7 +179,23 @@ abstract class AppDB : RoomDatabase() { ) .allowMainThreadQueries() .addMigrations(MIGRATION_1_2, MIGRATION_2_3) - .build() + // Necessary because it can't insert data on creation + .addCallback(object : Callback() { + override fun onOpen(db: SupportSQLiteDatabase) { + super.onCreate(db) + Executors.newSingleThreadExecutor().execute { + db.insert( + "AppSettings", + CONFLICT_IGNORE, // Ensures it won't overwrite the existing data + ContentValues(2).apply { + put("id", 1) + put("font_size", 13) + put("theme", 0) + } + ) + } + } + }).build() INSTANCE = instance // return instance instance