From 8e4d3df400dd68935ef0ce7fe72d5abdff017c0c Mon Sep 17 00:00:00 2001 From: tarsin Date: Thu, 2 Mar 2023 21:52:32 +0800 Subject: [PATCH] Provide cookies database autoMigration --- app/build.gradle.kts | 15 ++++ .../com.hippo.network.CookiesDatabase/1.json | 88 +++++++++++++++++++ .../com.hippo.network.CookiesDatabase/2.json | 88 +++++++++++++++++++ .../hippo/ehviewer/client/EhCookieStore.kt | 2 +- .../java/com/hippo/network/CookieDatabase.kt | 13 ++- 5 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 app/schemas/com.hippo.network.CookiesDatabase/1.json create mode 100644 app/schemas/com.hippo.network.CookiesDatabase/2.json diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 65e9d3512..c188e49f0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -227,3 +227,18 @@ configurations.all { exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk7") exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8") } + +ksp { + arg(RoomSchemaArgProvider(File(projectDir, "schemas"))) +} + +class RoomSchemaArgProvider( + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + val schemaDir: File +) : CommandLineArgumentProvider { + + override fun asArguments(): Iterable { + return listOf("room.schemaLocation=${schemaDir.path}") + } +} diff --git a/app/schemas/com.hippo.network.CookiesDatabase/1.json b/app/schemas/com.hippo.network.CookiesDatabase/1.json new file mode 100644 index 000000000..a0909aa08 --- /dev/null +++ b/app/schemas/com.hippo.network.CookiesDatabase/1.json @@ -0,0 +1,88 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "c801ee62729ba20a203b150211234d5c", + "entities": [ + { + "tableName": "OK_HTTP_3_COOKIE", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`NAME` TEXT, `VALUE` TEXT, `EXPIRES_AT` INTEGER NOT NULL, `DOMAIN` TEXT, `PATH` TEXT, `SECURE` INTEGER NOT NULL, `HTTP_ONLY` INTEGER NOT NULL, `PERSISTENT` INTEGER NOT NULL, `HOST_ONLY` INTEGER NOT NULL, `_id` INTEGER NOT NULL, PRIMARY KEY(`_id`))", + "fields": [ + { + "fieldPath": "name", + "columnName": "NAME", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "value", + "columnName": "VALUE", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "expiresAt", + "columnName": "EXPIRES_AT", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "domain", + "columnName": "DOMAIN", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "path", + "columnName": "PATH", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "secure", + "columnName": "SECURE", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "httpOnly", + "columnName": "HTTP_ONLY", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "persistent", + "columnName": "PERSISTENT", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hostOnly", + "columnName": "HOST_ONLY", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "id", + "columnName": "_id", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "_id" + ] + }, + "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, 'c801ee62729ba20a203b150211234d5c')" + ] + } +} \ No newline at end of file diff --git a/app/schemas/com.hippo.network.CookiesDatabase/2.json b/app/schemas/com.hippo.network.CookiesDatabase/2.json new file mode 100644 index 000000000..7726ebe23 --- /dev/null +++ b/app/schemas/com.hippo.network.CookiesDatabase/2.json @@ -0,0 +1,88 @@ +{ + "formatVersion": 1, + "database": { + "version": 2, + "identityHash": "1b80cd29939b0f43934721f1289ba94d", + "entities": [ + { + "tableName": "OK_HTTP_3_COOKIE", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`NAME` TEXT NOT NULL, `VALUE` TEXT NOT NULL, `EXPIRES_AT` INTEGER NOT NULL, `DOMAIN` TEXT NOT NULL, `PATH` TEXT NOT NULL, `SECURE` INTEGER NOT NULL, `HTTP_ONLY` INTEGER NOT NULL, `PERSISTENT` INTEGER NOT NULL, `HOST_ONLY` INTEGER NOT NULL, `_id` INTEGER, PRIMARY KEY(`_id`))", + "fields": [ + { + "fieldPath": "name", + "columnName": "NAME", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "VALUE", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "expiresAt", + "columnName": "EXPIRES_AT", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "domain", + "columnName": "DOMAIN", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "path", + "columnName": "PATH", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "secure", + "columnName": "SECURE", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "httpOnly", + "columnName": "HTTP_ONLY", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "persistent", + "columnName": "PERSISTENT", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hostOnly", + "columnName": "HOST_ONLY", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "id", + "columnName": "_id", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "_id" + ] + }, + "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, '1b80cd29939b0f43934721f1289ba94d')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.kt b/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.kt index 85dd47b28..cc3eab036 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.kt @@ -31,7 +31,7 @@ import java.util.regex.Pattern typealias KtorCookie = io.ktor.http.Cookie object EhCookieStore : CookieJar, CookiesStorage { - private val db: CookieDatabase = CookieDatabase(EhApplication.application, "okhttp3-cookie-2.db") + private val db: CookieDatabase = CookieDatabase(EhApplication.application, "okhttp3-cookie.db") private val map: MutableMap = db.allCookies fun signOut() { diff --git a/app/src/main/java/com/hippo/network/CookieDatabase.kt b/app/src/main/java/com/hippo/network/CookieDatabase.kt index 8b2af7de6..f47f6d17d 100644 --- a/app/src/main/java/com/hippo/network/CookieDatabase.kt +++ b/app/src/main/java/com/hippo/network/CookieDatabase.kt @@ -16,6 +16,7 @@ package com.hippo.network import android.content.Context +import androidx.room.AutoMigration import androidx.room.ColumnInfo import androidx.room.Dao import androidx.room.Database @@ -69,7 +70,17 @@ interface CookiesDao { fun update(cookie: Cookie) } -@Database(entities = [Cookie::class], version = 1, exportSchema = false) +/* 1 -> 2 some nullability changes */ +@Database( + entities = [Cookie::class], + version = 2, + autoMigrations = [ + AutoMigration( + from = 1, + to = 2 + ) + ] +) abstract class CookiesDatabase : RoomDatabase() { abstract fun cookiesDao(): CookiesDao }