From 869414d48a409f8573a54f7c8302a86dae96ca7a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 14 Aug 2022 16:36:18 +0200 Subject: [PATCH 1/2] fix ci --- .../7.json | 174 ++++++++++++++++++ .../main/java/com/github/libretube/MyApp.kt | 48 +++++ .../com/github/libretube/db/AppDatabase.kt | 5 +- .../com/github/libretube/db/DatabaseHolder.kt | 1 + .../libretube/preferences/PreferenceKeys.kt | 5 - 5 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 app/schemas/com.github.libretube.db.AppDatabase/7.json diff --git a/app/schemas/com.github.libretube.db.AppDatabase/7.json b/app/schemas/com.github.libretube.db.AppDatabase/7.json new file mode 100644 index 000000000..17bde0ca2 --- /dev/null +++ b/app/schemas/com.github.libretube.db.AppDatabase/7.json @@ -0,0 +1,174 @@ +{ + "formatVersion": 1, + "database": { + "version": 7, + "identityHash": "c9803a67ce206dbda6e44ed761f80136", + "entities": [ + { + "tableName": "watchHistoryItem", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`videoId` TEXT NOT NULL, `title` TEXT, `uploadDate` TEXT, `uploader` TEXT, `uploaderUrl` TEXT, `uploaderAvatar` TEXT, `thumbnailUrl` TEXT, `duration` INTEGER, PRIMARY KEY(`videoId`))", + "fields": [ + { + "fieldPath": "videoId", + "columnName": "videoId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "uploadDate", + "columnName": "uploadDate", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "uploader", + "columnName": "uploader", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "uploaderUrl", + "columnName": "uploaderUrl", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "uploaderAvatar", + "columnName": "uploaderAvatar", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "thumbnailUrl", + "columnName": "thumbnailUrl", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "duration", + "columnName": "duration", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "videoId" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "watchPosition", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`videoId` TEXT NOT NULL, `position` INTEGER NOT NULL, PRIMARY KEY(`videoId`))", + "fields": [ + { + "fieldPath": "videoId", + "columnName": "videoId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "videoId" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "searchHistoryItem", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`query` TEXT NOT NULL, PRIMARY KEY(`query`))", + "fields": [ + { + "fieldPath": "query", + "columnName": "query", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "query" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "customInstance", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`name` TEXT NOT NULL, `apiUrl` TEXT NOT NULL, `frontendUrl` TEXT NOT NULL, PRIMARY KEY(`name`))", + "fields": [ + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "apiUrl", + "columnName": "apiUrl", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "frontendUrl", + "columnName": "frontendUrl", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "name" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "localSubscription", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`channelId` TEXT NOT NULL, PRIMARY KEY(`channelId`))", + "fields": [ + { + "fieldPath": "channelId", + "columnName": "channelId", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "channelId" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c9803a67ce206dbda6e44ed761f80136')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/MyApp.kt b/app/src/main/java/com/github/libretube/MyApp.kt index 1b864c5f2..45071f592 100644 --- a/app/src/main/java/com/github/libretube/MyApp.kt +++ b/app/src/main/java/com/github/libretube/MyApp.kt @@ -7,13 +7,19 @@ import android.content.Context import android.os.Build import android.os.StrictMode import android.os.StrictMode.VmPolicy +import androidx.preference.PreferenceManager 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.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.PreferenceKeys import com.github.libretube.util.ExceptionHandler import com.github.libretube.util.NotificationHelper +import java.lang.Exception class MyApp : Application() { override fun onCreate() { @@ -61,6 +67,11 @@ class MyApp : Application() { * Legacy preference file migration */ prefFileMigration() + + /** + * Database Migration + */ + databaseMigration() } /** @@ -142,4 +153,41 @@ class MyApp : Application() { 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>() {} + 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>() {} + 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() + } } diff --git a/app/src/main/java/com/github/libretube/db/AppDatabase.kt b/app/src/main/java/com/github/libretube/db/AppDatabase.kt index 6ecbfbe22..f5e9ea164 100644 --- a/app/src/main/java/com/github/libretube/db/AppDatabase.kt +++ b/app/src/main/java/com/github/libretube/db/AppDatabase.kt @@ -22,10 +22,7 @@ import com.github.libretube.db.obj.WatchPosition CustomInstance::class, LocalSubscription::class ], - version = 7, - autoMigrations = [ - AutoMigration(from = 7, to = 8) - ] + version = 7 ) abstract class AppDatabase : RoomDatabase() { /** diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt index 1bf4db5ca..2d92fdbcc 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt @@ -13,6 +13,7 @@ object DatabaseHolder { AppDatabase::class.java, DATABASE_NAME ) + .fallbackToDestructiveMigration() .build() } } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt index 527142665..90809cbf9 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt @@ -108,9 +108,4 @@ object PreferenceKeys { * Error logs */ const val ERROR_LOG = "error_log" - - /** - * Data - */ - const val LOCAL_SUBSCRIPTIONS = "local_subscriptions" } From 47f5ef1ebc056e623374e4d23686a28602f86df5 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 14 Aug 2022 16:37:12 +0200 Subject: [PATCH 2/2] remove migration file --- .../7.json | 174 ------------------ .../com/github/libretube/db/AppDatabase.kt | 1 - 2 files changed, 175 deletions(-) delete mode 100644 app/schemas/com.github.libretube.db.AppDatabase/7.json diff --git a/app/schemas/com.github.libretube.db.AppDatabase/7.json b/app/schemas/com.github.libretube.db.AppDatabase/7.json deleted file mode 100644 index 17bde0ca2..000000000 --- a/app/schemas/com.github.libretube.db.AppDatabase/7.json +++ /dev/null @@ -1,174 +0,0 @@ -{ - "formatVersion": 1, - "database": { - "version": 7, - "identityHash": "c9803a67ce206dbda6e44ed761f80136", - "entities": [ - { - "tableName": "watchHistoryItem", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`videoId` TEXT NOT NULL, `title` TEXT, `uploadDate` TEXT, `uploader` TEXT, `uploaderUrl` TEXT, `uploaderAvatar` TEXT, `thumbnailUrl` TEXT, `duration` INTEGER, PRIMARY KEY(`videoId`))", - "fields": [ - { - "fieldPath": "videoId", - "columnName": "videoId", - "affinity": "TEXT", - "notNull": true - }, - { - "fieldPath": "title", - "columnName": "title", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "uploadDate", - "columnName": "uploadDate", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "uploader", - "columnName": "uploader", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "uploaderUrl", - "columnName": "uploaderUrl", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "uploaderAvatar", - "columnName": "uploaderAvatar", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "thumbnailUrl", - "columnName": "thumbnailUrl", - "affinity": "TEXT", - "notNull": false - }, - { - "fieldPath": "duration", - "columnName": "duration", - "affinity": "INTEGER", - "notNull": false - } - ], - "primaryKey": { - "columnNames": [ - "videoId" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - }, - { - "tableName": "watchPosition", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`videoId` TEXT NOT NULL, `position` INTEGER NOT NULL, PRIMARY KEY(`videoId`))", - "fields": [ - { - "fieldPath": "videoId", - "columnName": "videoId", - "affinity": "TEXT", - "notNull": true - }, - { - "fieldPath": "position", - "columnName": "position", - "affinity": "INTEGER", - "notNull": true - } - ], - "primaryKey": { - "columnNames": [ - "videoId" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - }, - { - "tableName": "searchHistoryItem", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`query` TEXT NOT NULL, PRIMARY KEY(`query`))", - "fields": [ - { - "fieldPath": "query", - "columnName": "query", - "affinity": "TEXT", - "notNull": true - } - ], - "primaryKey": { - "columnNames": [ - "query" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - }, - { - "tableName": "customInstance", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`name` TEXT NOT NULL, `apiUrl` TEXT NOT NULL, `frontendUrl` TEXT NOT NULL, PRIMARY KEY(`name`))", - "fields": [ - { - "fieldPath": "name", - "columnName": "name", - "affinity": "TEXT", - "notNull": true - }, - { - "fieldPath": "apiUrl", - "columnName": "apiUrl", - "affinity": "TEXT", - "notNull": true - }, - { - "fieldPath": "frontendUrl", - "columnName": "frontendUrl", - "affinity": "TEXT", - "notNull": true - } - ], - "primaryKey": { - "columnNames": [ - "name" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - }, - { - "tableName": "localSubscription", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`channelId` TEXT NOT NULL, PRIMARY KEY(`channelId`))", - "fields": [ - { - "fieldPath": "channelId", - "columnName": "channelId", - "affinity": "TEXT", - "notNull": true - } - ], - "primaryKey": { - "columnNames": [ - "channelId" - ], - "autoGenerate": false - }, - "indices": [], - "foreignKeys": [] - } - ], - "views": [], - "setupQueries": [ - "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c9803a67ce206dbda6e44ed761f80136')" - ] - } -} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/db/AppDatabase.kt b/app/src/main/java/com/github/libretube/db/AppDatabase.kt index f5e9ea164..88daecd16 100644 --- a/app/src/main/java/com/github/libretube/db/AppDatabase.kt +++ b/app/src/main/java/com/github/libretube/db/AppDatabase.kt @@ -1,6 +1,5 @@ package com.github.libretube.db -import androidx.room.AutoMigration import androidx.room.Database import androidx.room.RoomDatabase import com.github.libretube.db.dao.CustomInstanceDao