Skip to content

Commit

Permalink
fix(core/database): change journal mode to truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
xz-dev committed Jul 11, 2022
1 parent 0ac29b9 commit a1a4ed9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ fun getBackupMetaDatabase(dbFileBytes: ByteArray): MetaDatabase {

private fun deleteTmpDB() {
tmpDBFile.delete()
File(tmpDBFile.name + "-shm").delete()
File(tmpDBFile.name + "-wal").delete()
}

fun delBackupTmp() {
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
applicationId "net.xzos.upgradeall"
minSdkVersion 21
targetSdkVersion 33
versionCode 100
versionName "0.13-alpha.4"
versionCode 101
versionName "0.13-alpha.5"
if (project.hasProperty('appVerName')) {
versionName = "${versionName}_${appVerName}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SimpleSQLiteQuery
import kotlinx.coroutines.runBlocking
import net.xzos.upgradeall.core.database.dao.AppDao
import net.xzos.upgradeall.core.database.dao.ExtraAppDao
import net.xzos.upgradeall.core.database.dao.ExtraHubDao
Expand All @@ -14,6 +16,7 @@ import net.xzos.upgradeall.core.database.table.AppEntity
import net.xzos.upgradeall.core.database.table.HubEntity
import net.xzos.upgradeall.core.database.table.extra_app.ExtraAppEntity
import net.xzos.upgradeall.core.database.table.extra_hub.ExtraHubEntity
import java.io.File

@Database(
entities = [AppEntity::class, HubEntity::class, ExtraAppEntity::class, ExtraHubEntity::class],
Expand All @@ -27,12 +30,31 @@ abstract class MetaDatabase : RoomDatabase() {
abstract fun extraHubDao(): ExtraHubDao
}

suspend fun MetaDatabase.checkpoint() {
val query = SimpleSQLiteQuery("pragma wal_checkpoint(full)")
appDao().checkpoint(query)
hubDao().checkpoint(query)
extraAppDao().checkpoint(query)
extraHubDao().checkpoint(query)
}

lateinit var metaDatabase: MetaDatabase
fun initDatabase(context: Context) {
metaDatabase = getDatabase(context, MetaDatabase::class.java, "app_metadata_database.db")
val dbName = "app_metadata_database.db"
val dbFilePath = context.getDatabasePath(dbName).path
if (File("$dbFilePath-wal").exists() || File("$dbFilePath-shm").exists())
getDatabaseBuilder(
context, MetaDatabase::class.java, dbName
).build().apply {
runBlocking { this@apply.checkpoint() }
this.close()
}
metaDatabase = getDatabase(context, MetaDatabase::class.java, dbName)
}

fun <E : RoomDatabase> getDatabase(context: Context, less: Class<E>, name: String): E {
fun <E : RoomDatabase> getDatabaseBuilder(
context: Context, less: Class<E>, name: String
): RoomDatabase.Builder<E> {
return Room
.databaseBuilder(context, less, name)
.addMigrations(MIGRATION_6_7)
Expand All @@ -46,5 +68,9 @@ fun <E : RoomDatabase> getDatabase(context: Context, less: Class<E>, name: Strin
.addMigrations(MIGRATION_13_14)
.addMigrations(MIGRATION_14_15)
.addMigrations(MIGRATION_15_16)
.build()
}
}

fun <E : RoomDatabase> getDatabase(context: Context, less: Class<E>, name: String) =
getDatabaseBuilder(context, less, name)
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE)
.build()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package net.xzos.upgradeall.core.database.dao

import androidx.room.Delete
import androidx.room.Insert
import androidx.room.RawQuery
import androidx.room.Update
import androidx.sqlite.db.SupportSQLiteQuery


interface BaseDao<E> {
@Insert
Expand All @@ -13,4 +16,7 @@ interface BaseDao<E> {

@Delete
suspend fun delete(item: E)

@RawQuery
suspend fun checkpoint(supportSQLiteQuery: SupportSQLiteQuery): Int
}

0 comments on commit a1a4ed9

Please sign in to comment.