use ForeignKey.NO_ACTION for Favorites

(conflicts with REPLACE during insert)
This commit is contained in:
johan12345
2022-02-06 20:30:08 +01:00
parent 0f1d98bd61
commit d7a769a0c1
3 changed files with 23 additions and 4 deletions

View File

@@ -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 = [

View File

@@ -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()
}
}
}
}
}

View File

@@ -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<Long>
@Delete
suspend fun delete(vararg favorites: Favorite)