Skip to content

Commit

Permalink
Fix migration again.
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Dec 20, 2022
1 parent ac62498 commit fa89888
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
.externalNativeBuild
.cxx
local.properties
app/release
.project
.settings
.classpath
4 changes: 3 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build
/build
/release
/schemas
22 changes: 20 additions & 2 deletions app/src/main/java/com/jerboa/db/AppDB.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -147,7 +150,6 @@ val MIGRATION_2_3 = object : Migration(2, 3) {
NOT NULL DEFAULT 0)
"""
)
database.execSQL("insert into AppSettings default values")
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa89888

Please sign in to comment.