Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and Throw Explicit Errors for DB Migration Failure Points #3096

Merged
merged 1 commit into from
Aug 13, 2024
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
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
Loading