Skip to content

Commit

Permalink
Add and Throw Explicit Errors for DB Migration Failure Points (#3096)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1206488453854252/1207802624568506/f

**Description**: The PIR v3 migration can fail at three points:
1. Orphaned Record Cleanup
2. Table Recreation
3. Final Foreign Key Violation Check

Currently, we throw an explicit error for #3, which is observable via
Pixels. We should also throw explicit errors for #1 and #2. This PR adds
these new errors.
  • Loading branch information
aataraxiaa authored Aug 13, 2024
1 parent 878252b commit 6001f25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import Foundation
import GRDB
import Common

enum DataBrokerProtectionDatabaseMigrationErrors: Error {
case deleteOrphanedRecordFailed
case recreateTablesFailed
case foreignKeyViolation
}

/// Conforming types provide migrations for the PIR database. Mostly utilized for testing.
protocol DataBrokerProtectionDatabaseMigrationsProvider {
static var v2Migrations: (inout DatabaseMigrator) throws -> Void { get }
Expand Down Expand Up @@ -238,7 +244,7 @@ final class DefaultDataBrokerProtectionDatabaseMigrationsProvider: DataBrokerPro
// Throws an error if a foreign key violation exists in the database.
try database.checkForeignKeys()
} catch {
throw DataBrokerProtectionDatabaseErrors.migrationFailureIntegrityCheck
throw DataBrokerProtectionDatabaseMigrationErrors.foreignKeyViolation
}
}

Expand Down Expand Up @@ -279,8 +285,12 @@ final class DefaultDataBrokerProtectionDatabaseMigrationsProvider: DataBrokerPro
deleteStatements.append(sqlOrphanedCleanupFromProfile(of: AddressDB.databaseTableName))
deleteStatements.append(sqlOrphanedCleanupFromProfile(of: PhoneDB.databaseTableName))

for sql in deleteStatements {
try database.execute(sql: sql)
do {
for sql in deleteStatements {
try database.execute(sql: sql)
}
} catch {
throw DataBrokerProtectionDatabaseMigrationErrors.deleteOrphanedRecordFailed
}

// As a precaution, explicitly check for any foreign key violations which were missed
Expand Down Expand Up @@ -356,16 +366,20 @@ final class DefaultDataBrokerProtectionDatabaseMigrationsProvider: DataBrokerPro
}

private static func recreateTablesV3(database: Database) throws {
try recreateNameTable(database: database)
try recreateAddressTable(database: database)
try recreatePhoneTable(database: database)
try recreateProfileQueryTable(database: database)
try recreateScanTable(database: database)
try recreateScanHistoryTable(database: database)
try recreateExtractedProfileTable(database: database)
try recreateOptOutTable(database: database)
try recreateOptOutHistoryTable(database: database)
try recreateOptOutAttemptTable(database: database)
do {
try recreateNameTable(database: database)
try recreateAddressTable(database: database)
try recreatePhoneTable(database: database)
try recreateProfileQueryTable(database: database)
try recreateScanTable(database: database)
try recreateScanHistoryTable(database: database)
try recreateExtractedProfileTable(database: database)
try recreateOptOutTable(database: database)
try recreateOptOutHistoryTable(database: database)
try recreateOptOutAttemptTable(database: database)
} catch {
throw DataBrokerProtectionDatabaseMigrationErrors.recreateTablesFailed
}
}

private static func recreateNameTable(database: Database) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import GRDB

enum DataBrokerProtectionDatabaseErrors: Error {
case elementNotFound
case migrationFailureIntegrityCheck
}

protocol DataBrokerProtectionDatabaseProvider: SecureStorageDatabaseProvider {
Expand Down

0 comments on commit 6001f25

Please sign in to comment.