Skip to content

Commit

Permalink
Merge 25c2685 into 32ac88d
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova authored Mar 25, 2022
2 parents 32ac88d + 25c2685 commit 767ea8e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
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.**

### Enhancements
* Support disableFormatUpgrade ([#310](https://github.com/realm/realm-dart/pull/310))

0.2.1+alpha Release notes (2022-03-20)
==============================================================

Expand Down
13 changes: 12 additions & 1 deletion lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class Configuration {
/// [fifoFilesFallbackPath] enables FIFO special files.
/// [readOnly] controls whether a [Realm] is opened as read-only.
/// [inMemory] specifies if a [Realm] should be opened in-memory.
Configuration(List<SchemaObject> schemaObjects, {String? fifoFilesFallbackPath, bool readOnly = false, bool inMemory = false})
/// [disableFormatUpgrade] specifies if [Realm] file format should be automatically upgraded.
Configuration(List<SchemaObject> schemaObjects,
{String? fifoFilesFallbackPath, bool readOnly = false, bool inMemory = false, bool disableFormatUpgrade = false})
: _schema = RealmSchema(schemaObjects),
_handle = realmCore.createConfig() {
schemaVersion = 0;
Expand All @@ -49,6 +51,9 @@ class Configuration {
if (fifoFilesFallbackPath != null) {
this.fifoFilesFallbackPath = fifoFilesFallbackPath;
}
if (disableFormatUpgrade == true) {
this.disableFormatUpgrade = true;
}

if (readOnly) {
isReadOnly = true;
Expand Down Expand Up @@ -125,6 +130,12 @@ class Configuration {
/// by the [path] you property. This property is ignored if the directory defined by [path] allow FIFO special files.
String get fifoFilesFallbackPath => realmCore.getConfigFifoPath(this);
set fifoFilesFallbackPath(String value) => realmCore.setConfigFifoPath(this, value);

/// Specifies if a [Realm] file format should be automatically upgraded
/// if it was created with an older version of the [Realm] library.
/// If set to true and a file format upgrade is required, an error will be thrown instead.
bool get disableFormatUpgrade => realmCore.getConfigDisableFormatUpgrade(this);
set disableFormatUpgrade(bool value) => realmCore.setConfigDisableFormatUpgrade(this, value);
}

/// A collection of properties describing the underlying schema of a [RealmObject].
Expand Down
8 changes: 8 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ class _RealmCore {
_realmLib.invokeGetBool(() => _realmLib.realm_refresh(realm.handle._pointer), "Could not refresh");
}

bool getConfigDisableFormatUpgrade(Configuration config) {
return _realmLib.realm_config_get_disable_format_upgrade(config.handle._pointer);
}

void setConfigDisableFormatUpgrade(Configuration config, bool disabled) {
_realmLib.realm_config_set_disable_format_upgrade(config.handle._pointer, disabled);
}

RealmClassMetadata getClassMetadata(Realm realm, String className, Type classType) {
return using((Arena arena) {
final found = arena<Uint8>();
Expand Down
52 changes: 49 additions & 3 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Future<void> main([List<String>? args]) async {
expect(() => realm.write(() {}), throws<RealmException>("Can't perform transactions on read-only Realms."));
realm.close();
});

test('Configuration inMemory - no files after closing realm', () {
Configuration config = Configuration([Car.schema], inMemory: true);
var realm = Realm(config);
Expand All @@ -125,11 +125,57 @@ Future<void> main([List<String>? args]) async {
expect(() => Realm(config), throws<RealmException>("Realm at path '${config.path}' already opened with different read permissions"));
realm.close();
});

test('Configuration - FIFO files fallback path', () {
Configuration config = Configuration([Car.schema], fifoFilesFallbackPath: "./fifo_folder");
var realm = Realm(config);
realm.close();
});


test('Configuration - disableFormatUpgrade=true throws error', () {
const realmBundleFile = "test/data/realm_files/realm-bundle.realm";
final realmDir = "test/${Configuration.defaultPath}true";
final realmPath = "$realmDir/${Configuration.defaultPath}";
try {
Directory(realmDir).createSync();
var config = Configuration([Car.schema])..path = realmPath;
var realm = Realm(config);
realm.close();

Realm.deleteRealm(realmPath);
var file = File(realmBundleFile);
file.copySync(realmPath);

config = Configuration([Car.schema], disableFormatUpgrade: true)..path = realmPath;
expect(() {
realm = Realm(config);
}, throws<RealmException>("The Realm file format must be allowed to be upgraded in order to proceed"));
realm.close();
} finally {
Directory(realmDir).deleteSync(recursive: true);
}
});

test('Configuration - disableFormatUpgrade=false', () {
const realmBundleFile = "test/data/realm_files/realm-bundle.realm";
final realmDir = "test/${Configuration.defaultPath}false";
final realmPath = "$realmDir/${Configuration.defaultPath}";

try {
Directory(realmDir).createSync();
var config = Configuration([Car.schema])..path = realmPath;
var realm = Realm(config);
realm.close();

Realm.deleteRealm(realmPath);
var file = File(realmBundleFile);
file.copySync(realmPath);

config = Configuration([Car.schema], disableFormatUpgrade: false)..path = realmPath;
realm = Realm(config);
realm.close();
} finally {
Directory(realmDir).deleteSync(recursive: true);
}
});
}
Binary file added test/data/realm_files/realm-bundle.realm
Binary file not shown.

0 comments on commit 767ea8e

Please sign in to comment.