-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate database location on iOS to app group
- Loading branch information
Showing
9 changed files
with
158 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
core/src/iosMain/kotlin/com/prof18/feedflow/core/utils/AppGroupDatabasePath.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.prof18.feedflow.core.utils | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import platform.Foundation.NSFileManager | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
fun getAppGroupDatabasePath(): String { | ||
val fileManager = NSFileManager.defaultManager() | ||
val containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier(IOS_APP_GROUP) | ||
check(containerURL != null) { "Could not access App Group container" } | ||
|
||
val containerPath = containerURL.path | ||
val directoryPath = "$containerPath/databases" | ||
|
||
if (!fileManager.fileExistsAtPath(directoryPath)) { | ||
fileManager.createDirectoryAtPath( | ||
directoryPath, | ||
withIntermediateDirectories = true, | ||
attributes = null, | ||
error = null, | ||
) | ||
} | ||
return directoryPath | ||
} | ||
|
||
internal const val IOS_APP_GROUP = "group.com.prof18.feedflow" |
83 changes: 83 additions & 0 deletions
83
core/src/iosMain/kotlin/com/prof18/feedflow/core/utils/DatabaseFileMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.prof18.feedflow.core.utils | ||
|
||
import co.touchlab.kermit.Logger | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import platform.Foundation.NSApplicationSupportDirectory | ||
import platform.Foundation.NSFileManager | ||
import platform.Foundation.NSSearchPathForDirectoriesInDomains | ||
import platform.Foundation.NSURL | ||
import platform.Foundation.NSUserDomainMask | ||
import kotlin.experimental.ExperimentalObjCRefinement | ||
|
||
class DatabaseFileMigration( | ||
private val databaseName: String, | ||
) { | ||
|
||
@OptIn(ExperimentalForeignApi::class, ExperimentalObjCRefinement::class) | ||
@HiddenFromObjC | ||
fun migrate() { | ||
if (!isDatabaseAvailable()) { | ||
Logger.d { "No need to migrate the $databaseName database" } | ||
return | ||
} | ||
|
||
val databasePathString = getAppDatabasePathAsString() | ||
val appDatabasePath = NSURL.fileURLWithPath(databasePathString) | ||
|
||
val appWalPath = NSURL.fileURLWithPath("$databasePathString-wal") | ||
val appShmPath = NSURL.fileURLWithPath("$databasePathString-shm") | ||
|
||
val groupDatabasePath = NSURL.fileURLWithPath(getAppGroupDatabasePath()) | ||
.URLByAppendingPathComponent(databaseName) ?: return | ||
|
||
groupDatabasePath.path?.let { path -> | ||
if (NSFileManager.defaultManager.fileExistsAtPath(path)) { | ||
Logger.d { "Database file already exists in the new location, skipping" } | ||
return | ||
} | ||
} | ||
|
||
val groupWalPath = NSURL.fileURLWithPath(getAppGroupDatabasePath()) | ||
.URLByAppendingPathComponent("$databaseName-wal") ?: return | ||
|
||
val groupShmPath = NSURL.fileURLWithPath(getAppGroupDatabasePath()) | ||
.URLByAppendingPathComponent("$databaseName-shm") ?: return | ||
|
||
// Move files | ||
NSFileManager.defaultManager.copyItemAtURL( | ||
srcURL = appDatabasePath, | ||
toURL = groupDatabasePath, | ||
error = null, | ||
) | ||
NSFileManager.defaultManager.copyItemAtURL( | ||
srcURL = appWalPath, | ||
toURL = groupWalPath, | ||
error = null, | ||
) | ||
NSFileManager.defaultManager.copyItemAtURL( | ||
srcURL = appShmPath, | ||
toURL = groupShmPath, | ||
error = null, | ||
) | ||
|
||
// Delete stuff | ||
NSFileManager.defaultManager.removeItemAtURL(appDatabasePath, null) | ||
NSFileManager.defaultManager.removeItemAtURL(appWalPath, null) | ||
NSFileManager.defaultManager.removeItemAtURL(appShmPath, null) | ||
|
||
Logger.d { "$databaseName Database file Migration done" } | ||
} | ||
|
||
private fun isDatabaseAvailable(): Boolean { | ||
val databaseDirectory = getAppDatabasePathAsString() | ||
val fileManager = NSFileManager.defaultManager() | ||
return fileManager.fileExistsAtPath(databaseDirectory) | ||
} | ||
|
||
private fun getAppDatabasePathAsString(): String { | ||
val paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true) | ||
val documentsDirectory = paths[0] as String | ||
|
||
return "$documentsDirectory/databases/$databaseName" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters