11package com.fsck.k9.storage.migrations
22
33import android.database.sqlite.SQLiteDatabase
4+ import androidx.annotation.VisibleForTesting
45import com.fsck.k9.mail.AuthType
56import com.fsck.k9.mail.AuthenticationFailedException
67import com.fsck.k9.mail.ServerSettings
@@ -10,6 +11,7 @@ import com.fsck.k9.mail.ssl.TrustedSocketFactory
1011import com.fsck.k9.mail.store.imap.ImapClientInfo
1112import com.fsck.k9.mail.store.imap.ImapStore
1213import com.fsck.k9.mail.store.imap.ImapStoreConfig
14+ import com.fsck.k9.mail.store.imap.ImapStoreFactory
1315import com.fsck.k9.mail.store.imap.ImapStoreSettings
1416import com.fsck.k9.mail.store.imap.ImapStoreSettings.autoDetectNamespace
1517import com.fsck.k9.mail.store.imap.ImapStoreSettings.pathPrefix
@@ -19,6 +21,7 @@ import net.thunderbird.core.android.account.LegacyAccountDto
1921import net.thunderbird.core.common.mail.Protocols
2022import net.thunderbird.core.logging.Logger
2123import net.thunderbird.core.logging.legacy.Log
24+ import org.intellij.lang.annotations.Language
2225import org.koin.core.component.KoinComponent
2326import org.koin.core.component.inject
2427import org.koin.core.qualifier.named
@@ -29,6 +32,7 @@ internal class MigrationTo90(
2932 private val db : SQLiteDatabase ,
3033 private val migrationsHelper : MigrationsHelper ,
3134 private val logger : Logger = Log ,
35+ private val imapStoreFactory : ImapStoreFactory = ImapStore .Companion ,
3236) : KoinComponent {
3337 private val trustedSocketFactory: TrustedSocketFactory by inject()
3438 private val clientInfoAppName: String by inject(named(" ClientInfoAppName" ))
@@ -44,35 +48,29 @@ internal class MigrationTo90(
4448 return
4549 }
4650
47- logger.verbose(TAG ) { " started db migration to version 107 to account ${account.uuid} " }
51+ logger.verbose(TAG ) { " started db migration to version 90 to account ${account.uuid} " }
4852
4953 val imapStore = createImapStore(account)
5054
5155 try {
5256 logger.verbose(TAG ) { " fetching IMAP prefix" }
5357 imapStore.fetchImapPrefix()
5458 } catch (e: AuthenticationFailedException ) {
55- logger.warn(TAG , e) { " failed to fetch IMAP prefix." }
59+ logger.warn(TAG , e) { " failed to fetch IMAP prefix. skipping db migration " }
5660 return
5761 }
5862
5963 val imapPrefix = imapStore.combinedPrefix
6064
6165 if (imapPrefix?.isNotBlank() == true ) {
6266 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()
69-
67+ val query = buildQuery(imapPrefix)
7068 db.execSQL(query)
7169 } else {
7270 logger.verbose(TAG ) { " No Imap Prefix detected, skipping db migration" }
7371 }
7472
75- logger.verbose(TAG ) { " completed db migration to version 107 for account ${account.uuid} " }
73+ logger.verbose(TAG ) { " completed db migration to version 90 for account ${account.uuid} " }
7674 }
7775
7876 private fun createImapStore (account : LegacyAccountDto ): ImapStore {
@@ -87,7 +85,7 @@ internal class MigrationTo90(
8785 null
8886 }
8987
90- return ImapStore .create(
88+ return imapStoreFactory .create(
9189 serverSettings = serverSettings,
9290 config = createImapStoreConfig(account),
9391 trustedSocketFactory = trustedSocketFactory,
@@ -119,4 +117,15 @@ internal class MigrationTo90(
119117 override fun clientInfo () = ImapClientInfo (appName = clientInfoAppName, appVersion = clientInfoAppVersion)
120118 }
121119 }
120+
121+ @Language(" RoomSql" )
122+ @VisibleForTesting
123+ internal fun buildQuery (imapPrefix : String ): String {
124+ return """
125+ |UPDATE folders
126+ | SET server_id = REPLACE(server_id, '$imapPrefix ', '')
127+ |WHERE
128+ | server_id LIKE '$imapPrefix %'
129+ """ .trimMargin()
130+ }
122131}
0 commit comments