Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fsck.k9.storage.migrations

import android.database.sqlite.SQLiteDatabase
import androidx.annotation.VisibleForTesting
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.AuthenticationFailedException
import com.fsck.k9.mail.ServerSettings
Expand All @@ -10,6 +11,7 @@ import com.fsck.k9.mail.ssl.TrustedSocketFactory
import com.fsck.k9.mail.store.imap.ImapClientInfo
import com.fsck.k9.mail.store.imap.ImapStore
import com.fsck.k9.mail.store.imap.ImapStoreConfig
import com.fsck.k9.mail.store.imap.ImapStoreFactory
import com.fsck.k9.mail.store.imap.ImapStoreSettings
import com.fsck.k9.mail.store.imap.ImapStoreSettings.autoDetectNamespace
import com.fsck.k9.mail.store.imap.ImapStoreSettings.pathPrefix
Expand All @@ -19,6 +21,7 @@ import net.thunderbird.core.android.account.LegacyAccountDto
import net.thunderbird.core.common.mail.Protocols
import net.thunderbird.core.logging.Logger
import net.thunderbird.core.logging.legacy.Log
import org.intellij.lang.annotations.Language
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.core.qualifier.named
Expand All @@ -29,6 +32,7 @@ internal class MigrationTo90(
private val db: SQLiteDatabase,
private val migrationsHelper: MigrationsHelper,
private val logger: Logger = Log,
private val imapStoreFactory: ImapStoreFactory = ImapStore.Companion,
) : KoinComponent {
private val trustedSocketFactory: TrustedSocketFactory by inject()
private val clientInfoAppName: String by inject(named("ClientInfoAppName"))
Expand All @@ -44,35 +48,29 @@ internal class MigrationTo90(
return
}

logger.verbose(TAG) { "started db migration to version 107 to account ${account.uuid}" }
logger.verbose(TAG) { "started db migration to version 90 to account ${account.uuid}" }

val imapStore = createImapStore(account)

try {
logger.verbose(TAG) { "fetching IMAP prefix" }
imapStore.fetchImapPrefix()
} catch (e: AuthenticationFailedException) {
logger.warn(TAG, e) { "failed to fetch IMAP prefix." }
logger.warn(TAG, e) { "failed to fetch IMAP prefix. skipping db migration" }
return
}

val imapPrefix = imapStore.combinedPrefix

if (imapPrefix?.isNotBlank() == true) {
logger.verbose(TAG) { "Imap Prefix ($imapPrefix) detected, updating folder's server_id" }
val query = """
|UPDATE folders
| SET server_id = REPLACE(server_id, '$imapPrefix', '')
|WHERE
| server_id LIKE '$imapPrefix%'
""".trimMargin()

val query = buildQuery(imapPrefix)
db.execSQL(query)
} else {
logger.verbose(TAG) { "No Imap Prefix detected, skipping db migration" }
}

logger.verbose(TAG) { "completed db migration to version 107 for account ${account.uuid}" }
logger.verbose(TAG) { "completed db migration to version 90 for account ${account.uuid}" }
}

private fun createImapStore(account: LegacyAccountDto): ImapStore {
Expand All @@ -87,7 +85,7 @@ internal class MigrationTo90(
null
}

return ImapStore.create(
return imapStoreFactory.create(
serverSettings = serverSettings,
config = createImapStoreConfig(account),
trustedSocketFactory = trustedSocketFactory,
Expand Down Expand Up @@ -119,4 +117,15 @@ internal class MigrationTo90(
override fun clientInfo() = ImapClientInfo(appName = clientInfoAppName, appVersion = clientInfoAppVersion)
}
}

@Language("RoomSql")
@VisibleForTesting
internal fun buildQuery(imapPrefix: String): String {
return """
|UPDATE folders
| SET server_id = REPLACE(server_id, '$imapPrefix', '')
|WHERE
| server_id LIKE '$imapPrefix%'
""".trimMargin()
}
}
Loading
Loading