Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room - auto increment @database version number?

During development my Room db schema is very volatile. Every time I do any change to the schema I need to update my version number, like this;

@Database(version = 27,
    entities = {MyClass.class})

public abstract class MyDatabase extends RoomDatabase

I am using fallbackToDestructiveMigration too;

Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

Is there any way to avoid updating the version number for each "little" change? As I said, things are quite volatile right now.

like image 934
ausgeorge Avatar asked Feb 15 '18 04:02

ausgeorge


1 Answers

Deleteing the database would result in it being initialised/built each time the App is run.

You could do this using :-

context.getApplicationContext().deleteDatabase(SystemStrings.ROOM_DATABASE_NAME); //<<<< ADDED before building Database.
//Your existing code follows 
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

Of course you would not have this in the real app

like image 62
MikeT Avatar answered Oct 30 '22 19:10

MikeT