Closed devethan closed 1 year ago
@devethan Thank you for your detailed bug report. Your code looks fine but could be it related to https://github.com/facebook/hermes/issues/986? To test it, you could disable Hermes (and use JSC instead)?
@kneth I really appreciate your support🙏
There is one thing I want to check first.
I'm using hermes, and it's been working fine, but from the moment the following code in onMigration
is added, I'm getting an error.
const oldPlans = oldRealm.objects(OldPlan);
When I check the console.log(old.schemaVersion)
without importing the existing object, it's fine.
I still have to use hermes
.
I think it's related to an error in the migration step that gets objects from OldPlan
.
What is your opinion?
We have a test similar to your code.
OldPlan
in the schema
property the configuration, will it work?const oldPlans = oldRealm.objects<oldPlan>(oldPlan.name)
will it work?I hope you can find time to test and answer the two questions. If the answer to the second question is "yes", my guess is that our interacting with TypeScript class models might have some subtle bug which our tests cannot catch.
@kneth Okay, I'm gonna try right now.
@kneth OMG, it works.
const oldPlans = oldRealm.objects<OldPlan>(Plan.name!);
const newPlans = newRealm.objects<Plan>(Plan.name!);
But it didn't work
// Same error
const oldPlans = oldRealm.objects<OldPlan>(OldPlan.name!);
const newPlans = newRealm.objects<Plan>(Plan.name!);
I'm guessing it's related to Typescript, as you say. Maybe the problem is caused by the difference in how JS and TS interpret class objects?
OldPlan.name // 'OldPlan'
Plan.name // 'Plan'
In my opinion, the examples in Modify a Property Type should be updated to look like this
class Person extends Realm.Object<Person> {
_id!: Realm.BSON.ObjectId;
firstName!: string;
lastName!: string;
age!: number;
static schema = {
name: 'Person',
properties: {
// update the data type of '_id' to be 'objectId' within the schema
_id: 'objectId',
firstName: 'string',
lastName: 'string',
},
};
}
// `OldObjectModel` is only used for type injection for `oldRealm` and are not related to the actual schema, `Person`
interface OldObjectModel {
_id: string;
firstName: string;
lastName: string;
age: number;
}
const config: Realm.Configuration = {
schema: [Person],
// increment the 'schemaVersion', since the property type of '_id'
// has been modified
schemaVersion: 2,
onMigration: (oldRealm: Realm, newRealm: Realm) => {
if (oldRealm.schemaVersion < 2) {
const oldObjects = oldRealm.objects<OldObjectModel>(Person.name!); // OR 'Person'
const newObjects = newRealm.objects<Person>(Person.name!); // OR 'Person'
// loop through all objects and set the _id property
// in the new schema
for (const objectIndex in oldObjects) {
const oldObject = oldObjects[objectIndex];
const newObject = newObjects[objectIndex];
newObject._id = new Realm.BSON.ObjectId(oldObject._id);
}
}
},
};
// Pass the configuration object with the updated
// 'schemaVersion' and 'migration' function to createRealmContext()
const {RealmProvider} = createRealmContext(config);
Without change those scheme, like this
...
const oldObjects = oldRealm.objects<OldObjectModel>(OldObjectModel.scheme.name);
const newObjects = newRealm.objects<Person>(Person.scheme.name);
...
@devethan It is great to see some progress. We should investigate the differences between TypeScript and JavaScript.
I will ask the documentation team to take a look at your suggestion.
Thank you, @devethan! Definitely an issue with the docs. I apologize for sending you down this long path. I'll update this docs sample ASAP. And set aside some time to re-evaluate all the TypeScript samples just in case there are more issues like this elsewhere in the docs.
Hi @kneth @krollins-mdb
I have an opinion about the document. I got an error during migration related toschemaVersion
is 0
.
But no document said that schemaVersion
starts with 0
. I just thought that schemaVersion
starts with 1
.
Because there's only example that migrate schemaVersion
to 2
from 1
How about saying in the document that "initial schemaVersion is 0
"
I think it's helpful for the relam newbie.
@devethan, that's excellent feedback - thank you! I have a PR in progress right now to fix the example. I'll update to have it go from 0 > 1.
@devethan, changes haven't been deployed to the live docs site yet, but the PR is merged! Thanks again for providing your feedback and helping to make the docs better.
The docs change has been merged, and I am closing the issue. Feel free to create a new issue if you experience issues with migration.
How frequently does the bug occur?
Always
Description
The below error always occur when I try to migrate type of date to string.
And I got this message from issue navigator on Xcode
I've tested various migration cases, but I got the same error every time I did a migration that changed types. Here's I reffered for the migration
I've also tried to clean up cache on yarn and watchman
migration codes
Screenshot on Xcode
Screenshot on simulator
pacakge.json
Stacktrace & log output
Can you reproduce the bug?
Always
Reproduction Steps
onMigration
functionyarn ios
or Run build on XcodeVersion
"realm": "11.9.0", "@realm/react": "^0.4.3"
What services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
macOS 13.4, iOS 16.4
Build environment
Cocoapods version
1.12.0