-
Notifications
You must be signed in to change notification settings - Fork 157
/
MigrationTests.cs
345 lines (291 loc) · 15 KB
/
MigrationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 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.
//
////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
namespace Realms.Tests.Database
{
[TestFixture, Preserve(AllMembers = true)]
public class MigrationTests : RealmInstanceTest
{
private const string FileToMigrate = "ForMigrationsToCopyAndMigrate.realm";
[Test]
public void TriggerMigrationBySchemaVersion()
{
var config = (RealmConfiguration)RealmConfiguration.DefaultConfiguration;
using (var realm = GetRealm())
{
// new database doesn't push back a version number
Assert.That(realm.Config.SchemaVersion, Is.EqualTo(0));
}
var config2 = config.ConfigWithPath(config.DatabasePath);
config2.SchemaVersion = 99;
// same path, different version, should auto-migrate quietly
using var realm2 = GetRealm(config2);
Assert.That(realm2.Config.SchemaVersion, Is.EqualTo(99));
}
[Test]
public void TriggerMigrationBySchemaEditing()
{
// NOTE to regnerate the bundled database go edit the schema in Person.cs and comment/uncomment ExtraToTriggerMigration
// running in between and saving a copy with the added field
// this should never be needed as this test just needs the Realm to need migrating
// Because Realms opened during migration are not immediately disposed of, they can't be deleted.
// To circumvent that, we're leaking realm files.
// See https://github.com/realm/realm-dotnet/issues/1357
var path = TestHelpers.CopyBundledFileToDocuments(FileToMigrate, Path.Combine(InteropConfig.DefaultStorageFolder, Guid.NewGuid().ToString()));
var triggersSchemaFieldValue = string.Empty;
var configuration = new RealmConfiguration(path)
{
SchemaVersion = 100,
MigrationCallback = (migration, oldSchemaVersion) =>
{
Assert.That(oldSchemaVersion, Is.EqualTo(99));
var oldPeople = migration.OldRealm.DynamicApi.All("Person");
var newPeople = migration.NewRealm.All<Person>();
Assert.That(newPeople.Count(), Is.EqualTo(oldPeople.Count()));
for (var i = 0; i < newPeople.Count(); i++)
{
var oldPerson = oldPeople.ElementAt(i);
var newPerson = newPeople.ElementAt(i);
Assert.That(newPerson.LastName, Is.Not.EqualTo(oldPerson.TriggersSchema));
newPerson.LastName = triggersSchemaFieldValue = oldPerson.TriggersSchema;
}
}
};
var realm = GetRealm(configuration);
var person = realm.All<Person>().Single();
Assert.That(person.LastName, Is.EqualTo(triggersSchemaFieldValue));
}
[Test]
public void ExceptionInMigrationCallback()
{
// Because Realms opened during migration are not immediately disposed of, they can't be deleted.
// To circumvent that, we're leaking realm files.
// See https://github.com/realm/realm-dotnet/issues/1357
var path = TestHelpers.CopyBundledFileToDocuments(FileToMigrate, Path.Combine(InteropConfig.DefaultStorageFolder, Guid.NewGuid().ToString()));
var dummyException = new Exception();
var configuration = new RealmConfiguration(path)
{
SchemaVersion = 100,
MigrationCallback = (migration, oldSchemaVersion) =>
{
throw dummyException;
}
};
var ex = Assert.Throws<AggregateException>(() => GetRealm(configuration).Dispose());
Assert.That(ex.Flatten().InnerException, Is.SameAs(dummyException));
}
[Test]
public void MigrationTriggersDelete()
{
var path = RealmConfiguration.DefaultConfiguration.DatabasePath;
var oldSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
oldSchema.Add(person.Build());
}
using (var realm = Realm.GetInstance(new RealmConfiguration(path) { IsDynamic = true }, oldSchema.Build()))
{
realm.Write(() =>
{
dynamic person = realm.DynamicApi.CreateObject("Person", null);
person.Name = "Foo";
});
}
var newSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.Int });
newSchema.Add(person.Build());
}
using (var realm = Realm.GetInstance(new RealmConfiguration(path) { IsDynamic = true, ShouldDeleteIfMigrationNeeded = true }, newSchema.Build()))
{
Assert.That(realm.DynamicApi.All("Person"), Is.Empty);
}
}
[Test]
public void EmbeddedObjectWithOneBacklinkPerObject()
{
var path = RealmConfiguration.DefaultConfiguration.DatabasePath;
var oldSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property { Name = "Dog", Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable, ObjectType = "Dog" });
oldSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: false);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
oldSchema.Add(dog.Build());
}
using (var realm = Realm.GetInstance(new RealmConfiguration(path) { IsDynamic = true }, oldSchema.Build()))
{
realm.Write(() =>
{
dynamic scoobyDoo = realm.DynamicApi.CreateObject("Dog", null);
scoobyDoo.Name = "Scooby-Doo";
dynamic shaggy = realm.DynamicApi.CreateObject("Person", null);
shaggy.Name = "Shaggy Rogers";
shaggy.Dog = scoobyDoo;
});
}
var newSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property
{
Name = "Dog",
Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable,
ObjectType = "Dog"
});
newSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: true);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
newSchema.Add(dog.Build());
}
// All we need to do to make the change to embedded work IF every embedded object already
// has one and only one backlink is to increase the schema version.
var configuration = new RealmConfiguration(path)
{
SchemaVersion = 42,
IsDynamic = true
};
using (var realm = Realm.GetInstance(configuration, newSchema.Build()))
{
Assert.AreEqual(realm.DynamicApi.All("Person").Count(), 1);
Assert.AreEqual(realm.DynamicApi.All("Person").First().Name, "Shaggy Rogers");
Assert.NotNull(realm.DynamicApi.All("Person").First().Dog);
Assert.AreEqual(realm.DynamicApi.All("Person").First().Dog.Name, "Scooby-Doo");
}
}
[Test]
public void EmbeddedObjectBecomingOrphaned()
{
var path = RealmConfiguration.DefaultConfiguration.DatabasePath;
var oldSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property
{
Name = "Dog",
Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable,
ObjectType = "Dog"
});
oldSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: false);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
oldSchema.Add(dog.Build());
}
using (var realm = Realm.GetInstance(new RealmConfiguration(path) { IsDynamic = true }, oldSchema.Build()))
{
realm.Write(() =>
{
dynamic scoobyDoo = realm.DynamicApi.CreateObject("Dog", null);
scoobyDoo.Name = "Scooby-Doo";
dynamic shaggy = realm.DynamicApi.CreateObject("Person", null);
shaggy.Name = "Shaggy Rogers";
});
}
var newSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property { Name = "Dog", Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable, ObjectType = "Dog" });
newSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: true);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
newSchema.Add(dog.Build());
}
var configuration = new RealmConfiguration(path)
{
SchemaVersion = 42,
IsDynamic = true,
MigrationCallback = (migration, oldSchemaVersion) =>
{
Assert.That(oldSchemaVersion, Is.EqualTo(0));
var oldPeople = migration.OldRealm.DynamicApi.All("Person");
var newPeople = migration.NewRealm.DynamicApi.All("Person");
Assert.That(newPeople.Count(), Is.EqualTo(oldPeople.Count()));
var oldDogs = migration.OldRealm.DynamicApi.All("Dog");
var newDogs = migration.NewRealm.DynamicApi.All("Dog"); // Throws
Assert.That(newDogs.Count(), Is.EqualTo(oldDogs.Count()));
}
};
using (var realm = Realm.GetInstance(configuration, newSchema.Build()))
{
Assert.AreEqual(realm.DynamicApi.All("Person").Count(), 1);
Assert.AreEqual(realm.DynamicApi.All("Person").First().Name, "Shaggy Rogers");
}
}
[Test]
public void EmbeddedObjectWithMultipleBacklinks()
{
var path = RealmConfiguration.DefaultConfiguration.DatabasePath;
var oldSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property { Name = "Dog", Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable, ObjectType = "Dog" });
oldSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: false);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
oldSchema.Add(dog.Build());
}
using (var realm = Realm.GetInstance(new RealmConfiguration(path) { IsDynamic = true }, oldSchema.Build()))
{
realm.Write(() =>
{
dynamic scoobyDoo = realm.DynamicApi.CreateObject("Dog", null);
scoobyDoo.Name = "Scooby-Doo";
dynamic shaggy = realm.DynamicApi.CreateObject("Person", null);
shaggy.Name = "Shaggy Rogers";
shaggy.Dog = scoobyDoo;
dynamic charlie = realm.DynamicApi.CreateObject("Person", null);
charlie.Name = "Charlie Brown";
charlie.Dog = scoobyDoo;
});
}
var newSchema = new Schema.RealmSchema.Builder();
{
var person = new Schema.ObjectSchema.Builder("Person", isEmbedded: false);
person.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
person.Add(new Schema.Property { Name = "Dog", Type = Schema.PropertyType.Object | Schema.PropertyType.Nullable, ObjectType = "Dog" });
newSchema.Add(person.Build());
var dog = new Schema.ObjectSchema.Builder("Dog", isEmbedded: true);
dog.Add(new Schema.Property { Name = "Name", Type = Schema.PropertyType.String });
newSchema.Add(dog.Build());
}
var configuration = new RealmConfiguration(path)
{
SchemaVersion = 42,
IsDynamic = true,
};
using (var realm = Realm.GetInstance(configuration, newSchema.Build()))
{
Assert.AreEqual(realm.DynamicApi.All("Person").Count(), 1);
Assert.AreEqual(realm.DynamicApi.All("Person").First().Name, "Shaggy Rogers");
Assert.NotNull(realm.DynamicApi.All("Person").First().Dog);
Assert.AreEqual(realm.DynamicApi.All("Person").First().Dog.Name, "Scooby-Doo");
}
}
}
}