Skip to content

Commit

Permalink
feat(PublicShareMigration): Migrate ShareLink model
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Oct 4, 2024
1 parent b72d64d commit c2bbf29
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions app/src/main/java/com/infomaniak/drive/data/cache/FileMigration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FileMigration : RealmMigration {

// Migrated to version 1
if (oldVersionTemp == 0L) {
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
removeField("order")
removeField("orderBy")
if (hasField("canUseTag")) removeField("canUseTag")
Expand Down Expand Up @@ -73,17 +73,17 @@ class FileMigration : RealmMigration {
addField(FileCategory::userId.name, Int::class.java).setNullable(FileCategory::userId.name, true)
addField("addedToFileAt", Date::class.java, FieldAttribute.REQUIRED)
}
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
addRealmListField(File::categories.name, fileCategorySchema)
}
schema.get(FileCategory::class.java.simpleName)?.apply {
schema[FileCategory::class.java.simpleName]?.apply {
isEmbedded = true
}
// Rights migration with sentry logs
val sentryLogs = arrayListOf<Pair<Int, String>>()
var countOfflineFiles = 0
runCatching {
schema.get(Rights::class.java.simpleName)?.transform { // apply for each right
schema[Rights::class.java.simpleName]?.transform { // apply for each right
val fileId = it.getInt("fileId")
val file = realm.where(File::class.java.simpleName).equalTo(File::id.name, fileId).findFirst()
if (file == null) {
Expand Down Expand Up @@ -122,7 +122,7 @@ class FileMigration : RealmMigration {
// - Added new field (Folder Color) in File table
// - Added new field (Version Code) in File table
if (oldVersionTemp == 2L) {
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
addField("_color", String::class.java)
addField("versionCode", Int::class.java)
}
Expand Down Expand Up @@ -193,7 +193,7 @@ class FileMigration : RealmMigration {
}

// Rights migration
schema.get(Rights::class.java.simpleName)?.apply {
schema[Rights::class.java.simpleName]?.apply {
renameField("show", Rights::canShow.name)
renameField("read", Rights::canRead.name)
renameField("write", Rights::canWrite.name)
Expand Down Expand Up @@ -228,15 +228,15 @@ class FileMigration : RealmMigration {
}

// FileCategory migration
schema.get(FileCategory::class.java.simpleName)?.apply {
schema[FileCategory::class.java.simpleName]?.apply {
renameField("id", FileCategory::categoryId.name)
renameField("iaCategoryUserValidation", FileCategory::userValidation.name)
renameField("isGeneratedByIa", FileCategory::isGeneratedByAI.name)
renameField("addedToFileAt", FileCategory::addedAt.name)
}

// File migration
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
removeField("collaborativeFolder")
removeField("onlyofficeConvertExtension")
removeField("hasVersion")
Expand All @@ -259,19 +259,19 @@ class FileMigration : RealmMigration {
}

// FileActivity migration
schema.get(FileActivity::class.java.simpleName)?.apply {
schema[FileActivity::class.java.simpleName]?.apply {
removeField("path")
}

// Set embedded objects
schema.get(ShareLinkCapabilities::class.java.simpleName)?.isEmbedded = true
schema.get(ShareLink::class.java.simpleName)?.isEmbedded = true
schema.get(DropBoxValidity::class.java.simpleName)?.isEmbedded = true
schema.get(DropBoxSize::class.java.simpleName)?.isEmbedded = true
schema.get(DropBoxCapabilities::class.java.simpleName)?.isEmbedded = true
schema.get(DropBox::class.java.simpleName)?.isEmbedded = true
schema.get(FileVersion::class.java.simpleName)?.isEmbedded = true
schema.get(FileConversion::class.java.simpleName)?.isEmbedded = true
schema[ShareLinkCapabilities::class.java.simpleName]?.isEmbedded = true
schema[ShareLink::class.java.simpleName]?.isEmbedded = true
schema[DropBoxValidity::class.java.simpleName]?.isEmbedded = true
schema[DropBoxSize::class.java.simpleName]?.isEmbedded = true
schema[DropBoxCapabilities::class.java.simpleName]?.isEmbedded = true
schema[DropBox::class.java.simpleName]?.isEmbedded = true
schema[FileVersion::class.java.simpleName]?.isEmbedded = true
schema[FileConversion::class.java.simpleName]?.isEmbedded = true

oldVersionTemp++
}
Expand All @@ -296,17 +296,17 @@ class FileMigration : RealmMigration {
}

// File migration
schema.get(File::class.java.simpleName)?.addRealmObjectField(File::externalImport.name, fileExternalImportSchema)
schema[File::class.java.simpleName]?.addRealmObjectField(File::externalImport.name, fileExternalImportSchema)

// Set embedded objects
schema.get(FileExternalImport::class.java.simpleName)?.isEmbedded = true
schema[FileExternalImport::class.java.simpleName]?.isEmbedded = true

oldVersionTemp++
}

// Migrated to version 6
if (oldVersionTemp == 5L) {
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
renameField("_color", File::color.name)
}

Expand All @@ -316,7 +316,7 @@ class FileMigration : RealmMigration {
// Migrated to version 7
// - Migrate to ApiV3
if (oldVersionTemp == 6L) {
schema.get(File::class.java.simpleName)?.apply {
schema[File::class.java.simpleName]?.apply {
addField(File::isMarkedAsOffline.name, Boolean::class.java, FieldAttribute.REQUIRED)
addField("uid", String::class.java, FieldAttribute.REQUIRED)
addField("revisedAt", Long::class.java)
Expand Down Expand Up @@ -351,10 +351,18 @@ class FileMigration : RealmMigration {
}

// Migrated to version 8
// - Add new field `colorable` in Rights table
// - Add new field `fileId` in ShareLink table
// - Add new field `accessBlocked` in ShareLink table
if (oldVersionTemp == 7L) {
schema.get("Rights")?.apply {
schema["Rights"]?.apply {
addField("colorable", Boolean::class.java, FieldAttribute.REQUIRED)
}
schema["ShareLink"]?.apply {
addField("fileId", Int::class.java)
setNullable("fileId", true)
addField("accessBlocked", Boolean::class.java, FieldAttribute.REQUIRED)
}

oldVersionTemp++
}
Expand All @@ -375,7 +383,7 @@ class FileMigration : RealmMigration {
// Delete all realm DB
realm.deleteAll()
// Continue migration
schema.get(Rights::class.java.simpleName)?.isEmbedded = true
schema[Rights::class.java.simpleName]?.isEmbedded = true

// Logout the current user if there is at least one offline file
offlineFile?.let {
Expand Down

0 comments on commit c2bbf29

Please sign in to comment.