Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configuration - disable format upgrade #310

Merged
merged 33 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ef6bd51
Configuration disable format upgrade
desistefanova Mar 23, 2022
3e2222d
Update api doc and changelog
desistefanova Mar 23, 2022
feafecd
fix tests paths
desistefanova Mar 23, 2022
aa1ec2c
Merge branch 'master' into disable_format_upgrade
desistefanova Mar 24, 2022
e91eadc
Wait dyrectory to be created before continue
desistefanova Mar 24, 2022
db73909
Create random directory name
desistefanova Mar 25, 2022
6b1f75a
Different folder names for both tests
desistefanova Mar 25, 2022
25c2685
Sync file operations
desistefanova Mar 25, 2022
f96414c
Try to delete realm directory
desistefanova Mar 25, 2022
3d2a764
Rename realm bundle file
desistefanova Mar 25, 2022
4531ad1
Remove try-finally
desistefanova Mar 25, 2022
8f836d9
Merge branch 'master' into disable_format_upgrade
desistefanova Mar 25, 2022
396b675
Use realm full paths
desistefanova Mar 25, 2022
7482732
Skip disableFormatUpgrade tests for Android and IOS
desistefanova Mar 28, 2022
2a6d13a
Skip all flutter tests except windows
desistefanova Mar 28, 2022
914909f
Merge branch 'master' into disable_format_upgrade
desistefanova Mar 28, 2022
c43e979
Merge branch 'master' into disable_format_upgrade
desistefanova Mar 28, 2022
c642883
Skip tests for Flutter on all the platforms
desistefanova Mar 29, 2022
bb45239
Merge branch 'disable_format_upgrade' of https://github.com/realm/rea…
desistefanova Mar 29, 2022
b445dcc
Update CHANGELOG.md
desistefanova Apr 5, 2022
0df351b
change file name and paths
desistefanova Apr 5, 2022
ae0865f
fix path for Android
desistefanova Apr 6, 2022
0c1719d
Update CHANGELOG.md
desistefanova Apr 6, 2022
6394773
Update CHANGELOG.md
desistefanova Apr 6, 2022
33951a9
Merge branch 'disable_format_upgrade' of https://github.com/realm/rea…
desistefanova Apr 6, 2022
600e136
Merge branch 'master' into disable_format_upgrade
desistefanova Apr 11, 2022
5e0bbcb
Merge branch 'disable_format_upgrade' of https://github.com/realm/rea…
desistefanova Apr 11, 2022
5099827
Merge branch 'master' into disable_format_upgrade
desistefanova Apr 11, 2022
bccece3
Fix disableFormatUpgrade property after merge from master
desistefanova Apr 11, 2022
fde7871
Update lib/src/configuration.dart
desistefanova Apr 11, 2022
3af8b71
Update config properties only if they are different than defaults.
desistefanova Apr 11, 2022
8c8a476
Configuration isReadOnly always set
desistefanova Apr 11, 2022
fbb20ab
Set isReadOnly only when true
desistefanova Apr 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ x.x.x Release notes (yyyy-MM-dd)
* Made all `Configuration` fields final so they can only be initialized in the constructor. This better conveys the immutability of the configuration class. ([#455](https://github.com/realm/realm-dart/pull/455))

### Enhancements
* Added a new `Configuration` option: `disableFormatUpgrade`. When set to `true`, opening a Realm with an older file format will throw an exception to avoid automatically migrating it. ([#310](https://github.com/realm/realm-dart/pull/310))
```dart
var config = Configuration([Car.schema], disableFormatUpgrade: true);
await File('old-format.realm').copy(config.path);
var realm = Realm(config);
//Opening 'Realm' will throw a RealmException with message: "The Realm file format must be allowed to be upgraded in order to proceed."
```
* Support result value from write transaction callbacks ([#294](https://github.com/realm/realm-dart/pull/294/))
* Added a property `Realm.isInTransaction` that indicates whether the Realm instance has an open write transaction associated with it.
* Support anonymous application credentials ([#443](https://github.com/realm/realm-dart/pull/443/))
Expand Down
7 changes: 6 additions & 1 deletion lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Configuration {

/// Creates a [Configuration] with schema objects for opening a [Realm].
Configuration(List<SchemaObject> schemaObjects,
{this.fifoFilesFallbackPath, this.isReadOnly = false, this.isInMemory = false, this.schemaVersion = 0, String? path})
{String? path, this.fifoFilesFallbackPath, this.isReadOnly = false, this.isInMemory = false, this.schemaVersion = 0, this.disableFormatUpgrade = false})
: schema = RealmSchema(schemaObjects),
path = path ?? defaultPath;

Expand Down Expand Up @@ -99,6 +99,11 @@ class Configuration {
/// The FIFO special files are very lightweight and the main [Realm] file will still be stored in the location defined
/// by the [path] you property. This property is ignored if the directory defined by [path] allow FIFO special files.
final String? fifoFilesFallbackPath;

/// Specifies if a [Realm] file format should be automatically upgraded
/// if it was created with an older version of the [Realm] library.
/// An exception will be thrown if a file format upgrade is required.
final bool disableFormatUpgrade;
}

/// A collection of properties describing the underlying schema of a [RealmObject].
Expand Down
18 changes: 11 additions & 7 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@ class _RealmCore {
final configHandle = ConfigHandle._(configPtr);
_realmLib.realm_config_set_schema(configHandle._pointer, schemaHandle._pointer);
_realmLib.realm_config_set_schema_version(configHandle._pointer, config.schemaVersion);

final schemaMode = config.isReadOnly ? realm_schema_mode.RLM_SCHEMA_MODE_IMMUTABLE : realm_schema_mode.RLM_SCHEMA_MODE_AUTOMATIC;
_realmLib.realm_config_set_schema_mode(configHandle._pointer, schemaMode);
_realmLib.realm_config_set_in_memory(configHandle._pointer, config.isInMemory);

_realmLib.realm_config_set_path(configHandle._pointer, config.path.toUtf8Ptr(arena));
_realmLib.realm_config_set_scheduler(configHandle._pointer, schedulerHandle._pointer);
if (config.isReadOnly) {
blagoev marked this conversation as resolved.
Show resolved Hide resolved
_realmLib.realm_config_set_schema_mode(configHandle._pointer, realm_schema_mode.RLM_SCHEMA_MODE_IMMUTABLE);
}
if (config.isInMemory) {
desistefanova marked this conversation as resolved.
Show resolved Hide resolved
_realmLib.realm_config_set_in_memory(configHandle._pointer, config.isInMemory);
}
if (config.fifoFilesFallbackPath != null) {
_realmLib.realm_config_set_fifo_path(configHandle._pointer, config.fifoFilesFallbackPath!.toUtf8Ptr(arena));
}
_realmLib.realm_config_set_path(configHandle._pointer, config.path.toUtf8Ptr(arena));
_realmLib.realm_config_set_scheduler(configHandle._pointer, schedulerHandle._pointer);
if (config.disableFormatUpgrade) {
_realmLib.realm_config_set_disable_format_upgrade(configHandle._pointer, config.disableFormatUpgrade);
}

return configHandle;
});
Expand Down
17 changes: 17 additions & 0 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,21 @@ Future<void> main([List<String>? args]) async {
final realm2 = getRealm(config);
expect(realm1.config, isNot(realm2.config));
});

test('Configuration - disableFormatUpgrade=true throws error', () async {
final realmBundleFile = "test/data/realm_files/old-format.realm";
var config = Configuration([Car.schema], disableFormatUpgrade: true);
await File(realmBundleFile).copy(config.path);
expect(() {
Realm(config);
}, throws<RealmException>("The Realm file format must be allowed to be upgraded in order to proceed"));
}, skip: isFlutterPlatform);

test('Configuration - disableFormatUpgrade=false', () async {
final realmBundleFile = "test/data/realm_files/old-format.realm";
var config = Configuration([Car.schema], disableFormatUpgrade: false);
await File(realmBundleFile).copy(config.path);
var realm = Realm(config);
realm.close();
}, skip: isFlutterPlatform);
}
Binary file added test/data/realm_files/old-format.realm
Binary file not shown.
22 changes: 5 additions & 17 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,16 @@ Future<void> setupTests(List<String>? args) async {
}

for (final path in paths) {
var file = File(path);
try {
Realm.deleteRealm(path);
} catch (e) {
fail("Can not delete realm at path: $path. Did you forget to close it?");
}

if (await file.exists() && file.path.endsWith(".realm")) {
await tryDeleteFile(file);
}

file = File("$path.lock");
if (await file.exists()) {
await tryDeleteFile(file);
}

final dir = Directory("$path.management");
if (await dir.exists()) {
if ((await dir.stat()).type == FileSystemEntityType.directory) {
await tryDeleteFile(dir, recursive: true);
}
}
String pathKey = _path.basenameWithoutExtension(path);
String realmDir = _path.dirname(path);
await Directory(realmDir).list().forEach((f) {
if (f.path.contains(pathKey)) tryDeleteFile(f, recursive: true);
});
}
});
});
Expand Down