From ed851cb04d1de0a74fc59255e34ba8436d0449e4 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Mon, 23 Oct 2017 21:50:41 +0200 Subject: [PATCH] fixes a crash within TootDao that is caused by an change in the room library --- app/build.gradle | 2 +- .../keylesspalace/tusky/TuskyApplication.java | 1 + .../keylesspalace/tusky/db/AppDatabase.java | 20 +++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 94509f08..cf11a128 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,7 +59,7 @@ dependencies { compile "org.jsoup:jsoup:1.10.3" //room - compile "android.arch.persistence.room:runtime:1.0.0-beta2" + compile "android.arch.persistence.room:runtime:1.0.0-rc1" annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-beta2' testCompile "junit:junit:4.12" diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 86c865b0..26016566 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -54,6 +54,7 @@ public class TuskyApplication extends Application { db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB") .allowMainThreadQueries() + .addMigrations(AppDatabase.MIGRATION_2_3) .build(); } } \ No newline at end of file diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java index c4bea594..28f69da7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -1,14 +1,30 @@ package com.keylesspalace.tusky.db; +import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.room.Database; import android.arch.persistence.room.RoomDatabase; +import android.arch.persistence.room.migration.Migration; /** * DB version & declare DAO */ -@Database(entities = {TootEntity.class}, version = 2, exportSchema = false) +@Database(entities = {TootEntity.class}, version = 3, exportSchema = false) public abstract class AppDatabase extends RoomDatabase { public abstract TootDao tootDao(); -} + + public static final Migration MIGRATION_2_3 = new Migration(2, 3) { + @Override + public void migrate(SupportSQLiteDatabase database) { + //this migration is necessary because of a change in the room library + database.execSQL("CREATE TABLE TootEntity2 (uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, text TEXT, urls TEXT, contentWarning TEXT);"); + + database.execSQL("INSERT INTO TootEntity2 SELECT * FROM TootEntity;"); + database.execSQL("DROP TABLE TootEntity;"); + database.execSQL("ALTER TABLE TootEntity2 RENAME TO TootEntity;"); + + } + }; + +} \ No newline at end of file