properly handle migration of new fields

This commit is contained in:
tibbi 2022-10-08 22:42:05 +02:00
parent 593b0d575e
commit c4a9a2b14c
3 changed files with 21 additions and 4 deletions

View File

@ -4,12 +4,17 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.simplemobiletools.launcher.helpers.Converters
import com.simplemobiletools.launcher.interfaces.AppLaunchersDao
import com.simplemobiletools.launcher.interfaces.HomeScreenGridItemsDao
import com.simplemobiletools.launcher.models.AppLauncher
import com.simplemobiletools.launcher.models.HomeScreenGridItem
@Database(entities = [AppLauncher::class, HomeScreenGridItem::class], version = 1)
@Database(entities = [AppLauncher::class, HomeScreenGridItem::class], version = 2)
@TypeConverters(Converters::class)
abstract class AppsDatabase : RoomDatabase() {
abstract fun AppLaunchersDao(): AppLaunchersDao
@ -24,11 +29,19 @@ abstract class AppsDatabase : RoomDatabase() {
synchronized(AppsDatabase::class) {
if (db == null) {
db = Room.databaseBuilder(context.applicationContext, AppsDatabase::class.java, "apps.db")
.addMigrations(MIGRATION_1_2)
.build()
}
}
}
return db!!
}
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE home_screen_grid_items ADD COLUMN intent TEXT default '' NOT NULL")
database.execSQL("ALTER TABLE home_screen_grid_items ADD COLUMN icon BLOB")
}
}
}
}

View File

@ -7,8 +7,12 @@ import java.io.ByteArrayOutputStream
class Converters {
@TypeConverter
fun toBitmap(bytes: ByteArray): Bitmap? {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
fun toBitmap(bytes: ByteArray?): Bitmap? {
return if (bytes == null) {
null
} else {
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}
}
@TypeConverter

View File

@ -22,7 +22,7 @@ data class HomeScreenGridItem(
@ColumnInfo(name = "class_name") var className: String,
@ColumnInfo(name = "widget_id") var widgetId: Int,
@ColumnInfo(name = "intent") var intent: String, // used at shortcuts on click
@ColumnInfo(name = "icon") var icon: Bitmap?, // store only images of shortcuts, those cannot be retrieved anytime
@ColumnInfo(name = "icon") var icon: Bitmap? = null, // store only images of shortcuts, those cannot be retrieved anytime
@Ignore var drawable: Drawable? = null,
@Ignore var providerInfo: AppWidgetProviderInfo? = null, // used at widgets