Skip to content

Commit 2c9f07a

Browse files
Merge pull request #9827 from rafaeltonholo/uplift/beta/9826/fix-db-migration-crash
uplift(beta): fix db migration 90 and drawer app crashes
2 parents d7c898a + be298f2 commit 2c9f07a

File tree

5 files changed

+547
-28
lines changed

5 files changed

+547
-28
lines changed

feature/navigation/drawer/dropdown/src/main/kotlin/net/thunderbird/feature/navigation/drawer/dropdown/domain/usecase/GetDisplayTreeFolder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package net.thunderbird.feature.navigation.drawer.dropdown.domain.usecase
33
import kotlinx.collections.immutable.persistentListOf
44
import kotlinx.collections.immutable.toImmutableList
55
import net.thunderbird.core.logging.Logger
6+
import net.thunderbird.feature.mail.folder.api.FOLDER_DEFAULT_PATH_DELIMITER
67
import net.thunderbird.feature.mail.folder.api.Folder
78
import net.thunderbird.feature.mail.folder.api.FolderPathDelimiter
89
import net.thunderbird.feature.mail.folder.api.FolderType
@@ -28,7 +29,7 @@ internal class GetDisplayTreeFolder(
2829
)
2930
}
3031

31-
val pathDelimiter = folders.first().pathDelimiter
32+
val pathDelimiter = folders.firstOrNull()?.pathDelimiter ?: FOLDER_DEFAULT_PATH_DELIMITER
3233
val accountFolders = folders.filterIsInstance<MailDisplayFolder>().map {
3334
val path = flattenPath(it.folder.name, pathDelimiter, maxDepth)
3435
logger.debug { "Flattened path for ${it.folder.name}$path" }

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fsck.k9.storage.migrations
22

33
import android.database.sqlite.SQLiteDatabase
4+
import androidx.annotation.VisibleForTesting
45
import com.fsck.k9.mail.AuthType
56
import com.fsck.k9.mail.AuthenticationFailedException
67
import com.fsck.k9.mail.ServerSettings
@@ -10,6 +11,7 @@ import com.fsck.k9.mail.ssl.TrustedSocketFactory
1011
import com.fsck.k9.mail.store.imap.ImapClientInfo
1112
import com.fsck.k9.mail.store.imap.ImapStore
1213
import com.fsck.k9.mail.store.imap.ImapStoreConfig
14+
import com.fsck.k9.mail.store.imap.ImapStoreFactory
1315
import com.fsck.k9.mail.store.imap.ImapStoreSettings
1416
import com.fsck.k9.mail.store.imap.ImapStoreSettings.autoDetectNamespace
1517
import com.fsck.k9.mail.store.imap.ImapStoreSettings.pathPrefix
@@ -19,6 +21,8 @@ import net.thunderbird.core.android.account.LegacyAccount
1921
import net.thunderbird.core.common.mail.Protocols
2022
import net.thunderbird.core.logging.Logger
2123
import net.thunderbird.core.logging.legacy.Log
24+
import okio.IOException
25+
import org.intellij.lang.annotations.Language
2226
import org.koin.core.component.KoinComponent
2327
import org.koin.core.component.inject
2428
import org.koin.core.qualifier.named
@@ -29,6 +33,7 @@ internal class MigrationTo90(
2933
private val db: SQLiteDatabase,
3034
private val migrationsHelper: MigrationsHelper,
3135
private val logger: Logger = Log,
36+
private val imapStoreFactory: ImapStoreFactory = ImapStore.Companion,
3237
) : KoinComponent {
3338
private val trustedSocketFactory: TrustedSocketFactory by inject()
3439
private val clientInfoAppName: String by inject(named("ClientInfoAppName"))
@@ -44,35 +49,33 @@ internal class MigrationTo90(
4449
return
4550
}
4651

47-
logger.verbose(TAG) { "started db migration to version 107 to account ${account.uuid}" }
52+
logger.verbose(TAG) { "started db migration to version 90 to account ${account.uuid}" }
4853

4954
val imapStore = createImapStore(account)
5055

5156
try {
5257
logger.verbose(TAG) { "fetching IMAP prefix" }
5358
imapStore.fetchImapPrefix()
54-
} catch (e: AuthenticationFailedException) {
55-
logger.warn(TAG, e) { "failed to fetch IMAP prefix." }
56-
return
57-
}
58-
59-
val imapPrefix = imapStore.combinedPrefix
60-
61-
if (imapPrefix?.isNotBlank() == true) {
62-
logger.verbose(TAG) { "Imap Prefix ($imapPrefix) detected, updating folder's server_id" }
63-
val query = """
64-
|UPDATE folders
65-
| SET server_id = REPLACE(server_id, '$imapPrefix', '')
66-
|WHERE
67-
| server_id LIKE '$imapPrefix%'
68-
""".trimMargin()
59+
val imapPrefix = imapStore.combinedPrefix
60+
61+
if (imapPrefix?.isNotBlank() == true) {
62+
logger.verbose(TAG) { "Imap Prefix ($imapPrefix) detected, updating folder's server_id" }
63+
val query = buildQuery(imapPrefix)
64+
db.execSQL(query)
65+
} else {
66+
logger.verbose(TAG) { "No Imap Prefix detected, skipping db migration" }
67+
}
6968

70-
db.execSQL(query)
71-
} else {
72-
logger.verbose(TAG) { "No Imap Prefix detected, skipping db migration" }
69+
logger.verbose(TAG) { "completed db migration to version 90 for account ${account.uuid}" }
70+
} catch (e: AuthenticationFailedException) {
71+
logger.warn(TAG, e) {
72+
"failed to fetch IMAP prefix due to authentication error. skipping db migration"
73+
}
74+
} catch (e: IOException) {
75+
logger.warn(TAG, e) {
76+
"failed to fetch IMAP prefix due to network error. skipping db migration"
77+
}
7378
}
74-
75-
logger.verbose(TAG) { "completed db migration to version 107 for account ${account.uuid}" }
7679
}
7780

7881
private fun createImapStore(account: LegacyAccount): ImapStore {
@@ -87,7 +90,7 @@ internal class MigrationTo90(
8790
null
8891
}
8992

90-
return ImapStore.create(
93+
return imapStoreFactory.create(
9194
serverSettings = serverSettings,
9295
config = createImapStoreConfig(account),
9396
trustedSocketFactory = trustedSocketFactory,
@@ -119,4 +122,18 @@ internal class MigrationTo90(
119122
override fun clientInfo() = ImapClientInfo(appName = clientInfoAppName, appVersion = clientInfoAppVersion)
120123
}
121124
}
125+
126+
@Language("RoomSql")
127+
@VisibleForTesting
128+
internal fun buildQuery(imapPrefix: String): String {
129+
return """
130+
|UPDATE folders
131+
| SET server_id = REPLACE(server_id, '$imapPrefix', '')
132+
|WHERE
133+
| server_id IS NOT NULL
134+
| AND server_id LIKE '$imapPrefix%'
135+
| AND type <> 'outbox'
136+
| AND local_only <> 1
137+
""".trimMargin()
138+
}
122139
}

0 commit comments

Comments
 (0)