web-dev-qa-db-fra.com

Veuillez fournir une migration dans le générateur ou appeler fallbackToDestructiveMigration dans le générateur, auquel cas Room recréera toutes les tables

J'utilise Room With RxJava2. J'ai ajouté une colonne dans mon tableau afin de migrer vers une nouvelle version. J'ai changé la version de ma base de données en 2.

Voici mon code de migration

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE users "
        +"ADD COLUMN address String");

    }
};

AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DB_NAME)
.addMigrations(MIGRATION_1_2)
.build();

Si vous voulez voir le code complet, Je me réfère à cet exemple sur Github, il n’a pas le code de migration

https://github.com/alahammad/RoomSample

Je suis les étapes décrites dans la documentation, mais mon application se bloque toujours.

Journaux d'erreur

Process: demo.karaoke.sensibol.com.roomrajava2, PID: 13655
    io.reactivex.exceptions.OnErrorNotImplementedException: A migration from 1 to 2 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables.
        at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.Java:704)
        at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.Java:701)
        at io.reactivex.internal.operators.maybe.MaybeCallbackObserver.onError(MaybeCallbackObserver.Java:83)
        at io.reactivex.internal.operators.maybe.MaybeObserveOn$ObserveOnMaybeObserver.run(MaybeObserveOn.Java:99)
        at io.reactivex.Android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.Java:109)
        at Android.os.Handler.handleCallback(Handler.Java:815)
        at Android.os.Handler.dispatchMessage(Handler.Java:104)
        at Android.os.Looper.loop(Looper.Java:194)
        at Android.app.ActivityThread.main(ActivityThread.Java:5643)
        at Java.lang.reflect.Method.invoke(Native Method)
        at Java.lang.reflect.Method.invoke(Method.Java:372)
        at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:960)
        at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:755)
     Caused by: Java.lang.IllegalStateException: A migration from 1 to 2 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables.
        at Android.Arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.Java:82)
        at Android.Arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.Java:118)
        at Android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.Java:256)
        at Android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.Java:163)
        at Android.Arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.Java:93)
        at Android.Arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.Java:54)
        at Android.Arch.persistence.room.RoomDatabase.query(RoomDatabase.Java:193)
        at demo.karaoke.sensibol.com.roomrajava2.UserDao_Impl$4.call(UserDao_Impl.Java:137)
        at demo.karaoke.sensibol.com.roomrajava2.UserDao_Impl$4.call(UserDao_Impl.Java:135)
        at io.reactivex.internal.operators.maybe.MaybeFromCallable.subscribeActual(MaybeFromCallable.Java:46)
        at io.reactivex.Maybe.subscribe(Maybe.Java:3749)
        at io.reactivex.internal.operators.maybe.MaybeSubscribeOn$SubscribeTask.run(MaybeSubscribeOn.Java:54)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.Java:452)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.Java:66)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.Java:57)
        at Java.util.concurrent.FutureTask.run(FutureTask.Java:237)
        at Java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.Java:152)
        at Java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.Java:265)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1112)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:587)
        at Java.lang.Thread.run(Thread.Java:818)
6
Nudge

J'ai lancé votre application à partir de GitHub et effectué un exemple de migration de la version 1 à la version 2. Il s'agit d'une erreur dans une requête SQL. CA devrait etre:

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE users "
        +"ADD COLUMN address TEXT");

    }
};

String> TEXT

Il est également préférable de faire de l'instance de base de données de Room un singleton et de ne l'utiliser que dans la classe Repo/CacheManager. S'il vous plaît vérifier la Gist pour un changement complet du code - https://Gist.github.com/lomza/0f311a1b1e9c896bc58dff925d65eab2

4
lomza

Effacer les données de l'application et désinstaller, puis réinstallez-le, cela fonctionnera. :)

1
Abdul Rizwan