Fix alarm migration issues

pull/1790/head
Alex Baker 4 years ago
parent 488836f848
commit ec3150f455

@ -15,15 +15,16 @@ class Upgrade_12_4 @Inject constructor(
private val upgraderDao: UpgraderDao, private val upgraderDao: UpgraderDao,
) { ) {
internal suspend fun syncExistingAlarms() { internal suspend fun syncExistingAlarms() {
val existingAlarms = alarmDao.getActiveAlarms().map { it.task } val existingAlarms = alarmDao.getActiveAlarms()
upgraderDao.tasksWithVtodos().forEach { caldav -> upgraderDao.tasksWithVtodos().forEach { caldav ->
val remoteTask = caldav.vtodo?.let(iCalendar::fromVtodo) ?: return@forEach val remoteTask = caldav.vtodo?.let(iCalendar::fromVtodo) ?: return@forEach
remoteTask remoteTask
.reminders .reminders
.onEach { alarm -> alarm.task = caldav.id } .filter { existingAlarms.find { e -> e.task == caldav.id && e.same(it) } == null }
.let { alarms -> alarmDao.insert(alarms) } .onEach { it.task = caldav.id }
.let { alarmDao.insert(it) }
} }
taskDao.touch(existingAlarms) taskDao.touch(existingAlarms.map { it.task }.toSet().toList())
} }
companion object { companion object {

@ -70,6 +70,12 @@ class Alarm : Parcelable {
return "Alarm(id=$id, task=$task, time=$timestamp, type=$type, repeat=$repeat, interval=$interval)" return "Alarm(id=$id, task=$task, time=$timestamp, type=$type, repeat=$repeat, interval=$interval)"
} }
fun same(other: Alarm) =
type == other.type &&
time == other.time &&
repeat == other.repeat &&
interval == other.interval
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (javaClass != other?.javaClass) return false if (javaClass != other?.javaClass) return false

@ -420,13 +420,13 @@ object Migrations {
database.execSQL("ALTER TABLE `alarms` ADD COLUMN `repeat` INTEGER NOT NULL DEFAULT 0") database.execSQL("ALTER TABLE `alarms` ADD COLUMN `repeat` INTEGER NOT NULL DEFAULT 0")
database.execSQL("ALTER TABLE `alarms` ADD COLUMN `interval` INTEGER NOT NULL DEFAULT 0") database.execSQL("ALTER TABLE `alarms` ADD COLUMN `interval` INTEGER NOT NULL DEFAULT 0")
database.execSQL( database.execSQL(
"INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_START FROM `tasks` WHERE `hideUntil` > 0 AND `notificationFlags` | $NOTIFY_AT_START" "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_START FROM `tasks` WHERE `hideUntil` > 0 AND `notificationFlags` & $NOTIFY_AT_START = $NOTIFY_AT_START"
) )
database.execSQL( database.execSQL(
"INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_END FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` | $NOTIFY_AT_DEADLINE" "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, 0, $TYPE_REL_END FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` & $NOTIFY_AT_DEADLINE = $NOTIFY_AT_DEADLINE"
) )
database.execSQL( database.execSQL(
"INSERT INTO `alarms` (`task`, `time`, `type`, `repeat`, `interval`) SELECT `_id`, ${HOURS.toMillis(24)}, $TYPE_REL_END, 6, ${HOURS.toMillis(24)} FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` | $NOTIFY_AFTER_DEADLINE" "INSERT INTO `alarms` (`task`, `time`, `type`, `repeat`, `interval`) SELECT `_id`, ${HOURS.toMillis(24)}, $TYPE_REL_END, 6, ${HOURS.toMillis(24)} FROM `tasks` WHERE `dueDate` > 0 AND `notificationFlags` & $NOTIFY_AFTER_DEADLINE = $NOTIFY_AFTER_DEADLINE"
) )
database.execSQL( database.execSQL(
"INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, `notifications`, $TYPE_RANDOM FROM `tasks` WHERE `notifications` > 0" "INSERT INTO `alarms` (`task`, `time`, `type`) SELECT `_id`, `notifications`, $TYPE_RANDOM FROM `tasks` WHERE `notifications` > 0"

Loading…
Cancel
Save