Ensure all tables are cleaned up the same before backup import.

This commit is contained in:
Greyson Parrelli
2024-10-02 22:33:04 -04:00
parent 8b7dcd51ae
commit 321c344e77
9 changed files with 30 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ import org.thoughtcrime.securesms.attachments.Attachment
import org.thoughtcrime.securesms.attachments.AttachmentId import org.thoughtcrime.securesms.attachments.AttachmentId
import org.thoughtcrime.securesms.attachments.Cdn import org.thoughtcrime.securesms.attachments.Cdn
import org.thoughtcrime.securesms.attachments.DatabaseAttachment import org.thoughtcrime.securesms.attachments.DatabaseAttachment
import org.thoughtcrime.securesms.backup.v2.database.clearAllDataForBackup
import org.thoughtcrime.securesms.backup.v2.database.clearAllDataForBackupRestore import org.thoughtcrime.securesms.backup.v2.database.clearAllDataForBackupRestore
import org.thoughtcrime.securesms.backup.v2.importer.ChatItemArchiveImporter import org.thoughtcrime.securesms.backup.v2.importer.ChatItemArchiveImporter
import org.thoughtcrime.securesms.backup.v2.processor.AccountDataArchiveProcessor import org.thoughtcrime.securesms.backup.v2.processor.AccountDataArchiveProcessor
@@ -386,8 +387,6 @@ object BackupRepository {
return ImportResult.Failure return ImportResult.Failure
} }
// Note: Without a transaction, bad imports could lead to lost data. But because we have a transaction,
// writes from other threads are blocked. This is something to think more about.
SignalDatabase.rawDatabase.withinTransaction { SignalDatabase.rawDatabase.withinTransaction {
SignalDatabase.recipients.clearAllDataForBackupRestore() SignalDatabase.recipients.clearAllDataForBackupRestore()
SignalDatabase.distributionLists.clearAllDataForBackupRestore() SignalDatabase.distributionLists.clearAllDataForBackupRestore()
@@ -398,6 +397,8 @@ object BackupRepository {
SignalDatabase.reactions.clearAllDataForBackupRestore() SignalDatabase.reactions.clearAllDataForBackupRestore()
SignalDatabase.inAppPayments.clearAllDataForBackupRestore() SignalDatabase.inAppPayments.clearAllDataForBackupRestore()
SignalDatabase.chatColors.clearAllDataForBackupRestore() SignalDatabase.chatColors.clearAllDataForBackupRestore()
SignalDatabase.calls.clearAllDataForBackup()
SignalDatabase.callLinks.clearAllDataForBackup()
// Add back self after clearing data // Add back self after clearing data
val selfId: RecipientId = SignalDatabase.recipients.getAndPossiblyMerge(selfData.aci, selfData.pni, selfData.e164, pniVerified = true, changeSelf = true) val selfId: RecipientId = SignalDatabase.recipients.getAndPossiblyMerge(selfData.aci, selfData.pni, selfData.e164, pniVerified = true, changeSelf = true)

View File

@@ -5,6 +5,7 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.thoughtcrime.securesms.attachments.Attachment import org.thoughtcrime.securesms.attachments.Attachment
import org.thoughtcrime.securesms.attachments.AttachmentId import org.thoughtcrime.securesms.attachments.AttachmentId
@@ -12,6 +13,7 @@ import org.thoughtcrime.securesms.database.AttachmentTable
fun AttachmentTable.clearAllDataForBackupRestore() { fun AttachmentTable.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(AttachmentTable.TABLE_NAME) writableDatabase.deleteAll(AttachmentTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, AttachmentTable.TABLE_NAME)
} }
fun AttachmentTable.restoreWallpaperAttachment(attachment: Attachment): AttachmentId? { fun AttachmentTable.restoreWallpaperAttachment(attachment: Attachment): AttachmentId? {

View File

@@ -5,6 +5,8 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll
import org.signal.core.util.select import org.signal.core.util.select
import org.thoughtcrime.securesms.database.CallLinkTable import org.thoughtcrime.securesms.database.CallLinkTable
@@ -16,3 +18,8 @@ fun CallLinkTable.getCallLinksForBackup(): CallLinkArchiveExporter {
return CallLinkArchiveExporter(cursor) return CallLinkArchiveExporter(cursor)
} }
fun CallLinkTable.clearAllDataForBackup() {
writableDatabase.deleteAll(CallLinkTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, CallLinkTable.TABLE_NAME)
}

View File

@@ -5,6 +5,8 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll
import org.signal.core.util.select import org.signal.core.util.select
import org.thoughtcrime.securesms.database.CallTable import org.thoughtcrime.securesms.database.CallTable
@@ -17,3 +19,8 @@ fun CallTable.getAdhocCallsForBackup(): AdHocCallArchiveExporter {
.run() .run()
) )
} }
fun CallTable.clearAllDataForBackup() {
writableDatabase.deleteAll(CallTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, CallTable.TABLE_NAME)
}

View File

@@ -5,9 +5,11 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.thoughtcrime.securesms.database.ChatColorsTable import org.thoughtcrime.securesms.database.ChatColorsTable
fun ChatColorsTable.clearAllDataForBackupRestore() { fun ChatColorsTable.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(ChatColorsTable.TABLE_NAME) writableDatabase.deleteAll(ChatColorsTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, ChatColorsTable.TABLE_NAME)
} }

View File

@@ -5,6 +5,7 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.signal.core.util.select import org.signal.core.util.select
import org.signal.core.util.withinTransaction import org.signal.core.util.withinTransaction
@@ -42,4 +43,6 @@ fun DistributionListTables.getMembersForBackup(id: DistributionListId): List<Rec
fun DistributionListTables.clearAllDataForBackupRestore() { fun DistributionListTables.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(DistributionListTables.ListTable.TABLE_NAME) writableDatabase.deleteAll(DistributionListTables.ListTable.TABLE_NAME)
writableDatabase.deleteAll(DistributionListTables.MembershipTable.TABLE_NAME) writableDatabase.deleteAll(DistributionListTables.MembershipTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, DistributionListTables.ListTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, DistributionListTables.MembershipTable.TABLE_NAME)
} }

View File

@@ -5,9 +5,11 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.thoughtcrime.securesms.database.InAppPaymentTable import org.thoughtcrime.securesms.database.InAppPaymentTable
fun InAppPaymentTable.clearAllDataForBackupRestore() { fun InAppPaymentTable.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(InAppPaymentTable.TABLE_NAME) writableDatabase.deleteAll(InAppPaymentTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, InAppPaymentTable.TABLE_NAME)
} }

View File

@@ -5,9 +5,11 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.thoughtcrime.securesms.database.ReactionTable import org.thoughtcrime.securesms.database.ReactionTable
fun ReactionTable.clearAllDataForBackupRestore() { fun ReactionTable.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(ReactionTable.TABLE_NAME) writableDatabase.deleteAll(ReactionTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, ReactionTable.TABLE_NAME)
} }

View File

@@ -5,9 +5,11 @@
package org.thoughtcrime.securesms.backup.v2.database package org.thoughtcrime.securesms.backup.v2.database
import org.signal.core.util.SqlUtil
import org.signal.core.util.deleteAll import org.signal.core.util.deleteAll
import org.thoughtcrime.securesms.database.StickerTable import org.thoughtcrime.securesms.database.StickerTable
fun StickerTable.clearAllDataForBackupRestore() { fun StickerTable.clearAllDataForBackupRestore() {
writableDatabase.deleteAll(StickerTable.TABLE_NAME) writableDatabase.deleteAll(StickerTable.TABLE_NAME)
SqlUtil.resetAutoIncrementValue(writableDatabase, StickerTable.TABLE_NAME)
} }