Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete/recreate realm if migration failed #4552

Closed
semenoh opened this issue Apr 25, 2017 · 9 comments
Closed

delete/recreate realm if migration failed #4552

semenoh opened this issue Apr 25, 2017 · 9 comments
Assignees
Labels

Comments

@semenoh
Copy link

semenoh commented Apr 25, 2017

It's easy to delete realm when migration needed in config. But I'd like to do a migration and delete realm only if migration failed.
I've tried to wrap migration code in try/catch block but realm.deleteAll() does not do the trick.

Please provide some documentation on this.

And BTW. Do the migration is performed in transaction ? Sometimes it looks like failed migration broke the data so fixed migration does not help...

Thanks in advance

@cmelchior
Copy link
Contributor

Something like this in a wrapper method should work:

        Realm realm;
        RealmConfiguration config = getConfig();
        try {
            realm = Realm.getInstance(config); // Will migrate if needed
        } catch (RealmMigrationNeededException e) {
            Realm.deleteRealm(config);
            realm = Realm.getInstance(config);
        }
        return realm;

In the future, this kind of questions are better asked on Stack Overflow where more people will be able to answer it.

@jpmcosta
Copy link

jpmcosta commented Oct 25, 2017

I'm trying to implement what @cmelchior mentioned:

Realm realm;
RealmConfiguration config = getConfig();
try {
    realm = Realm.getInstance(config); // Will migrate if needed
} catch (RealmMigrationNeededException e) {
    Realm.deleteRealm(config);
    realm = Realm.getInstance(config);
}
return realm;

However, I'm consistently getting the following exception when the migration fails:
java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file: <file>

I've simplified my workflow to make sure it's the only instance I'm trying to open.

@RemyTabardel
Copy link

Is there a solution for that ?
I've same IllegalStateException, i just call Realm.init() before try/catch as @cmelchior and deleteRealm throw this exception.

@Zhuinden
Copy link
Contributor

Zhuinden commented Nov 22, 2017

You can also try my slightly experimental lib called RealmAutoMigration, it works for Realm 4.x+ if you annotate your RealmList fields with @RealmAutoMigration.MigratedList.

Make sure you return; out (or apply transform() afterwards, but still ignore the later versions) of the migration once applied, because it uses your latest models.

@RemyTabardel
Copy link

I can't really use experimental lib in professional project i think.. =x
I updated my realm from 3.0.0 to 4.1.0 directly, so i tried to come back to 3.0.0 and i have not this IllegalStateException with this release.

Maybe i missed something in changelog..

@Zhuinden
Copy link
Contributor

Zhuinden commented Nov 22, 2017

Technically it's "experimental" only because I was lazy to write automated integration tests, but otherwise it works

Either way, if nothing works, you can check the current schema version without opening a typed realm using DynamicRealm.getInstance(), and if the schema version is below a certain number, you can close the dyn realm and call deleteRealm

@RemyTabardel
Copy link

I tried to check with DynamicRealm and it's working yes.
I'll take a look about your lib, i don't want to check if i need to delete base every schema update.

Thanks @Zhuinden !

@AamirAbro
Copy link

is there any other way to fix it except for auto migration?

@AamirAbro
Copy link

the solution by @jpmcosta used to work this a new regression in 4.1.1 as far as I can tell.
can we treat as a regression issue and fix this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants