RoomAsset icon indicating copy to clipboard operation
RoomAsset copied to clipboard

migrate to newer db

Open indraAnisa opened this issue 5 years ago • 23 comments

i try to update my db file with new data, and update database version to 3. but the data is still from the old db. how to achieve this?

indraAnisa avatar Aug 13 '18 06:08 indraAnisa

You only need to update the version. I will try to make a migration to make sure it's not a library bug.

On Mon, Aug 13, 2018, 8:31 AM Indra Anisa [email protected] wrote:

i try to update my db file with new data, and update database version to 3. but the data is still from the old db. how to achieve this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/humazed/RoomAsset/issues/13, or mute the thread https://github.com/notifications/unsubscribe-auth/AIbcJrJzeBlzjtvAPu7UmYO-ISFoZ76pks5uQR04gaJpZM4V58-V .

humazed avatar Aug 14 '18 07:08 humazed

I have a similar issue. I update the DB data upon every release, but would rather not change the version number unless the schema has changed. Previously I was using android-sqlite-asset-helper, which allowed the db to be overwritten upon every app upgrade. Does such a feature exist?

four01 avatar Aug 19 '18 22:08 four01

The lib uses android-sqlite-asset-helper under the hood I will look at supporting this.

I find the problem and I will release a new version with a fix.

humazed avatar Aug 21 '18 19:08 humazed

Any news about this? It's quite important to be able to update the database.

I tried with fallbackToDestructiveMigrationFrom and it only deleted the old database without getting the new database.

kusan-thana avatar Sep 18 '18 16:09 kusan-thana

I had no free time lately but I will start working on it actively soon -1 or 2 days- and hopefully fix this bug.

humazed avatar Sep 18 '18 21:09 humazed

My guess is that when you migrate with fallbackToDestructiveMigrationFrom, it doesn't really delete the db file. It only drops and regenerates all the tables and it prevents RoomAsset from loading the new db file from assets folder.

Any news about this? It's quite important to be able to update the database.

I tried with fallbackToDestructiveMigrationFrom and it only deleted the old database without getting the new database.

alierdogan7 avatar Sep 18 '18 23:09 alierdogan7

i just manually delete data on table and read and loop sql file on migration.

indraAnisa avatar Sep 19 '18 01:09 indraAnisa

@indraAnisa You're doing that manually? Make the use of the library useless then, no?

@humazed I don't know much about sqlite, but maybe this option can work: https://github.com/jgilfelt/android-sqlite-asset-helper#upgrades-via-overwrite

kusan-thana avatar Sep 19 '18 09:09 kusan-thana

@indraAnisa You're doing that manually? Make the use of the library useless then, no?

yes, the performance is not bad though, about 1 second for 8K row sql insert.

indraAnisa avatar Sep 19 '18 09:09 indraAnisa

I had no free time lately but I will start working on it actively soon -1 or 2 days- and hopefully fix this bug.

I'm waiting for this fix too. Will you be working on this issue soon?

soundeos avatar Oct 12 '18 07:10 soundeos

unfortunately, I have no free time lately to fix this issue but PRs are welcomed.

humazed avatar Oct 15 '18 06:10 humazed

I'm still facing this issue, do you have any solution for this ?

KulwinderSinghRahal avatar Mar 23 '19 02:03 KulwinderSinghRahal

Any news about this? It's quite important to be able to update the database.

"Quite important" is quite an understatement..

getsadzeg avatar May 05 '19 22:05 getsadzeg

Updating the Database still not possible, please provide a fix (the old database needs to be deleted and the new one copied)

ueen avatar May 24 '19 11:05 ueen

I made a fork with destructive upgrading, that should be fine for most use cases, if you need something else, feel free to take a look at SQLiteAssetHelper to modify further. https://github.com/ueen/RoomAsset

ueen avatar May 24 '19 21:05 ueen

@ueen Using Room's addMigrations method, right?

getsadzeg avatar May 26 '19 14:05 getsadzeg

Rooms fallbackToDestructiveMigtation simply drops all rows, addMigration performs actions you specify to convert the rows to the new schema, in this case we only need to tell Room, that the database has changed (overwritten with the new one) and therefore raise the version and in order to do this we need to provide a migration strategy, but as we don't want Room to actually do anything (the database is already overwritten), so we need to set an empty addMigration for the version raise. This only works with my fork tough as humazed code doesn't overwrite anything, it only instantiates the database once.

ueen avatar May 26 '19 19:05 ueen

@ueen

I am getting this crash by using your fork

Unable to create application .ApplicationClass: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer

on this line

appDatabase = RoomAsset.databaseBuilder(getApplicationContext(), AppDatabase.class, "dbname",3). addMigrations(MIGRATION_1_2,MIGRATION_2_3).allowMainThreadQueries().build();

Can you please look into this?

rushabhshah065 avatar Jun 03 '19 18:06 rushabhshah065

First of all don't use queries on the main thread, that's considered bad practice, secondly you need to add the fileextension (if your file has one, which it should), and most importantly there seems to be an issue with your migrations, please refer to my forks documentation on how to add a new migration (sample at the bottom). And finally, please open an issue on my fork if problems persist, and don't spam this issue :)

ueen avatar Jun 03 '19 19:06 ueen

@ueen

I am getting this crash by using your fork

Unable to create application .ApplicationClass: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer

appDatabase = RoomAsset.databaseBuilder(getApplicationContext(), AppDatabase.class, "dbname.db",3). addMigrations(MIGRATION_1_2,MIGRATION_2_3).allowMainThreadQueries().build();

My migration class

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Since we didn't alter the table, there's nothing else to do here.
    }
};

static final Migration MIGRATION_2_3 = new Migration(2, 3) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Since we didn't alter the table, there's nothing else to do here.
    }
};

I have already file extension and migration as needed in my code. Until it is crashing. And please allow to open issue in your fork publicly first.

rushabhshah065 avatar Jun 04 '19 07:06 rushabhshah065

alright, enabled issues, do you have the latest version, what does the stack trace look like -> where exactly is the exception thrown?

ueen avatar Jun 04 '19 08:06 ueen

@ueen Using Room's addMigrations method, right?

updated my fork implementation 'com.github.ueen:RoomAsset:1.2.1', manually addMigration is no longer necessary! Just update the version number of the database and in the databaseBuilder and https://github.com/ueen/RoomAsset takes care of everything else :)

ueen avatar Jun 05 '19 23:06 ueen

Created a new library that allows you to keep specific columns (eg for userdata) while still updating your database, please have a look https://github.com/ueen/RoomAssetHelper

ueen avatar Jan 09 '20 13:01 ueen