From 2cd24ff574592bfae3a6a25e27518c0c4c96e613 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Wed, 2 May 2018 02:56:42 +0200 Subject: [PATCH] Add support for migrating old Quasseldroid settings Signed-off-by: Janne Koschinski <janne@kuschku.de> --- lib/build.gradle.kts | 22 ---- malheur/build.gradle.kts | 22 ---- .../persistence/AccountDatabase.kt | 3 +- .../persistence/LegacyAccountDatabase.kt | 103 ++++++++++++++++++ viewmodel/build.gradle.kts | 22 ---- 5 files changed, 104 insertions(+), 68 deletions(-) create mode 100644 persistence/src/main/java/de/kuschku/quasseldroid/persistence/LegacyAccountDatabase.kt diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index e5afee061..d0cc17b26 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -36,28 +36,6 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* - * Quasseldroid - Quassel client for Android - * - * Copyright (c) 2018 Janne Koschinski - * Copyright (c) 2018 Ken Børge Viktil - * Copyright (c) 2018 Magnus Fjell - * Copyright (c) 2018 Martin Sandsmark - * Copyright (c) 2018 The Quassel Project - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - import org.gradle.api.Project import org.gradle.api.artifacts.ExternalModuleDependency import org.gradle.kotlin.dsl.* diff --git a/malheur/build.gradle.kts b/malheur/build.gradle.kts index 92d078e95..5874f08e3 100644 --- a/malheur/build.gradle.kts +++ b/malheur/build.gradle.kts @@ -36,28 +36,6 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* - * Quasseldroid - Quassel client for Android - * - * Copyright (c) 2018 Janne Koschinski - * Copyright (c) 2018 Ken Børge Viktil - * Copyright (c) 2018 Magnus Fjell - * Copyright (c) 2018 Martin Sandsmark - * Copyright (c) 2018 The Quassel Project - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - plugins { id("com.android.library") kotlin("android") diff --git a/persistence/src/main/java/de/kuschku/quasseldroid/persistence/AccountDatabase.kt b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/AccountDatabase.kt index 3f12044cb..ae7e85f7c 100644 --- a/persistence/src/main/java/de/kuschku/quasseldroid/persistence/AccountDatabase.kt +++ b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/AccountDatabase.kt @@ -73,8 +73,7 @@ abstract class AccountDatabase : RoomDatabase() { database = Room.databaseBuilder( context.applicationContext, AccountDatabase::class.java, DATABASE_NAME - ) - .build() + ).allowMainThreadQueries().build() } } } diff --git a/persistence/src/main/java/de/kuschku/quasseldroid/persistence/LegacyAccountDatabase.kt b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/LegacyAccountDatabase.kt new file mode 100644 index 000000000..963e77f03 --- /dev/null +++ b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/LegacyAccountDatabase.kt @@ -0,0 +1,103 @@ +/* + * Quasseldroid - Quassel client for Android + * + * Copyright (c) 2018 Janne Koschinski + * Copyright (c) 2018 The Quassel Project + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package de.kuschku.quasseldroid.persistence + +import android.arch.persistence.db.SupportSQLiteDatabase +import android.arch.persistence.room.* +import android.arch.persistence.room.migration.Migration +import android.content.Context + +@Database(entities = [(LegacyAccountDatabase.Account::class)], version = 4) +abstract class LegacyAccountDatabase : RoomDatabase() { + abstract fun accounts(): AccountDao + + @Entity + data class Account( + @PrimaryKey + var id: Long, + var host: String, + var port: Int, + var user: String, + var pass: String, + var name: String + ) + + @Dao + interface AccountDao { + @Query("SELECT * FROM account") + fun all(): List<Account> + } + + object Creator { + private var database: LegacyAccountDatabase? = null + + // For Singleton instantiation + private val LOCK = Any() + + fun init(context: Context): LegacyAccountDatabase { + if (database == null) { + synchronized(LOCK) { + if (database == null) { + database = Room.databaseBuilder( + context.applicationContext, + LegacyAccountDatabase::class.java, DATABASE_NAME + ).addMigrations( + object : Migration(0, 1) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("DROP TABLE IF EXISTS cores") + database.execSQL("create table cores (_id integer primary key autoincrement, name text not null, server text not null, port integer not null);") + + database.execSQL("DROP TABLE IF EXISTS user") + database.execSQL("CREATE TABLE user(userid integer primary key autoincrement, username text not null, password text not null, coreid integer not null unique, foreign key(coreid) references cores(_id) ON DELETE CASCADE ON UPDATE CASCADE)") + } + }, + object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("DROP TABLE IF EXISTS cores") + database.execSQL("create table cores (_id integer primary key autoincrement, name text not null, server text not null, port integer not null);") + + database.execSQL("DROP TABLE IF EXISTS user") + database.execSQL("CREATE TABLE user(userid integer primary key autoincrement, username text not null, password text not null, coreid integer not null unique, foreign key(coreid) references cores(_id) ON DELETE CASCADE ON UPDATE CASCADE)") + } + }, + object : Migration(2, 3) { + override fun migrate(database: SupportSQLiteDatabase) = Unit + }, + object : Migration(3, 4) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE IF NOT EXISTS `Account` (`id` INTEGER NOT NULL, `host` TEXT NOT NULL, `port` INTEGER NOT NULL, `user` TEXT NOT NULL, `pass` TEXT NOT NULL, `name` TEXT NOT NULL, PRIMARY KEY(`id`))") + database.execSQL("INSERT INTO Account (id, host, port, user, pass, name) SELECT _id AS id, server, port, coalesce(username, ''), coalesce(password, ''), name FROM cores LEFT JOIN user ON user.coreid = cores._id") + + database.execSQL("DROP TABLE IF EXISTS cores") + database.execSQL("DROP TABLE IF EXISTS user") + } + } + ).allowMainThreadQueries().build() + } + } + } + return database!! + } + } + + companion object { + const val DATABASE_NAME = "data" + } +} diff --git a/viewmodel/build.gradle.kts b/viewmodel/build.gradle.kts index 026e5d4a3..6a24107d6 100644 --- a/viewmodel/build.gradle.kts +++ b/viewmodel/build.gradle.kts @@ -36,28 +36,6 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* - * Quasseldroid - Quassel client for Android - * - * Copyright (c) 2018 Janne Koschinski - * Copyright (c) 2018 Ken Børge Viktil - * Copyright (c) 2018 Magnus Fjell - * Copyright (c) 2018 Martin Sandsmark - * Copyright (c) 2018 The Quassel Project - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3 as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - plugins { id("com.android.library") kotlin("android") -- GitLab