Skip to content

Commit ca25716

Browse files
committed
fix: database migration 89 needs to be 91 to allow 90 being applied first
This adds safety checks for versions that already applied migration 89.
1 parent 901befc commit ca25716

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

legacy/storage/src/main/java/com/fsck/k9/storage/migrations/MigrationTo89.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fsck.k9.storage.migrations
2+
3+
import android.database.sqlite.SQLiteDatabase
4+
import com.fsck.k9.mailstore.MigrationsHelper
5+
6+
/**
7+
* Migration to version 91. Was planed as version 89 but got skipped, to do 90 first
8+
*
9+
* Adds the `account_id` column to the `folders` and `messages` tables.
10+
*/
11+
internal class MigrationTo91(private val db: SQLiteDatabase, private val migrationsHelper: MigrationsHelper) {
12+
13+
fun columnExists(tableName: String, columnName: String): Boolean {
14+
val cursor = db.rawQuery("PRAGMA table_info($tableName)", null)
15+
cursor.use {
16+
while (it.moveToNext()) {
17+
val currentColumnName = it.getString(it.getColumnIndexOrThrow("name"))
18+
if (currentColumnName == columnName) {
19+
return true
20+
}
21+
}
22+
}
23+
return false
24+
}
25+
26+
fun addAccountIdColumn() {
27+
if (!columnExists("folders", "account_id")) {
28+
db.execSQL("ALTER TABLE folders ADD account_id TEXT")
29+
}
30+
31+
if (!columnExists("messages", "account_id")) {
32+
db.execSQL("ALTER TABLE messages ADD account_id TEXT")
33+
}
34+
35+
val accountUuid = migrationsHelper.account.uuid
36+
db.execSQL("UPDATE messages SET account_id = ?", arrayOf(accountUuid))
37+
db.execSQL("UPDATE folders SET account_id = ?", arrayOf(accountUuid))
38+
39+
db.execSQL("CREATE INDEX IF NOT EXISTS folders_account_id ON folders(account_id)")
40+
db.execSQL("CREATE INDEX IF NOT EXISTS messages_account_id ON messages(account_id)")
41+
}
42+
}

legacy/storage/src/main/java/com/fsck/k9/storage/migrations/Migrations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object Migrations {
3535
if (oldVersion < 86) MigrationTo86(db, migrationsHelper).addFoldersPushEnabledColumn()
3636
if (oldVersion < 87) MigrationTo87(db, migrationsHelper).addFoldersSyncEnabledColumn()
3737
if (oldVersion < 88) MigrationTo88(db, migrationsHelper).addFoldersVisibleColumn()
38-
if (oldVersion < 89) MigrationTo89(db, migrationsHelper).addAccountIdColumn()
3938
if (oldVersion < 90) MigrationTo90(db, migrationsHelper).removeImapPrefixFromFolderServerId()
39+
if (oldVersion < 91) MigrationTo91(db, migrationsHelper).addAccountIdColumn()
4040
}
4141
}

legacy/storage/src/test/java/com/fsck/k9/storage/migrations/MigrationTo89Test.kt renamed to legacy/storage/src/test/java/com/fsck/k9/storage/migrations/MigrationTo91Test.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import org.mockito.kotlin.mock
1818
import org.robolectric.RobolectricTestRunner
1919

2020
@RunWith(RobolectricTestRunner::class)
21-
class MigrationTo89Test {
21+
class MigrationTo91Test {
2222
private val database = createDatabaseVersion88()
2323
private val account = createAccount()
2424
private val migrationHelper = createMigrationsHelper(account)
25-
private val migration = MigrationTo89(database, migrationHelper)
25+
private val migration = MigrationTo91(database, migrationHelper)
2626

2727
@After
2828
fun tearDown() {
@@ -50,6 +50,24 @@ class MigrationTo89Test {
5050
assertThat(messages[0].accountId).isEqualTo(ACCOUNT_UUID)
5151
}
5252

53+
@Test
54+
fun `should not fail if account_id column already exists`() {
55+
// Arrange
56+
migration.addAccountIdColumn()
57+
58+
// Act
59+
migration.addAccountIdColumn()
60+
61+
// Assert
62+
val folders = database.readFolders()
63+
assertThat(folders).isNotNull()
64+
assertThat(folders.size).isEqualTo(0)
65+
66+
val messages = database.readMessages()
67+
assertThat(messages).isNotNull()
68+
assertThat(messages.size).isEqualTo(0)
69+
}
70+
5371
private fun createAccount(): LegacyAccountDto {
5472
return mock {
5573
on { uuid } doReturn ACCOUNT_UUID

0 commit comments

Comments
 (0)