Update: Migrate temporary tables (4.5/5 in replacing SQLite with Room)

This commit is contained in:
machiav3lli 2021-10-29 23:30:12 +02:00
parent a59bd2bd8a
commit 6aab888c0f
4 changed files with 88 additions and 12 deletions

View File

@ -177,4 +177,56 @@ interface InstalledDao : BaseDao<Installed> {
interface LockDao : BaseDao<Lock> {
@Query("DELETE FROM 'memory.lock' WHERE package_name = :packageName")
fun delete(packageName: String)
}
@Dao
interface ProductTempDao : BaseDao<ProductTemp> {
@get:Query("SELECT * FROM `product.temporary`")
val all: Array<ProductTemp>
@Query("DELETE FROM `product.temporary`")
fun emptyTable()
@Insert
fun insertCategory(vararg product: CategoryTemp)
@Transaction
fun putTemporary(products: List<com.looker.droidify.entity.Product>) {
products.forEach {
val signatures = it.signatures.joinToString { ".$it" }
.let { if (it.isNotEmpty()) "$it." else "" }
insert(it.let {
ProductTemp().apply {
repository_id = it.repositoryId
package_name = it.packageName
name = it.name
summary = it.summary
description = it.description
added = it.added
updated = it.updated
version_code = it.versionCode
this.signatures = signatures
compatible = if (it.compatible) 1 else 0
data = it
data_item = it.item()
}
})
it.categories.forEach { category ->
insertCategory(CategoryTemp().apply {
repository_id = it.repositoryId
package_name = it.packageName
name = category
})
}
}
}
}
@Dao
interface CategoryTempDao : BaseDao<CategoryTemp> {
@get:Query("SELECT * FROM `category.temporary`")
val all: Array<CategoryTemp>
@Query("DELETE FROM `category.temporary`")
fun emptyTable()
}

View File

@ -761,7 +761,7 @@ object Database {
}
}
// TODO add temporary tables
// Done
object UpdaterAdapter {
private val Table.temporaryName: String
get() = "${name}_temporary"
@ -774,6 +774,7 @@ object Database {
db.execSQL(Schema.Category.formatCreateTable(Schema.Category.temporaryName))
}
// Done
fun putTemporary(products: List<Product>) {
db.beginTransaction()
try {
@ -812,6 +813,7 @@ object Database {
}
}
// Done
fun finishTemporary(repository: Repository, success: Boolean) {
if (success) {
db.beginTransaction()

View File

@ -1,16 +1,16 @@
package com.looker.droidify.database
import android.content.Context
import androidx.room.*
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
@Database(
entities = [
Repository::class,
Product::class,
ProductTemp::class,
Category::class,
CategoryTemp::class,
Installed::class,
Lock::class
], version = 1
@ -19,7 +19,9 @@ import androidx.room.TypeConverters
abstract class DatabaseX : RoomDatabase() {
abstract val repositoryDao: RepositoryDao
abstract val productDao: ProductDao
abstract val productTempDao: ProductTempDao
abstract val categoryDao: CategoryDao
abstract val categoryTempDao: CategoryTempDao
abstract val installedDao: InstalledDao
abstract val lockDao: LockDao
@ -45,6 +47,7 @@ abstract class DatabaseX : RoomDatabase() {
}
}
@Transaction
fun cleanUp(pairs: Set<Pair<Long, Boolean>>) {
val result = pairs.windowed(10, 10, true).map {
val ids = it.map { it.first }.toLongArray()
@ -59,4 +62,17 @@ abstract class DatabaseX : RoomDatabase() {
com.looker.droidify.database.Database.notifyChanged(com.looker.droidify.database.Database.Subject.Products)
}*/
}
@Transaction
fun finishTemporary(repository: com.looker.droidify.entity.Repository, success: Boolean) {
if (success) {
productDao.deleteById(repository.id)
categoryDao.deleteById(repository.id)
productDao.insert(*(productTempDao.all))
categoryDao.insert(*(categoryTempDao.all))
repositoryDao.put(repository)
}
productTempDao.emptyTable()
categoryTempDao.emptyTable()
}
}

View File

@ -6,7 +6,6 @@ import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import com.looker.droidify.database.Database.jsonGenerate
import com.looker.droidify.database.Database.jsonParse
import com.looker.droidify.entity.Product
import com.looker.droidify.entity.ProductItem
import com.looker.droidify.entity.Repository
@ -32,25 +31,28 @@ class Repository {
@Entity(tableName = "product", primaryKeys = ["repository_id", "package_name"])
open class Product {
var repository_id: Long = 0
var repository_id = 0L
var package_name = ""
var name = ""
var summary = ""
var description = ""
var added = 0
var updated = 0
var version_code = 0
var added = 0L
var updated = 0L
var version_code = 0L
var signatures = ""
var compatible = 0
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
var data: Product? = null
var data: com.looker.droidify.entity.Product? = null
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
var data_item: ProductItem? = null
}
@Entity(tableName = "product.temporary")
class ProductTemp : Product()
@Entity(tableName = "category", primaryKeys = ["repository_id", "package_name", "name"])
open class Category {
var repository_id: Long = 0
@ -58,6 +60,9 @@ open class Category {
var name = ""
}
@Entity(tableName = "category.temporary")
class CategoryTemp : Category()
@Entity(tableName = "memory.installed")
class Installed {
@PrimaryKey
@ -87,11 +92,12 @@ object Converters {
@TypeConverter
@JvmStatic
fun toProduct(byteArray: ByteArray) = byteArray.jsonParse { Product.deserialize(it) }
fun toProduct(byteArray: ByteArray) =
byteArray.jsonParse { com.looker.droidify.entity.Product.deserialize(it) }
@TypeConverter
@JvmStatic
fun toByteArray(product: Product) = jsonGenerate(product::serialize)
fun toByteArray(product: com.looker.droidify.entity.Product) = jsonGenerate(product::serialize)
@TypeConverter
@JvmStatic