From 45e0add99c46006a5177b1a517c3b50ec9711d77 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 16 Feb 2022 20:26:57 +0200 Subject: [PATCH 01/23] Configuration isReadOnly implemented --- lib/src/configuration.dart | 11 +++++--- lib/src/native/realm_core.dart | 12 ++++++++- test/realm_test.dart | 46 +++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 45a4ecd9a..c97a207c2 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -53,7 +53,7 @@ class Configuration { } /// The platform dependent path to the default realm file - `default.realm`. - /// + /// /// If set it should contain the name of the realm file. Ex. /mypath/myrealm.realm static String get defaultPath => _defaultPath ??= _initDefaultPath(); static set defaultPath(String value) => _defaultPath = value; @@ -83,10 +83,15 @@ class Configuration { /// If omitted the [defaultPath] for the platform will be used. String get path => realmCore.getConfigPath(this); set path(String value) => realmCore.setConfigPath(this, value); + + /// Gets or sets a value indicating whether a [Realm] is opened as readonly. This allows opening it + /// from locked locations such as resources, bundled with an application. + bool get isReadOnly => realmCore.getReadOnlyMode(this); + set isReadOnly(bool value) => realmCore.setReadOnlyMode(this, value); } /// A collection of properties describing the underlying schema of a [RealmObject]. -/// +/// /// {@category Configuration} class SchemaObject { /// Schema object type. @@ -103,7 +108,7 @@ class SchemaObject { } /// Describes the complete set of classes which may be stored in a `Realm` -/// +/// /// {@category Configuration} class RealmSchema extends Iterable { ///@nodoc diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 443044509..6fad3c09e 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -152,6 +152,16 @@ class _RealmCore { _realmLib.realm_config_set_schema_version(config.handle._pointer, version); } + bool getReadOnlyMode(Configuration config) { + int mode = _realmLib.realm_config_get_schema_mode(config.handle._pointer); + return mode == realm_schema_mode.RLM_SCHEMA_MODE_READ_ONLY; + } + + void setReadOnlyMode(Configuration config, bool value) { + int mode = value ? realm_schema_mode.RLM_SCHEMA_MODE_READ_ONLY : realm_schema_mode.RLM_SCHEMA_MODE_AUTOMATIC; + _realmLib.realm_config_set_schema_mode(config.handle._pointer, mode); + } + ConfigHandle createConfig() { final configPtr = _realmLib.realm_config_new(); return ConfigHandle._(configPtr); @@ -461,7 +471,7 @@ class _RealmCore { return _realmLib.realm_object_is_valid(object.handle._pointer); } - bool listIsValid(RealmList list) { + bool listIsValid(RealmList list) { return _realmLib.realm_list_is_valid(list.handle._pointer); } } diff --git a/test/realm_test.dart b/test/realm_test.dart index 9fa02d9f8..3c8d8ec17 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -194,6 +194,51 @@ Future main([List? args]) async { config.schemaVersion = 3; expect(config.schemaVersion, equals(3)); }); + + test('Configuration after isReadOnly can not open Realm', () { + Configuration config = Configuration([Car.schema]); + config.isReadOnly = true; + expect(() => Realm(config), throws("Error code: 19 . Message: No such table exists")); + }); + + test('Configuration after isReadOnly can read', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + var car = Car("Mustang"); + realm.write(() => realm.add(car)); + config.isReadOnly = true; + var cars = realm.all(); + realm.close(); + }); + + test('Configuration after isReadOnly can not write', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + config.isReadOnly = true; + var car = Car("Mustang"); + expect(() => realm.write(() => realm.add(car)), throws); + realm.close(); + }); + + test('Configuration after isReadOnly can not be edited', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + var car = Car("Mustang"); + realm.write(() => realm.add(car)); + config.isReadOnly = true; + expect(() => realm.write(() => car.make = "Opel"), throws); + realm.close(); + }); + + test('Configuration after isReadOnly can not delete', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + var car = Car("Mustang"); + realm.write(() => realm.add(car)); + config.isReadOnly = true; + expect(() => realm.write(() => realm.delete(car)), throws); + realm.close(); + }); }); group('RealmClass tests:', () { @@ -1496,7 +1541,6 @@ Future main([List? args]) async { realm.close(); }); - test('Realm adding objects graph', () { var studentMichele = Student(1) ..name = "Michele Ernesto" From 0e736d90f5eccabfd94d04cf8851a99d74db4ec8 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 17 Feb 2022 15:46:51 +0200 Subject: [PATCH 02/23] Configuration read-only tests added + documentation --- lib/src/configuration.dart | 9 +++- test/realm_test.dart | 89 ++++++++++++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 15 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index c97a207c2..2043c7463 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -36,11 +36,14 @@ class Configuration { RealmSchema get schema => _schema; /// Creates a [Configuration] with schema objects for opening a [Realm]. - Configuration(List schemaObjects) + /// Read-only configuration could be used for opening [Realm] only for reading data in case its file is locked. + /// Realm file must exist before to reopen [Realm] in read-only mode. + Configuration(List schemaObjects, {bool readOnly = false}) : _schema = RealmSchema(schemaObjects), _handle = realmCore.createConfig() { schemaVersion = 0; path = defaultPath; + if (readOnly) isReadOnly = readOnly; realmCore.setSchema(this); } @@ -86,6 +89,10 @@ class Configuration { /// Gets or sets a value indicating whether a [Realm] is opened as readonly. This allows opening it /// from locked locations such as resources, bundled with an application. + /// + /// Realm can not be opened in read-only mode if Realm files do not exist. + /// Opening Realm in read-only mode can be done after reopening existing Realm with read-only configuration. + /// First opening of Realm if configuration is read-only will throw an exception. bool get isReadOnly => realmCore.getReadOnlyMode(this); set isReadOnly(bool value) => realmCore.setReadOnlyMode(this, value); } diff --git a/test/realm_test.dart b/test/realm_test.dart index 3c8d8ec17..3276c6dab 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -195,48 +195,109 @@ Future main([List? args]) async { expect(config.schemaVersion, equals(3)); }); - test('Configuration after isReadOnly can not open Realm', () { + test('Configuration isReadOnly property - realm files must exist', () { Configuration config = Configuration([Car.schema]); config.isReadOnly = true; - expect(() => Realm(config), throws("Error code: 19 . Message: No such table exists")); + //You cannot open Realm in read-only mode if Realm files do not exist. + //Files are created the first time Realm is opened. + //Opening Realm in read-only mode can be done after reopening existing Realm with read-only configuration. + //First opening of Realm if configuration is read-only will throw an exception. + expect(() => Realm(config), throws("Message: No such table exists")); }); - test('Configuration after isReadOnly can read', () { + test('Configuration readOnly argument - realm files must exist', () { + Configuration config = Configuration([Car.schema], readOnly: true); + expect(() => Realm(config), throws("Message: No such table exists")); + }); + + test('Configuration read-only - open existing realm with read-only config', () { + //Create realm and its files Configuration config = Configuration([Car.schema]); var realm = Realm(config); - var car = Car("Mustang"); - realm.write(() => realm.add(car)); + realm.close(); + + //Reconfigure existing realm for read-only mode. + config = Configuration([Car.schema], readOnly: true); + realm = Realm(config); + realm.close(); + }); + + test('Configuration read-only - reading is possible', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + realm.write(() => realm.add(Car("Mustang"))); + realm.close(); + + //Reconfigure existing realm for read-only mode. config.isReadOnly = true; + realm = Realm(config); var cars = realm.all(); realm.close(); }); - test('Configuration after isReadOnly can not write', () { + test('Configuration read-only - writing on read-only Realms throws', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); - config.isReadOnly = true; - var car = Car("Mustang"); - expect(() => realm.write(() => realm.add(car)), throws); + realm.close(); + + //Reconfigure existing realm for read-only mode. + config = Configuration([Car.schema], readOnly: true); + realm = Realm(config); + expect(() => realm.write(() => realm.add(Car("Mustang"))), throws("Can't perform transactions on read-only Realms.")); realm.close(); }); - test('Configuration after isReadOnly can not be edited', () { + test('Configuration read-only - editing realm objects throws', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); var car = Car("Mustang"); realm.write(() => realm.add(car)); + realm.close(); + config.isReadOnly = true; - expect(() => realm.write(() => car.make = "Opel"), throws); + realm = Realm(config); + expect(() => realm.write(() => car.make = "Opel"), throws("Can't perform transactions on read-only Realms.")); realm.close(); }); - test('Configuration after isReadOnly can not delete', () { + test('Configuration read-only - deleting realm object throws', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); var car = Car("Mustang"); realm.write(() => realm.add(car)); - config.isReadOnly = true; - expect(() => realm.write(() => realm.delete(car)), throws); + realm.close(); + + config = Configuration([Car.schema], readOnly: true); + realm = Realm(config); + expect(() => realm.write(() => realm.delete(car)), throws("Can't perform transactions on read-only Realms.")); + realm.close(); + }); + + test('Configuration read-only - deleting realm results throws', () { + Configuration config = Configuration([Car.schema]); + var realm = Realm(config); + realm.write(() => realm.addAll([Car("Mustang"), Car("Opel")])); + realm.close(); + + config = Configuration([Car.schema], readOnly: true); + realm = Realm(config); + var cars = realm.all(); + expect(() => realm.write(() => realm.deleteMany(cars)), throws("Can't perform transactions on read-only Realms.")); + realm.close(); + }); + + test('Configuration read-only - changing realm lists throws', () { + Configuration config = Configuration([Person.schema, Team.schema]); + var realm = Realm(config); + realm.write(() => realm.add(Team("Boston")..players.addAll([Person("N.M"), Person("K.L")]))); + realm.close(); + + config = Configuration([Person.schema, Team.schema], readOnly: true); + realm = Realm(config); + var team = realm.find("Boston"); + expect(() => realm.write(() => team!.players.clear()), throws("Can't perform transactions on read-only Realms.")); + expect(() => realm.write(() => team!.players.add(Person("O.P"))), throws("Can't perform transactions on read-only Realms.")); + expect(() => realm.write(() => realm.deleteMany(team!.players)), throws("Can't perform transactions on read-only Realms.")); realm.close(); }); }); From 882796615227dd64e71830849dc67982b0a297ea Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 17 Feb 2022 15:52:06 +0200 Subject: [PATCH 03/23] CHANGELOG updated --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b40fce67e..934d7081c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ vNext ### Changes * Added support for isValid() on RealmList and RealmObject. ([#183](https://github.com/realm/realm-dart/pull/183)) +* Added support for opening Realm in read-only mode. ([#260](https://github.com/realm/realm-dart/pull/260)) ### Enhancements * Support query on lists of realm objects From 1bc9fe99e5dbb55bb1fa19b1153024fc8931c8f2 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:50:36 +0200 Subject: [PATCH 04/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 2043c7463..849f53106 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -43,7 +43,9 @@ class Configuration { _handle = realmCore.createConfig() { schemaVersion = 0; path = defaultPath; - if (readOnly) isReadOnly = readOnly; + if (readOnly) { + isReadOnly = true; + } realmCore.setSchema(this); } From 2929f9c794cf80cbbedd386911c75c5cc15f1f4b Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:41:28 +0200 Subject: [PATCH 05/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 849f53106..4f36792e3 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -36,7 +36,7 @@ class Configuration { RealmSchema get schema => _schema; /// Creates a [Configuration] with schema objects for opening a [Realm]. - /// Read-only configuration could be used for opening [Realm] only for reading data in case its file is locked. + /// [readOnly] controls whether a `Realm` is opened as readonly. This allows opening it from locked locations such as resources, bundled with an application. The realm file must already exists. /// Realm file must exist before to reopen [Realm] in read-only mode. Configuration(List schemaObjects, {bool readOnly = false}) : _schema = RealmSchema(schemaObjects), From 2a8aa0735b0f5425772867a026f48b073c5603a7 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:42:51 +0200 Subject: [PATCH 06/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 4f36792e3..f5879823b 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -37,7 +37,6 @@ class Configuration { /// Creates a [Configuration] with schema objects for opening a [Realm]. /// [readOnly] controls whether a `Realm` is opened as readonly. This allows opening it from locked locations such as resources, bundled with an application. The realm file must already exists. - /// Realm file must exist before to reopen [Realm] in read-only mode. Configuration(List schemaObjects, {bool readOnly = false}) : _schema = RealmSchema(schemaObjects), _handle = realmCore.createConfig() { From 07210b74c40eea030fea826b2e5965734f22c513 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:43:39 +0200 Subject: [PATCH 07/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index f5879823b..815f3bd26 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -92,7 +92,7 @@ class Configuration { /// from locked locations such as resources, bundled with an application. /// /// Realm can not be opened in read-only mode if Realm files do not exist. - /// Opening Realm in read-only mode can be done after reopening existing Realm with read-only configuration. + /// First opening of Realm if configuration is read-only will throw an exception. bool get isReadOnly => realmCore.getReadOnlyMode(this); set isReadOnly(bool value) => realmCore.setReadOnlyMode(this, value); From d2b65f4b330aa79665222afae2bc9719f3feb136 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:43:52 +0200 Subject: [PATCH 08/23] Update test/realm_test.dart Co-authored-by: blagoev --- test/realm_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 3276c6dab..581da5162 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -240,7 +240,7 @@ Future main([List? args]) async { var realm = Realm(config); realm.close(); - //Reconfigure existing realm for read-only mode. + config = Configuration([Car.schema], readOnly: true); realm = Realm(config); expect(() => realm.write(() => realm.add(Car("Mustang"))), throws("Can't perform transactions on read-only Realms.")); From 777801439ec0d1ac53e2a926e816499599681b40 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:46:58 +0200 Subject: [PATCH 09/23] Update test/realm_test.dart Co-authored-by: blagoev --- test/realm_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 581da5162..5aa35e38c 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -243,7 +243,7 @@ Future main([List? args]) async { config = Configuration([Car.schema], readOnly: true); realm = Realm(config); - expect(() => realm.write(() => realm.add(Car("Mustang"))), throws("Can't perform transactions on read-only Realms.")); + expect(() => realm.write(() => {})), throws("Can't perform transactions on read-only Realms.")); realm.close(); }); From cc752978fdf510ce32d8e757e34a297fff90ecd0 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:47:26 +0200 Subject: [PATCH 10/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 815f3bd26..3efb0e662 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -91,7 +91,7 @@ class Configuration { /// Gets or sets a value indicating whether a [Realm] is opened as readonly. This allows opening it /// from locked locations such as resources, bundled with an application. /// - /// Realm can not be opened in read-only mode if Realm files do not exist. + /// The realm file must already exists at [path] /// First opening of Realm if configuration is read-only will throw an exception. bool get isReadOnly => realmCore.getReadOnlyMode(this); From 72fdbff12f650f2ca32183c585a7a41ba64d0838 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:48:44 +0200 Subject: [PATCH 11/23] Update lib/src/configuration.dart Co-authored-by: blagoev --- lib/src/configuration.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 3efb0e662..59f9475c0 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -93,7 +93,7 @@ class Configuration { /// /// The realm file must already exists at [path] - /// First opening of Realm if configuration is read-only will throw an exception. + bool get isReadOnly => realmCore.getReadOnlyMode(this); set isReadOnly(bool value) => realmCore.setReadOnlyMode(this, value); } From d586b6fc7f56e9dffa293de87a961de72fc3e2ef Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:49:11 +0200 Subject: [PATCH 12/23] Update lib/src/native/realm_core.dart Co-authored-by: blagoev --- lib/src/native/realm_core.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 6fad3c09e..87c6e5b83 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -152,7 +152,7 @@ class _RealmCore { _realmLib.realm_config_set_schema_version(config.handle._pointer, version); } - bool getReadOnlyMode(Configuration config) { + bool getConfigReadOnly(Configuration config) { int mode = _realmLib.realm_config_get_schema_mode(config.handle._pointer); return mode == realm_schema_mode.RLM_SCHEMA_MODE_READ_ONLY; } From 88649a9ba74625a18a43b23ef797435de7eadef5 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:49:50 +0200 Subject: [PATCH 13/23] Update lib/src/native/realm_core.dart Co-authored-by: blagoev --- lib/src/native/realm_core.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 87c6e5b83..c435efaa8 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -157,7 +157,7 @@ class _RealmCore { return mode == realm_schema_mode.RLM_SCHEMA_MODE_READ_ONLY; } - void setReadOnlyMode(Configuration config, bool value) { + void setConfigReadOnly(Configuration config, bool value) { int mode = value ? realm_schema_mode.RLM_SCHEMA_MODE_READ_ONLY : realm_schema_mode.RLM_SCHEMA_MODE_AUTOMATIC; _realmLib.realm_config_set_schema_mode(config.handle._pointer, mode); } From bac56c021cf2505b00866ab11598ed0bfae1e9a2 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:50:11 +0200 Subject: [PATCH 14/23] Update test/realm_test.dart Co-authored-by: blagoev --- test/realm_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 5aa35e38c..34665bdd3 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -195,7 +195,7 @@ Future main([List? args]) async { expect(config.schemaVersion, equals(3)); }); - test('Configuration isReadOnly property - realm files must exist', () { + test('Configuration readOnly - opening non existing realm throws', () { Configuration config = Configuration([Car.schema]); config.isReadOnly = true; //You cannot open Realm in read-only mode if Realm files do not exist. From 375f424bd660355ebd5f511746faf7ac92680c31 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:52:15 +0200 Subject: [PATCH 15/23] Update test/realm_test.dart Co-authored-by: blagoev --- test/realm_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 34665bdd3..30ab8a914 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -211,7 +211,7 @@ Future main([List? args]) async { }); test('Configuration read-only - open existing realm with read-only config', () { - //Create realm and its files + Configuration config = Configuration([Car.schema]); var realm = Realm(config); realm.close(); From 949cc3554581e75f6cdec4bee6a3184e7a3a4f64 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:06:21 +0200 Subject: [PATCH 16/23] Configuration and schema API documentation Co-authored-by: blagoev --- lib/src/configuration.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 59f9475c0..c1dd5623c 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -94,8 +94,8 @@ class Configuration { /// The realm file must already exists at [path] - bool get isReadOnly => realmCore.getReadOnlyMode(this); - set isReadOnly(bool value) => realmCore.setReadOnlyMode(this, value); + bool get isReadOnly => realmCore.getConfigReadOnly(this); + set isReadOnly(bool value) => realmCore.setConfigReadOnly(this, value); } /// A collection of properties describing the underlying schema of a [RealmObject]. From ad4407f6b4359fdda697d9cd0667085a03e7960c Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:12:58 +0200 Subject: [PATCH 17/23] Updated CHANGELOG --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a502585e9..5563c892d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ x.x.x Release notes (yyyy-MM-dd) ============================================================== +**This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.** + ### Changes -* Added support for isValid() on RealmList and RealmObject. ([#183](https://github.com/realm/realm-dart/pull/183)) * Added support for opening Realm in read-only mode. ([#260](https://github.com/realm/realm-dart/pull/260)) ### Enhancements @@ -13,7 +14,6 @@ x.x.x Release notes (yyyy-MM-dd) ### Compatibility * Dart ^2.15 on Windows, MacOS and Linux -**This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.** 0.2.0+alpha Release notes (2022-01-31) ============================================================== From ea51d3060d5df615c96d35426425f6c3b3472708 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:34:43 +0200 Subject: [PATCH 18/23] deleted redundant tests --- test/realm_test.dart | 89 +++++--------------------------------------- 1 file changed, 10 insertions(+), 79 deletions(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 706a9d8db..a57ac82d1 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -204,108 +204,40 @@ Future main([List? args]) async { }); test('Configuration readOnly - opening non existing realm throws', () { - Configuration config = Configuration([Car.schema]); - config.isReadOnly = true; - //You cannot open Realm in read-only mode if Realm files do not exist. - //Files are created the first time Realm is opened. - //Opening Realm in read-only mode can be done after reopening existing Realm with read-only configuration. - //First opening of Realm if configuration is read-only will throw an exception. - expect(() => Realm(config), throws("Message: No such table exists")); - }); - - test('Configuration readOnly argument - realm files must exist', () { Configuration config = Configuration([Car.schema], readOnly: true); expect(() => Realm(config), throws("Message: No such table exists")); }); - test('Configuration read-only - open existing realm with read-only config', () { - + test('Configuration readOnly - open existing realm with read-only config', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); realm.close(); - //Reconfigure existing realm for read-only mode. config = Configuration([Car.schema], readOnly: true); realm = Realm(config); realm.close(); }); - test('Configuration read-only - reading is possible', () { + test('Configuration readOnly - reading is possible', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); realm.write(() => realm.add(Car("Mustang"))); realm.close(); - //Reconfigure existing realm for read-only mode. config.isReadOnly = true; realm = Realm(config); var cars = realm.all(); realm.close(); }); - test('Configuration read-only - writing on read-only Realms throws', () { + test('Configuration readOnly - writing on read-only Realms throws', () { Configuration config = Configuration([Car.schema]); var realm = Realm(config); realm.close(); - - config = Configuration([Car.schema], readOnly: true); - realm = Realm(config); - expect(() => realm.write(() => {})), throws("Can't perform transactions on read-only Realms.")); - realm.close(); - }); - - test('Configuration read-only - editing realm objects throws', () { - Configuration config = Configuration([Car.schema]); - var realm = Realm(config); - var car = Car("Mustang"); - realm.write(() => realm.add(car)); - realm.close(); - - config.isReadOnly = true; - realm = Realm(config); - expect(() => realm.write(() => car.make = "Opel"), throws("Can't perform transactions on read-only Realms.")); - realm.close(); - }); - - test('Configuration read-only - deleting realm object throws', () { - Configuration config = Configuration([Car.schema]); - var realm = Realm(config); - var car = Car("Mustang"); - realm.write(() => realm.add(car)); - realm.close(); - config = Configuration([Car.schema], readOnly: true); realm = Realm(config); - expect(() => realm.write(() => realm.delete(car)), throws("Can't perform transactions on read-only Realms.")); - realm.close(); - }); - - test('Configuration read-only - deleting realm results throws', () { - Configuration config = Configuration([Car.schema]); - var realm = Realm(config); - realm.write(() => realm.addAll([Car("Mustang"), Car("Opel")])); - realm.close(); - - config = Configuration([Car.schema], readOnly: true); - realm = Realm(config); - var cars = realm.all(); - expect(() => realm.write(() => realm.deleteMany(cars)), throws("Can't perform transactions on read-only Realms.")); - realm.close(); - }); - - test('Configuration read-only - changing realm lists throws', () { - Configuration config = Configuration([Person.schema, Team.schema]); - var realm = Realm(config); - realm.write(() => realm.add(Team("Boston")..players.addAll([Person("N.M"), Person("K.L")]))); - realm.close(); - - config = Configuration([Person.schema, Team.schema], readOnly: true); - realm = Realm(config); - var team = realm.find("Boston"); - expect(() => realm.write(() => team!.players.clear()), throws("Can't perform transactions on read-only Realms.")); - expect(() => realm.write(() => team!.players.add(Person("O.P"))), throws("Can't perform transactions on read-only Realms.")); - expect(() => realm.write(() => realm.deleteMany(team!.players)), throws("Can't perform transactions on read-only Realms.")); + expect(() => realm.write(() {}), throws("Can't perform transactions on read-only Realms.")); realm.close(); }); }); @@ -1004,15 +936,15 @@ Future main([List? args]) async { var config = Configuration([Dog.schema, Person.schema]); var realm = Realm(config); - realm.write(() { + realm.write(() { realm.add(Dog("Lassy")); }); var callbackCalled = false; - final subscription = realm.all().changes.listen((changes) { + final subscription = realm.all().changes.listen((changes) { callbackCalled = true; }); - + await Future.delayed(Duration(milliseconds: 10)); expect(callbackCalled, true); @@ -1036,10 +968,10 @@ Future main([List? args]) async { var realm = Realm(config); var callbackCalled = false; - final subscription = realm.all().changes.listen((changes) { + final subscription = realm.all().changes.listen((changes) { callbackCalled = true; }); - + await Future.delayed(Duration(milliseconds: 10)); expect(callbackCalled, true); @@ -1057,8 +989,7 @@ Future main([List? args]) async { realm.add(Dog("Lassy1")); }); await Future.delayed(Duration(milliseconds: 10)); - expect(callbackCalled,true); - + expect(callbackCalled, true); await subscription.cancel(); await Future.delayed(Duration(milliseconds: 10)); From edffc2b0c4d6c113c7bcfd8ba921817679691950 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:36:35 +0200 Subject: [PATCH 19/23] updated CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5563c892d..152241422 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,13 @@ x.x.x Release notes (yyyy-MM-dd) **This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.** ### Changes -* Added support for opening Realm in read-only mode. ([#260](https://github.com/realm/realm-dart/pull/260)) + ### Enhancements * Support change notifications on query results. ([208](https://github.com/realm/realm-dart/pull/208)) * Added support checking if Realm lists and Realm objects are valid. ([#183](https://github.com/realm/realm-dart/pull/183)) * Support query on lists of realm objects. ([239](https://github.com/realm/realm-dart/pull/239)) +* Added support for opening Realm in read-only mode. ([#260](https://github.com/realm/realm-dart/pull/260)) ### Compatibility * Dart ^2.15 on Windows, MacOS and Linux From 3e797e165756ad6bd6ae9222368f4dbbbe532a49 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:37:54 +0200 Subject: [PATCH 20/23] empty rows --- lib/src/configuration.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index c1dd5623c..a3a10c846 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -92,8 +92,6 @@ class Configuration { /// from locked locations such as resources, bundled with an application. /// /// The realm file must already exists at [path] - - bool get isReadOnly => realmCore.getConfigReadOnly(this); set isReadOnly(bool value) => realmCore.setConfigReadOnly(this, value); } From cc6782d27382e2b5269f6841ac5f9b9b6280740a Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:54:44 +0200 Subject: [PATCH 21/23] Fixed comments --- lib/src/configuration.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index a3a10c846..f59e8df9f 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -36,7 +36,9 @@ class Configuration { RealmSchema get schema => _schema; /// Creates a [Configuration] with schema objects for opening a [Realm]. - /// [readOnly] controls whether a `Realm` is opened as readonly. This allows opening it from locked locations such as resources, bundled with an application. The realm file must already exists. + /// [readOnly] controls whether a [Realm] is opened as readonly. + /// This allows opening it from locked locations such as resources, + /// bundled with an application. The realm file must already exists. Configuration(List schemaObjects, {bool readOnly = false}) : _schema = RealmSchema(schemaObjects), _handle = realmCore.createConfig() { @@ -88,8 +90,9 @@ class Configuration { String get path => realmCore.getConfigPath(this); set path(String value) => realmCore.setConfigPath(this, value); - /// Gets or sets a value indicating whether a [Realm] is opened as readonly. This allows opening it - /// from locked locations such as resources, bundled with an application. + /// Gets or sets a value indicating whether a [Realm] is opened as readonly. + /// This allows opening it from locked locations such as resources, + /// bundled with an application. /// /// The realm file must already exists at [path] bool get isReadOnly => realmCore.getConfigReadOnly(this); From 1c056fe59c8c866610af1bbadb97fc42a1ad04c1 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 14:58:30 +0200 Subject: [PATCH 22/23] Fixed comments --- test/realm_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index a57ac82d1..d8fa1fb7d 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -212,7 +212,8 @@ Future main([List? args]) async { Configuration config = Configuration([Car.schema]); var realm = Realm(config); realm.close(); - + + // Open an existing realm as readonly. config = Configuration([Car.schema], readOnly: true); realm = Realm(config); realm.close(); From 7e2885e26844cb76789d872676ed72ec0ae7a0e6 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 18 Feb 2022 15:04:56 +0200 Subject: [PATCH 23/23] updated CHANGELOG --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 152241422..91a024575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,6 @@ x.x.x Release notes (yyyy-MM-dd) **This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.** -### Changes - - ### Enhancements * Support change notifications on query results. ([208](https://github.com/realm/realm-dart/pull/208)) * Added support checking if Realm lists and Realm objects are valid. ([#183](https://github.com/realm/realm-dart/pull/183))