diff --git a/app-backup/src/main/java/net/xzos/upgradeall/app/backup/utils/FileUtils.kt b/app-backup/src/main/java/net/xzos/upgradeall/app/backup/utils/FileUtils.kt index cb2f7128e..7a17c716b 100644 --- a/app-backup/src/main/java/net/xzos/upgradeall/app/backup/utils/FileUtils.kt +++ b/app-backup/src/main/java/net/xzos/upgradeall/app/backup/utils/FileUtils.kt @@ -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() { diff --git a/app/build.gradle b/app/build.gradle index 649d6f215..7e8c1eecd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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}" } diff --git a/core/src/main/java/net/xzos/upgradeall/core/database/MetaDatabase.kt b/core/src/main/java/net/xzos/upgradeall/core/database/MetaDatabase.kt index 45db1a41f..3bed30f31 100644 --- a/core/src/main/java/net/xzos/upgradeall/core/database/MetaDatabase.kt +++ b/core/src/main/java/net/xzos/upgradeall/core/database/MetaDatabase.kt @@ -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 @@ -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], @@ -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 getDatabase(context: Context, less: Class, name: String): E { +fun getDatabaseBuilder( + context: Context, less: Class, name: String +): RoomDatabase.Builder { return Room .databaseBuilder(context, less, name) .addMigrations(MIGRATION_6_7) @@ -46,5 +68,9 @@ fun getDatabase(context: Context, less: Class, name: Strin .addMigrations(MIGRATION_13_14) .addMigrations(MIGRATION_14_15) .addMigrations(MIGRATION_15_16) - .build() -} \ No newline at end of file +} + +fun getDatabase(context: Context, less: Class, name: String) = + getDatabaseBuilder(context, less, name) + .setJournalMode(RoomDatabase.JournalMode.TRUNCATE) + .build() \ No newline at end of file diff --git a/core/src/main/java/net/xzos/upgradeall/core/database/dao/BaseDao.kt b/core/src/main/java/net/xzos/upgradeall/core/database/dao/BaseDao.kt index 130be3e21..6f3704e91 100644 --- a/core/src/main/java/net/xzos/upgradeall/core/database/dao/BaseDao.kt +++ b/core/src/main/java/net/xzos/upgradeall/core/database/dao/BaseDao.kt @@ -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 { @Insert @@ -13,4 +16,7 @@ interface BaseDao { @Delete suspend fun delete(item: E) + + @RawQuery + suspend fun checkpoint(supportSQLiteQuery: SupportSQLiteQuery): Int } \ No newline at end of file