exp : add initial migration from lc14 to lc15

This commit is contained in:
MrSluffy
2025-01-23 13:46:52 +08:00
parent 8630ae61aa
commit 224702a605
3 changed files with 263 additions and 2 deletions

View File

@@ -4,7 +4,9 @@ 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.Migration
import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteDatabase
import app.lawnchair.data.folder.FolderInfoEntity import app.lawnchair.data.folder.FolderInfoEntity
import app.lawnchair.data.folder.FolderItemEntity import app.lawnchair.data.folder.FolderItemEntity
import app.lawnchair.data.folder.service.FolderDao import app.lawnchair.data.folder.service.FolderDao
@@ -15,7 +17,7 @@ import app.lawnchair.data.wallpaper.service.WallpaperDao
import app.lawnchair.util.MainThreadInitializedObject import app.lawnchair.util.MainThreadInitializedObject
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@Database(entities = [IconOverride::class, Wallpaper::class, FolderInfoEntity::class, FolderItemEntity::class], version = 2) @Database(entities = [IconOverride::class, Wallpaper::class, FolderInfoEntity::class, FolderItemEntity::class], version = 3)
@TypeConverters(Converters::class) @TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@@ -36,12 +38,57 @@ abstract class AppDatabase : RoomDatabase() {
} }
companion object { companion object {
val MIGRATION_1_3 = object : Migration(1, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `Wallpapers` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`imagePath` TEXT NOT NULL,
`rank` INTEGER NOT NULL,
`timestamp` INTEGER NOT NULL,
`checksum` TEXT
)
""".trimIndent(),
)
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `Folders` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`title` TEXT NOT NULL,
`hide` INTEGER NOT NULL,
`rank` INTEGER NOT NULL,
`timestamp` INTEGER NOT NULL
)
""".trimIndent(),
)
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `FolderItems` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`folderId` INTEGER NOT NULL,
`rank` INTEGER NOT NULL,
`item_info` TEXT,
`timestamp` INTEGER NOT NULL,
FOREIGN KEY(`folderId`) REFERENCES `Folders`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
""".trimIndent(),
)
database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_FolderItems_folderId` ON `FolderItems` (`folderId`)",
)
}
}
val INSTANCE = MainThreadInitializedObject { context -> val INSTANCE = MainThreadInitializedObject { context ->
Room.databaseBuilder( Room.databaseBuilder(
context, context,
AppDatabase::class.java, AppDatabase::class.java,
"preferences", "preferences",
).fallbackToDestructiveMigration().build() ).addMigrations(MIGRATION_1_3).build()
} }
} }
} }

View File

@@ -3,6 +3,7 @@
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.ForeignKey import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity(tableName = "Folders") @Entity(tableName = "Folders")
@@ -25,6 +26,7 @@ data class FolderInfoEntity(
onUpdate = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE,
), ),
], ],
indices = [Index(value = ["folderId"])],
) )
data class FolderItemEntity( data class FolderItemEntity(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,

View File

@@ -0,0 +1,212 @@
{
"formatVersion": 1,
"database": {
"version": 3,
"identityHash": "eeb34e88cc3be83c58c0c9e73fbd8c31",
"entities": [
{
"tableName": "IconOverride",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`target` TEXT NOT NULL, `packPackageName` TEXT NOT NULL, `drawableName` TEXT NOT NULL, `label` TEXT NOT NULL, `type` TEXT NOT NULL, PRIMARY KEY(`target`))",
"fields": [
{
"fieldPath": "target",
"columnName": "target",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "iconPickerItem.packPackageName",
"columnName": "packPackageName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "iconPickerItem.drawableName",
"columnName": "drawableName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "iconPickerItem.label",
"columnName": "label",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "iconPickerItem.type",
"columnName": "type",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"target"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Wallpapers",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `imagePath` TEXT NOT NULL, `rank` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `checksum` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "imagePath",
"columnName": "imagePath",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "rank",
"columnName": "rank",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "checksum",
"columnName": "checksum",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Folders",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `hide` INTEGER NOT NULL, `rank` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "hide",
"columnName": "hide",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "rank",
"columnName": "rank",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "FolderItems",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `folderId` INTEGER NOT NULL, `rank` INTEGER NOT NULL, `item_info` TEXT, `timestamp` INTEGER NOT NULL, FOREIGN KEY(`folderId`) REFERENCES `Folders`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "folderId",
"columnName": "folderId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "rank",
"columnName": "rank",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "componentKey",
"columnName": "item_info",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_FolderItems_folderId",
"unique": false,
"columnNames": [
"folderId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_FolderItems_folderId` ON `${TABLE_NAME}` (`folderId`)"
}
],
"foreignKeys": [
{
"table": "Folders",
"onDelete": "CASCADE",
"onUpdate": "CASCADE",
"columns": [
"folderId"
],
"referencedColumns": [
"id"
]
}
]
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'eeb34e88cc3be83c58c0c9e73fbd8c31')"
]
}
}