realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

Schema version is increased before Realm is initialized causing migration block to not run #8661

Open bgoncal opened 3 months ago

bgoncal commented 3 months ago

How frequently does the bug occur?

Always

Description

I need to run the migration block of my Realm from schema 27 to 29, it works flawless in iOS, but in watchOS as soon as the app try to access Realm, schemaVersionAtURL() already returns 29 instead of 27, causing the migration to not run.

I already tried to delete the app in both devices, reboot devices install old version of the app (with schema 27) and upgrading it to the app version with schema 29.

What could cause the schema version to be 29 already even though Realm was not initialized before?

Luckily I am working on an open source project so here it is where I initialise Realm (line 37): https://github.com/home-assistant/iOS/blob/master/Sources/Shared/Common/Extensions/Realm%2BInitialization.swift

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

10.35 - 10.52.2

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 17.4 and watchOS 10.4

Build environment

Xcode version: 15.4 Dependency manager and version: Cocoapods

sync-by-unito[bot] commented 3 months ago

➤ PM Bot commented:

Jira ticket: RCOCOA-2417

bgoncal commented 3 months ago

Also, after the watch app crashes because "migration is required", the schema version is bumped anyways, so if I try to run a version that uses schema 27 afterwards it also crashes saying "27 is lower than 29".

bgoncal commented 3 months ago

Ok, I may have found the root cause:

Can someone coment on that?

nirinchev commented 3 months ago

The migration callback should be invoked when you change the schema version, even if the models are the same.

bgoncal commented 3 months ago

Well... it didn't happen, it only started working after I removed object types from the initialization.

bgoncal commented 3 months ago

Or you mean manually called?