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

FallbackToDestructiveMigration feature #7786

Open
Khudoyshukur opened this issue Mar 14, 2023 · 2 comments
Open

FallbackToDestructiveMigration feature #7786

Khudoyshukur opened this issue Mar 14, 2023 · 2 comments

Comments

@Khudoyshukur
Copy link

When we are using a Room database, we are allowed to use .fallbackToDestructiveMigration() option.
Setting this option, Room tries to execute the migrations we specified. If any error occurs, Room will delete the schema.

How can we achieve this with Realm. .deleteRealmIfMigrationNeeded() is just dropping the database. It just ignores the migration.

@Khudoyshukur
Copy link
Author

I found different workarounds for this in StackOverFlow.
But I am gonna get the right way of doing it from the developers

@cmelchior
Copy link
Contributor

Hi @Khudoyshukur

We don't have explicit support for it right now, but you could just catch the RealmMigrationNeededException and delete the file in that case, i.e. something like this:

val config = RealmConfiguration.Builder()
  .schemaVersion(1)
  .migration(MyMigration())
  .build()

val realm = try {
    Realm.getInstance(config)
} catch(ex: RealmMigrationNeededException) {
    Realm.deleteRealm(config)
    Realm.open(config)
}

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

No branches or pull requests

2 participants