From d7a769a0c135c7d00af41753d0b4eb819e562eb5 Mon Sep 17 00:00:00 2001 From: johan12345 Date: Sun, 6 Feb 2022 20:30:08 +0100 Subject: [PATCH] use ForeignKey.NO_ACTION for Favorites (conflicts with REPLACE during insert) --- .../vonforst/evmap/model/FavoritesModel.kt | 2 +- .../net/vonforst/evmap/storage/Database.kt | 23 +++++++++++++++++-- .../vonforst/evmap/storage/FavoritesDao.kt | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/vonforst/evmap/model/FavoritesModel.kt b/app/src/main/java/net/vonforst/evmap/model/FavoritesModel.kt index 33da35da..c8b860ec 100644 --- a/app/src/main/java/net/vonforst/evmap/model/FavoritesModel.kt +++ b/app/src/main/java/net/vonforst/evmap/model/FavoritesModel.kt @@ -8,7 +8,7 @@ import androidx.room.* entity = ChargeLocation::class, parentColumns = arrayOf("id", "dataSource"), childColumns = arrayOf("chargerId", "chargerDataSource"), - onDelete = ForeignKey.RESTRICT, + onDelete = ForeignKey.NO_ACTION, ) ], indices = [ diff --git a/app/src/main/java/net/vonforst/evmap/storage/Database.kt b/app/src/main/java/net/vonforst/evmap/storage/Database.kt index eb646eb2..8fae8a12 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/Database.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/Database.kt @@ -32,7 +32,7 @@ import net.vonforst.evmap.model.* OCMConnectionType::class, OCMCountry::class, OCMOperator::class - ], version = 17 + ], version = 18 ) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { @@ -56,7 +56,7 @@ abstract class AppDatabase : RoomDatabase() { MIGRATION_2, MIGRATION_3, MIGRATION_4, MIGRATION_5, MIGRATION_6, MIGRATION_7, MIGRATION_8, MIGRATION_9, MIGRATION_10, MIGRATION_11, MIGRATION_12, MIGRATION_13, MIGRATION_14, MIGRATION_15, MIGRATION_16, - MIGRATION_17 + MIGRATION_17, MIGRATION_18 ) .addCallback(object : Callback() { override fun onCreate(db: SupportSQLiteDatabase) { @@ -356,5 +356,24 @@ abstract class AppDatabase : RoomDatabase() { db.execSQL("CREATE INDEX IF NOT EXISTS `index_SliderFilterValue_profile_dataSource` ON `SliderFilterValue` (`profile`, `dataSource`)") } } + + private val MIGRATION_18 = object : Migration(17, 18) { + override fun migrate(db: SupportSQLiteDatabase) { + try { + db.beginTransaction() + db.execSQL("CREATE TABLE IF NOT EXISTS `FavoriteNew` (`favoriteId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `chargerId` INTEGER NOT NULL, `chargerDataSource` TEXT NOT NULL, FOREIGN KEY(`chargerId`, `chargerDataSource`) REFERENCES `ChargeLocation`(`id`, `dataSource`) ON UPDATE NO ACTION ON DELETE NO ACTION )"); + val columnList = + "`favoriteId`,`chargerId`,`chargerDataSource`" + db.execSQL("INSERT INTO `FavoriteNew`($columnList) SELECT $columnList FROM `Favorite`") + db.execSQL("DROP TABLE `Favorite`") + db.execSQL("ALTER TABLE `FavoriteNew` RENAME TO `Favorite`") + + db.setTransactionSuccessful() + } finally { + db.endTransaction() + } + } + + } } } \ No newline at end of file diff --git a/app/src/main/java/net/vonforst/evmap/storage/FavoritesDao.kt b/app/src/main/java/net/vonforst/evmap/storage/FavoritesDao.kt index e2c2c963..7a691191 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/FavoritesDao.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/FavoritesDao.kt @@ -8,7 +8,7 @@ import net.vonforst.evmap.model.FavoriteWithDetail @Dao interface FavoritesDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun insert(vararg favorites: Favorite) + suspend fun insert(vararg favorites: Favorite): List @Delete suspend fun delete(vararg favorites: Favorite)