Merge pull request #1064 from Bnyro/master

Auto-Migration to the database for users
This commit is contained in:
Bnyro 2022-08-14 16:38:35 +02:00 committed by GitHub
commit a274ba7979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 10 deletions

View File

@ -7,13 +7,19 @@ import android.content.Context
import android.os.Build import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import androidx.preference.PreferenceManager
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.ExceptionHandler import com.github.libretube.util.ExceptionHandler
import com.github.libretube.util.NotificationHelper import com.github.libretube.util.NotificationHelper
import java.lang.Exception
class MyApp : Application() { class MyApp : Application() {
override fun onCreate() { override fun onCreate() {
@ -61,6 +67,11 @@ class MyApp : Application() {
* Legacy preference file migration * Legacy preference file migration
*/ */
prefFileMigration() prefFileMigration()
/**
* Database Migration
*/
databaseMigration()
} }
/** /**
@ -142,4 +153,41 @@ class MyApp : Application() {
legacyTokenPrefs.edit().putString("token", "") legacyTokenPrefs.edit().putString("token", "")
} }
} }
/**
* Migration from the preferences to the database
*/
private fun databaseMigration() {
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val mapper = ObjectMapper()
Thread {
val legacyWatchHistory = prefs.getString("watch_history", "")
if (legacyWatchHistory != "") {
try {
val type = object : TypeReference<List<WatchHistoryItem>>() {}
val watchHistoryItems = mapper.readValue(legacyWatchHistory, type)
DatabaseHolder.db.watchHistoryDao().insertAll(
*watchHistoryItems.toTypedArray()
)
} catch (e: Exception) {}
prefs.edit().putString("watch_history", "").commit()
}
val legacyWatchPositions = prefs.getString("watch_positions", "")
if (legacyWatchPositions != "") {
try {
val type = object : TypeReference<List<WatchPosition>>() {}
val watchPositions = mapper.readValue(legacyWatchPositions, type)
DatabaseHolder.db.watchPositionDao().insertAll(
*watchPositions.toTypedArray()
)
} catch (e: Exception) {}
prefs.edit().remove("watch_positions").commit()
}
prefs.edit()
.remove("custom_instances")
.remove("local_subscriptions")
.commit()
}.start()
}
} }

View File

@ -1,6 +1,5 @@
package com.github.libretube.db package com.github.libretube.db
import androidx.room.AutoMigration
import androidx.room.Database import androidx.room.Database
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import com.github.libretube.db.dao.CustomInstanceDao import com.github.libretube.db.dao.CustomInstanceDao
@ -22,10 +21,7 @@ import com.github.libretube.db.obj.WatchPosition
CustomInstance::class, CustomInstance::class,
LocalSubscription::class LocalSubscription::class
], ],
version = 7, version = 7
autoMigrations = [
AutoMigration(from = 7, to = 8)
]
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
/** /**

View File

@ -13,6 +13,7 @@ object DatabaseHolder {
AppDatabase::class.java, AppDatabase::class.java,
DATABASE_NAME DATABASE_NAME
) )
.fallbackToDestructiveMigration()
.build() .build()
} }
} }

View File

@ -108,9 +108,4 @@ object PreferenceKeys {
* Error logs * Error logs
*/ */
const val ERROR_LOG = "error_log" const val ERROR_LOG = "error_log"
/**
* Data
*/
const val LOCAL_SUBSCRIPTIONS = "local_subscriptions"
} }