Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit e6097b1

Browse files
committed
fix: bad room migration crash
1 parent 7c4ed0f commit e6097b1

File tree

1 file changed

+1
-17
lines changed

1 file changed

+1
-17
lines changed

app/src/main/java/com/bnyro/clock/data/database/AppDatabase.kt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import com.bnyro.clock.data.database.dao.Converters
1515
import com.bnyro.clock.data.database.dao.TimeZonesDao
1616
import com.bnyro.clock.domain.model.Alarm
1717
import com.bnyro.clock.domain.model.TimeZone
18-
import kotlinx.coroutines.CoroutineScope
19-
import kotlinx.coroutines.Dispatchers
20-
import kotlinx.coroutines.launch
2118

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

4946
private const val dbName = "com.bnyro.clock"
50-
lateinit var instance: AppDatabase
5147

5248
private val MIGRATION_1_2 = object : Migration(1, 2) {
5349
override fun migrate(db: SupportSQLiteDatabase) {
@@ -72,20 +68,8 @@ abstract class AppDatabase : RoomDatabase() {
7268
override fun migrate(db: SupportSQLiteDatabase) {
7369
db.execSQL("ALTER TABLE timeZones RENAME TO temp_table")
7470
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`))")
75-
db.execSQL("INSERT INTO timeZones (key, zoneId, offset, zoneName, countryName) SELECT name, name, offset, displayName, countryName FROM temp_table")
71+
db.execSQL("INSERT INTO timeZones (key, zoneId, offset, zoneName, countryName) SELECT name || ',' || displayName || ',' || countryName, name, offset, displayName, countryName FROM temp_table")
7672
db.execSQL("DROP TABLE temp_table")
77-
78-
postMigrate7to8()
79-
}
80-
}
81-
82-
private fun postMigrate7to8() {
83-
CoroutineScope(Dispatchers.IO).launch {
84-
val zones = instance.timeZonesDao().getAll().map {
85-
it.copy(key = arrayOf(it.zoneId, it.zoneName, it.countryName).joinToString(","))
86-
}
87-
instance.timeZonesDao().clear()
88-
instance.timeZonesDao().insertAll(*zones.toTypedArray())
8973
}
9074
}
9175

0 commit comments

Comments
 (0)