Skip to content

Commit

Permalink
fix: bad room migration crash
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 28, 2024
1 parent 7c4ed0f commit e6097b1
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions app/src/main/java/com/bnyro/clock/data/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import com.bnyro.clock.data.database.dao.Converters
import com.bnyro.clock.data.database.dao.TimeZonesDao
import com.bnyro.clock.domain.model.Alarm
import com.bnyro.clock.domain.model.TimeZone
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Database(
entities = [TimeZone::class, Alarm::class],
Expand Down Expand Up @@ -47,7 +44,6 @@ abstract class AppDatabase : RoomDatabase() {
private var INSTANCE: AppDatabase? = null

private const val dbName = "com.bnyro.clock"
lateinit var instance: AppDatabase

private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
Expand All @@ -72,20 +68,8 @@ abstract class AppDatabase : RoomDatabase() {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE timeZones RENAME TO temp_table")
db.execSQL("CREATE TABLE IF NOT EXISTS `timeZones` (`zoneId` TEXT NOT NULL, `zoneName` TEXT NOT NULL, `countryName` TEXT NOT NULL, `offset` INTEGER NOT NULL, `key` TEXT NOT NULL, PRIMARY KEY(`key`))")
db.execSQL("INSERT INTO timeZones (key, zoneId, offset, zoneName, countryName) SELECT name, name, offset, displayName, countryName FROM temp_table")
db.execSQL("INSERT INTO timeZones (key, zoneId, offset, zoneName, countryName) SELECT name || ',' || displayName || ',' || countryName, name, offset, displayName, countryName FROM temp_table")
db.execSQL("DROP TABLE temp_table")

postMigrate7to8()
}
}

private fun postMigrate7to8() {
CoroutineScope(Dispatchers.IO).launch {
val zones = instance.timeZonesDao().getAll().map {
it.copy(key = arrayOf(it.zoneId, it.zoneName, it.countryName).joinToString(","))
}
instance.timeZonesDao().clear()
instance.timeZonesDao().insertAll(*zones.toTypedArray())
}
}

Expand Down

0 comments on commit e6097b1

Please sign in to comment.