Skip to content

Commit

Permalink
Add update schema functionality (#5003)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik authored Oct 12, 2022
1 parent c1a3712 commit 6f49ce1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/bindgen/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ classes:
read_group: () -> Group&
duplicate: () -> TransactionRef

update_schema: '(schema: std::vector<ObjectSchema>, version: uint64_t, migration_function: Nullable<std::function<(old_realm: SharedRealm, new_realm: SharedRealm, new_schema_handle: Schema&) -> void>>, initialization_function: Nullable<std::function<(realm: SharedRealm) -> void>>, in_transaction: bool) -> void'
enable_wait_for_change: ()
wait_for_change: () -> bool
wait_for_change_release: ()
Expand Down
10 changes: 8 additions & 2 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,14 @@ export class Realm {
throw new Error("Not yet implemented");
}

_updateSchema(): unknown {
throw new Error("Not yet implemented");
_updateSchema(schema: Realm.ObjectSchema[]): void {
const normalizedSchema = schema && normalizeRealmSchema(schema);
const bindingSchema = normalizedSchema && toBindingSchema(normalizedSchema);
if (!this.isInTransaction) {
throw new Error("Can only create object schema within a transaction.");
}
this.internal.updateSchema(bindingSchema, this.internal.schemaVersion + 1n, null, null, true);
this.classes = new ClassMap(this, this.internal.schema, this.schemaExtras);
}

/**
Expand Down
51 changes: 51 additions & 0 deletions packages/realm/src/tests/updateschema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { ObjectSchema, Realm } from "../index";
import { expect } from "chai";

import { closeRealm, generateTempRealmPath, RealmContext } from "./utils";

const oldSchema: ObjectSchema[] = [{ name: "Person", properties: { name: "string", age: "int" } }];
const newSchema: ObjectSchema[] = [{ name: "Person2", properties: { title: "string", friends: "int" } }];

describe("realm._updateSchema", () => {
beforeEach(function (this: RealmContext) {
this.realm = new Realm({
path: generateTempRealmPath(),
inMemory: true,
schema: oldSchema,
});
});
after(closeRealm);

it("can add new schema classes", function (this: RealmContext) {
this.realm.write(() => {
this.realm._updateSchema(newSchema);
this.realm.create("Person2", { title: "Good", friends: 3 });
});
});
it("can only be called in a transaction", function (this: RealmContext) {
this.realm = new Realm({
path: generateTempRealmPath(),
inMemory: true,
schema: [{ name: "Person", properties: { name: "string", age: "int" } }],
});
expect(() => this.realm._updateSchema(newSchema)).throw("Can only create object schema within a transaction.");
});
});

0 comments on commit 6f49ce1

Please sign in to comment.