Add: AutoMigration of DB adding the new repos

This commit is contained in:
machiav3lli 2022-07-09 23:32:12 +02:00
parent 20b99682fe
commit 2ace49469e
3 changed files with 38 additions and 4 deletions

View File

@ -33,6 +33,10 @@ android {
"room.incremental" to "true" "room.incremental" to "true"
) )
) )
ksp {
arg("room.schemaLocation", "$projectDir/schemas")
arg("room.incremental", "true")
}
} }
} }
} }

View File

@ -1,10 +1,13 @@
package com.machiav3lli.fdroid.database package com.machiav3lli.fdroid.database
import android.content.Context import android.content.Context
import androidx.room.AutoMigration
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters import androidx.room.TypeConverters
import androidx.room.migration.AutoMigrationSpec
import androidx.sqlite.db.SupportSQLiteDatabase
import com.machiav3lli.fdroid.database.dao.CategoryDao import com.machiav3lli.fdroid.database.dao.CategoryDao
import com.machiav3lli.fdroid.database.dao.CategoryTempDao import com.machiav3lli.fdroid.database.dao.CategoryTempDao
import com.machiav3lli.fdroid.database.dao.ExtrasDao import com.machiav3lli.fdroid.database.dao.ExtrasDao
@ -21,6 +24,7 @@ import com.machiav3lli.fdroid.database.entity.Product
import com.machiav3lli.fdroid.database.entity.ProductTemp import com.machiav3lli.fdroid.database.entity.ProductTemp
import com.machiav3lli.fdroid.database.entity.Release import com.machiav3lli.fdroid.database.entity.Release
import com.machiav3lli.fdroid.database.entity.Repository import com.machiav3lli.fdroid.database.entity.Repository
import com.machiav3lli.fdroid.database.entity.Repository.Companion.addedReposV9
import com.machiav3lli.fdroid.database.entity.Repository.Companion.defaultRepositories import com.machiav3lli.fdroid.database.entity.Repository.Companion.defaultRepositories
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@ -36,7 +40,14 @@ import kotlinx.coroutines.launch
CategoryTemp::class, CategoryTemp::class,
Installed::class, Installed::class,
Extras::class Extras::class
], version = 8 ],
version = 9,
exportSchema = true,
autoMigrations = [AutoMigration(
from = 8,
to = 9,
spec = DatabaseX.Companion.MigrationSpec8to9::class
)]
) )
@TypeConverters(Converters::class) @TypeConverters(Converters::class)
abstract class DatabaseX : RoomDatabase() { abstract class DatabaseX : RoomDatabase() {
@ -75,19 +86,32 @@ abstract class DatabaseX : RoomDatabase() {
return INSTANCE!! return INSTANCE!!
} }
} }
class MigrationSpec8to9 : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
super.onPostMigrate(db)
val preRepos = if (db.version < 9) addedReposV9
else emptyList()
GlobalScope.launch(Dispatchers.IO) {
preRepos.forEach {
INSTANCE?.repositoryDao?.put(it)
}
}
}
}
} }
fun cleanUp(pairs: Set<Pair<Long, Boolean>>) { fun cleanUp(pairs: Set<Pair<Long, Boolean>>) {
runInTransaction { runInTransaction {
pairs.windowed(10, 10, true).map { pairs.windowed(10, 10, true).map {
it.map { it.first } it.map { pair -> pair.first }
.toLongArray() .toLongArray()
.forEach { id -> .forEach { id ->
productDao.deleteById(id) productDao.deleteById(id)
categoryDao.deleteById(id) categoryDao.deleteById(id)
} }
it.filter { it.second } it.filter { pair -> pair.second }
.map { it.first } .map { pair -> pair.first }
.toLongArray() .toLongArray()
.forEach { id -> repositoryDao.deleteById(id) } .forEach { id -> repositoryDao.deleteById(id) }
} }

View File

@ -306,5 +306,11 @@ data class Repository(
FROSTNERD, FROSTNERD_ARCHIVE, FROSTNERD, FROSTNERD_ARCHIVE,
UNOFFICIAL_FIREFOX, PATCHED, WIND, UMBRELLA, ALEFVANOON UNOFFICIAL_FIREFOX, PATCHED, WIND, UMBRELLA, ALEFVANOON
) )
val addedReposV9 = listOf(
FLUFFY_CHAT, SIMPLEX_CHAT, I2P,
ELEMENT_DEV_FDROID, ELEMENT_DEV_GPLAY,
FROSTNERD, FROSTNERD_ARCHIVE
)
} }
} }