Skip to content

Commit

Permalink
fix db -> new build
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Jul 18, 2024
1 parent 7315867 commit a477bc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Brand/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ import Foundation
// Database Realm
//
let databaseName = "nextcloud.realm"
let databaseSchemaVersion: UInt64 = 350
let databaseSchemaVersion: UInt64 = 354
4 changes: 2 additions & 2 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5390,7 +5390,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -5456,7 +5456,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down
109 changes: 27 additions & 82 deletions iOSClient/Data/NCManageDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class NCManageDatabase: NSObject {
override init() {
let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups)
let databaseFileUrlPath = dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName)
let bundleUrl: URL = Bundle.main.bundleURL
let bundlePathExtension: String = bundleUrl.pathExtension
let isAppex: Bool = bundlePathExtension == "appex"

if let databaseFilePath = databaseFileUrlPath?.path {
if FileManager.default.fileExists(atPath: databaseFilePath) {
Expand All @@ -65,92 +62,40 @@ class NCManageDatabase: NSObject {
}
}

if isAppex {
Realm.Configuration.defaultConfiguration = Realm.Configuration(
fileURL: dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName),
do {
_ = try Realm(configuration: Realm.Configuration(
fileURL: databaseFileUrlPath,
schemaVersion: databaseSchemaVersion,
objectTypes: [tableMetadata.self,
tableLocalFile.self,
tableDirectory.self,
tableTag.self,
tableAccount.self,
tableCapabilities.self,
tablePhotoLibrary.self,
tableE2eEncryption.self,
tableE2eEncryptionLock.self,
tableE2eMetadata12.self,
tableE2eMetadata.self,
tableE2eUsers.self,
tableE2eCounter.self,
tableShare.self,
tableChunk.self,
tableAvatar.self,
tableDashboardWidget.self,
tableDashboardWidgetButton.self,
NCDBLayoutForView.self,
TableSecurityGuardDiagnostics.self]
)
} else {
do {
_ = try Realm(configuration: Realm.Configuration(
fileURL: databaseFileUrlPath,
schemaVersion: databaseSchemaVersion,

migrationBlock: { migration, oldSchemaVersion in

if oldSchemaVersion < 255 {
migration.deleteData(forType: tableActivity.className())
migration.deleteData(forType: tableActivityLatestId.className())
migration.deleteData(forType: tableActivityPreview.className())
migration.deleteData(forType: tableActivitySubjectRich.className())
}

if oldSchemaVersion < 292 {
migration.deleteData(forType: tableVideo.className())
}

if oldSchemaVersion < 319 {
migration.deleteData(forType: tableChunk.className())
migration.deleteData(forType: tableDirectory.className())
migration.deleteData(forType: tableE2eEncryptionLock.className())
migration.deleteData(forType: tableGPS.className())
}

if oldSchemaVersion < 333 {
migration.deleteData(forType: tableMetadata.className())
migration.enumerateObjects(ofType: tableDirectory.className()) { _, newObject in
newObject?["etag"] = ""
}
}

}, shouldCompactOnLaunch: { totalBytes, usedBytes in
// totalBytes refers to the size of the file on disk in bytes (data + free space)
// usedBytes refers to the number of bytes used by data in the file
// Compact if the file is over 100MB in size and less than 50% 'used'
let oneHundredMB = 100 * 1024 * 1024
return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 354 {
migration.deleteData(forType: NCDBLayoutForView.className())
}
))

} catch let error {
if let databaseFileUrlPath = databaseFileUrlPath {
do {
}, shouldCompactOnLaunch: { totalBytes, usedBytes in
// totalBytes refers to the size of the file on disk in bytes (data + free space)
// usedBytes refers to the number of bytes used by data in the file
// Compact if the file is over 100MB in size and less than 50% 'used'
let oneHundredMB = 100 * 1024 * 1024
return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
}
))
} catch let error {
if let databaseFileUrlPath = databaseFileUrlPath {
do {
#if !EXTENSION
let nkError = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: error.localizedDescription)
NCContentPresenter().showError(error: nkError, priority: .max)
let nkError = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: error.localizedDescription)
NCContentPresenter().showError(error: nkError, priority: .max)
#endif
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] DATABASE ERROR: \(error.localizedDescription)")
try FileManager.default.removeItem(at: databaseFileUrlPath)
} catch {}
}
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] DATABASE ERROR: \(error.localizedDescription)")
try FileManager.default.removeItem(at: databaseFileUrlPath)
} catch {}
}

Realm.Configuration.defaultConfiguration = Realm.Configuration(
fileURL: dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName),
schemaVersion: databaseSchemaVersion
)
}

Realm.Configuration.defaultConfiguration = Realm.Configuration(
fileURL: dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName),
schemaVersion: databaseSchemaVersion
)

// Verify Database, if corrupt remove it
do {
_ = try Realm()
Expand Down

0 comments on commit a477bc3

Please sign in to comment.