Skip to content

Commit

Permalink
Test list migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kneth committed Aug 21, 2023
1 parent 33394ca commit 21c994f
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions integration-tests/tests/src/tests/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import Realm from "realm";
import { expect } from "chai";
import { DogSchema, IDog, IPerson, PersonSchema } from "../schemas/person-and-dogs";
import { BSON } from "realm/dist/bundle";

const TestSchema = {
name: "Test",
Expand Down Expand Up @@ -561,6 +562,10 @@ describe("Migrations", () => {
const child2 = realm.create(ChildSchema.name, { value: 22 });
realm.create(ParentSchema.name, { id: 1, child: child1 });
realm.create(ParentSchema.name, { id: 2, child: child2 });

const child3 = realm.create(ChildSchema.name, { value: 33 });
const child4 = realm.create(ChildSchema.name, { value: 44 });
const child5 = realm.create(ChildSchema.name, { value: 44 });
});

realm.write(() => {
Expand Down Expand Up @@ -592,4 +597,97 @@ describe("Migrations", () => {
);
});
});

describe("Migrate list elements to embedded objects", () => {
const ParentSchema = {
name: "ParentSchema",
primaryKey: "id",
properties: {
id: "int",
children: "Child[]",
},
};

interface Parent {
id: number;
children: Realm.List<Child>;
}

interface Child {
value: number;
}

const ChildSchema = {
name: "Child",
embedded: false,
properties: {
value: "int",
},
};

const EmbeddedChildSchema = {
name: "Child",
embedded: true,
properties: {
value: "int",
},
};

beforeEach(() => {
Realm.clearTestState();

const realm = new Realm({
schema: [ParentSchema, ChildSchema],
schemaVersion: 1,
});

realm.write(() => {
const child1 = realm.create(ChildSchema.name, { value: 11 });
const child2 = realm.create(ChildSchema.name, { value: 22 });
realm.create(ParentSchema.name, { id: 1, children: [child1, child2] });

const child3 = realm.create(ChildSchema.name, { value: 33 });
const child4 = realm.create(ChildSchema.name, { value: 44 });
const child5 = realm.create(ChildSchema.name, { value: 44 });
realm.create(ParentSchema.name, { id: 2, children: [child3, child4, child5] });

realm.create(ParentSchema.name, { id: 3, children: [child1, child5] });
});

realm.write(() => {
realm.delete(realm.objectForPrimaryKey(ParentSchema.name, 1));
});

realm.close();
});

it("migration should delete orphan objects", () => {
const realm = new Realm({
schema: [ParentSchema, EmbeddedChildSchema],
schemaVersion: 2,
automaticallyHandleBacklinksInMigration: true,
});
const parents = realm.objects<Parent>(ParentSchema.name);
expect(parents.length).equals(2);

const parent2 = realm.objectForPrimaryKey<Parent>(ParentSchema.name, 2);
const parent3 = realm.objectForPrimaryKey<Parent>(ParentSchema.name, 3);
expect(parent2?.children.length).equals(3);
expect(parent3?.children.length).equals(2);
expect(parent2?.children[2].value).equals(parent3?.children[1].value); // TODO: value -> _objectKey()
realm.close();
});

it("orphan objects lead to exception", () => {
expect(() => {
new Realm({
schema: [ParentSchema, EmbeddedChildSchema],
schemaVersion: 2,
automaticallyHandleBacklinksInMigration: false,
});
}).to.throw(
`Cannot convert '${EmbeddedChildSchema.name}' to embedded: at least one object has no incoming links and would be deleted.`,
);
});
});
});

0 comments on commit 21c994f

Please sign in to comment.