Fix migration

pull/1935/head
Alex Baker 2 years ago
parent 57ca2f013a
commit 4d8a058bfb

@ -20,7 +20,7 @@ import org.tasks.R
import org.tasks.caldav.VtodoCache
import org.tasks.data.*
import org.tasks.data.Place.Companion.newPlace
import org.tasks.db.Migrations.isRepeatAfterCompletion
import org.tasks.db.Migrations.repeatFrom
import org.tasks.db.Migrations.withoutFrom
import org.tasks.preferences.Preferences
import timber.log.Timber
@ -157,11 +157,7 @@ class TasksJsonImporter @Inject constructor(
taskDao.save(task)
}
if (version < V12_8) {
task.repeatFrom = if (task.recurrence.isRepeatAfterCompletion()) {
Task.RepeatFrom.COMPLETION_DATE
} else {
Task.RepeatFrom.DUE_DATE
}
task.repeatFrom = task.recurrence.repeatFrom()
task.recurrence = task.recurrence.withoutFrom()
}
for (comment in backup.comments) {

@ -17,6 +17,7 @@ import org.tasks.data.Alarm.Companion.TYPE_REL_START
import org.tasks.data.Alarm.Companion.TYPE_SNOOZE
import org.tasks.data.CaldavAccount.Companion.SERVER_UNKNOWN
import org.tasks.data.OpenTaskDao.Companion.getLong
import org.tasks.extensions.getLongOrNull
import org.tasks.extensions.getString
import org.tasks.repeats.RecurrenceUtils.newRecur
import org.tasks.time.DateTime
@ -538,20 +539,17 @@ object Migrations {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE `tasks` ADD COLUMN `repeat_from` INTEGER NOT NULL DEFAULT ${Task.RepeatFrom.DUE_DATE}")
database
.query("SELECT `_id`, `repeatUntil`, `recurrence` FROM `tasks` WHERE `repeatUntil` > 0")
.use {
while (it.moveToNext()) {
val id = it.getLong("_id")
val repeatUntil = it.getLong("repeatUntil")
val recurrence = it.getString("recurrence") ?: continue
val recur = newRecur(recurrence.withoutFrom()!!).apply {
until = DateTime(repeatUntil).toDate()
}
val repeatFrom = if (recurrence.isRepeatAfterCompletion()) {
Task.RepeatFrom.COMPLETION_DATE
} else {
Task.RepeatFrom.DUE_DATE
}
.query("SELECT `_id`, `repeatUntil`, `recurrence` FROM `tasks` WHERE `recurrence` IS NOT NULL AND `recurrence` != ''")
.use { cursor ->
while (cursor.moveToNext()) {
val id = cursor.getLong("_id")
val recurrence =
cursor.getString("recurrence")?.takeIf { it.isNotBlank() } ?: continue
val recur = newRecur(recurrence.withoutFrom()!!)
cursor.getLongOrNull("repeatUntil")
?.takeIf { it > 0 }
?.let { recur.until = DateTime(it).toDate() }
val repeatFrom = recurrence.repeatFrom()
database.execSQL("UPDATE `tasks` SET `repeat_from` = $repeatFrom, `recurrence` = '$recur' WHERE `_id` = $id")
}
}
@ -611,7 +609,11 @@ object Migrations {
override fun migrate(database: SupportSQLiteDatabase) {}
}
fun String?.isRepeatAfterCompletion() = this?.contains("FROM=COMPLETION") ?: false
fun String?.repeatFrom() = if (this?.contains("FROM=COMPLETION") == true) {
Task.RepeatFrom.COMPLETION_DATE
} else {
Task.RepeatFrom.DUE_DATE
}
fun String?.withoutFrom(): String? = this?.replace(";?FROM=[^;]*".toRegex(), "")
}

@ -1,6 +1,10 @@
package org.tasks.extensions
import android.database.Cursor
import androidx.core.database.getLongOrNull
fun Cursor.getString(columnName: String): String? =
getColumnIndex(columnName).takeIf { it >= 0 }?.let { getString(it) }
fun Cursor.getLongOrNull(columnName: String): Long? =
getColumnIndex(columnName).takeIf { it >= 0 }?.let { getLongOrNull(it) }
Loading…
Cancel
Save