Merge pull request #55 from realm/al-migrations

Minimal migration tests - use same path for all tests
This commit is contained in:
Ari Lazier 2015-10-12 17:26:51 -07:00
commit f4715da2a4
6 changed files with 73 additions and 10 deletions

View File

@ -860,6 +860,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = Debug; name = Debug;
@ -875,6 +880,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = Release; name = Release;
@ -982,6 +992,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = GCov_Build; name = GCov_Build;

View File

@ -403,6 +403,21 @@ JSValueRef RealmAddNotification(JSContextRef ctx, JSObjectRef function, JSObject
} }
} }
JSValueRef RealmClose(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try {
RJSValidateArgumentCount(argumentCount, 0);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
realm->invalidate();
realm::Realm::s_global_cache.remove(realm->config().path, realm->thread_id());
}
catch (std::exception &exp) {
if (jsException) {
*jsException = RJSMakeError(ctx, exp);
}
}
return NULL;
}
void RJSNotificationFinalize(JSObjectRef object) { void RJSNotificationFinalize(JSObjectRef object) {
Notification *notification = RJSGetInternal<Notification *>(object); Notification *notification = RJSGetInternal<Notification *>(object);
JSGlobalContextRelease(notification->ctx); JSGlobalContextRelease(notification->ctx);
@ -416,6 +431,7 @@ const JSStaticFunction RJSRealmFuncs[] = {
{"deleteAll", RealmDeleteAll, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"deleteAll", RealmDeleteAll, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"write", RealmWrite, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"write", RealmWrite, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"addNotification", RealmAddNotification, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"addNotification", RealmAddNotification, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"close", RealmClose, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL}, {NULL, NULL},
}; };

View File

@ -198,7 +198,9 @@ bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
auto migration_function = [&](Group*, Schema&) { auto migration_function = [&](Group*, Schema&) {
SharedRealm old_realm(new Realm(old_config)); SharedRealm old_realm(new Realm(old_config));
auto updated_realm = shared_from_this(); auto updated_realm = shared_from_this();
m_config.migration_function(old_realm, updated_realm); if (m_config.migration_function) {
m_config.migration_function(old_realm, updated_realm);
}
}; };
try { try {
@ -438,6 +440,19 @@ void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id)
} }
} }
void RealmCache::invalidate_all()
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto &path_realms : m_cache) {
for (auto &realm_iter : path_realms.second) {
if (auto realm = realm_iter.second.lock()) {
realm->invalidate();
}
}
}
}
void RealmCache::clear() void RealmCache::clear()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);

View File

@ -131,6 +131,7 @@ namespace realm {
SharedRealm get_any_realm(const std::string &path); SharedRealm get_any_realm(const std::string &path);
void remove(const std::string &path, std::thread::id thread_id); void remove(const std::string &path, std::thread::id thread_id);
void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id()); void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id());
void invalidate_all();
void clear(); void clear();
private: private:

View File

@ -20,6 +20,8 @@
#import "RJSUtil.hpp" #import "RJSUtil.hpp"
#import "RJSRealm.hpp" #import "RJSRealm.hpp"
#import "shared_realm.hpp"
NSString *RealmPathForFile(NSString *fileName) { NSString *RealmPathForFile(NSString *fileName) {
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
@ -30,10 +32,9 @@ NSString *RealmPathForFile(NSString *fileName) {
return [path stringByAppendingPathComponent:fileName]; return [path stringByAppendingPathComponent:fileName];
} }
static NSString *s_testPrefix;
NSString *TestRealmPath() { NSString *TestRealmPath() {
return RealmPathForFile([s_testPrefix stringByAppendingPathComponent:@"test.realm"]); return RealmPathForFile(@"test.realm");
} }
static void DeleteOrThrow(NSString *path) { static void DeleteOrThrow(NSString *path) {
@ -72,10 +73,8 @@ static JSClassRef s_globalClass;
- (void)setUp { - (void)setUp {
[super setUp]; [super setUp];
s_testPrefix = [[NSUUID UUID] UUIDString]; NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent];
NSString *defaultDir = RealmPathForFile(s_testPrefix);
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil]; [[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
RJSSetDefaultPath([defaultDir stringByAppendingPathComponent:@"default.realm"].UTF8String);
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(NULL, s_globalClass); JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(NULL, s_globalClass);
self.context = [JSContext contextWithJSGlobalContextRef:ctx]; self.context = [JSContext contextWithJSGlobalContextRef:ctx];
@ -90,9 +89,15 @@ static JSClassRef s_globalClass;
- (void)tearDown { - (void)tearDown {
self.context = nil; self.context = nil;
DeleteRealmFilesAtPath(TestRealmPath()); realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
// FIXME - find all realm files in the docs dir and delete them rather than hardcoding these
DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm"));
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));
[super tearDown]; [super tearDown];
} }

View File

@ -54,9 +54,20 @@ var RealmTests = {
var testPath = TestUtil.realmPathForFile('test1.realm'); var testPath = TestUtil.realmPathForFile('test1.realm');
var realm = new Realm({path: testPath, schema: [], schemaVersion: 1}); var realm = new Realm({path: testPath, schema: [], schemaVersion: 1});
TestCase.assertEqual(realm.schemaVersion, 1); TestCase.assertEqual(realm.schemaVersion, 1);
// FIXME - enable once Realm exposes a schema object
//TestCase.assertEqual(realm.schema.length, 0);
realm.close();
// FIXME - enable once realm initialization supports schema comparison
// TestCase.assertThrows(function() {
// realm = new Realm({path: testPath, schema: [TestObjectSchema], schemaVersion: 1});
// }, "schema changes require updating the schema version");
//realm = undefined; realm = new Realm({path: testPath, schema: [TestObjectSchema], schemaVersion: 2});
//realm = new Realm({path: testPath, schema: [], schemaVersion: 2}); realm.write(function() {
realm.create('TestObject', [1]);
});
TestCase.assertEqual(realm.objects('TestObject')[0].doubleCol, 1)
}, },
testDefaultPath: function() { testDefaultPath: function() {