Move realm data migrations inside the `RealmContextFactory`

This commit is contained in:
Dean Herbert 2021-11-23 18:04:21 +09:00
parent d9917ee6c7
commit 4149d458f0
3 changed files with 52 additions and 31 deletions

View File

@ -96,6 +96,9 @@ public RealmContextFactory(Storage storage, string filename, IDatabaseContextFac
Filename += realm_extension;
cleanupPendingDeletions();
// run after the above operation to ensure realm is already in a good (fully migrated) state.
migrateDataFromEF();
}
private void cleanupPendingDeletions()
@ -163,6 +166,54 @@ private RealmConfiguration getConfiguration()
};
}
private void migrateDataFromEF()
{
if (efContextFactory == null)
return;
using (var db = efContextFactory.GetForWrite())
{
// migrate ruleset settings. can be removed 20220315.
var existingSettings = db.Context.DatabasedSetting;
// previous entries in EF are removed post migration.
if (!existingSettings.Any())
return;
using (var realm = CreateContext())
using (var transaction = realm.BeginWrite())
{
// only migrate data if the realm database is empty.
if (!realm.All<RealmRulesetSetting>().Any())
{
foreach (var dkb in existingSettings)
{
if (dkb.RulesetID == null)
continue;
string? shortName = getRulesetShortNameFromLegacyID(dkb.RulesetID.Value);
if (string.IsNullOrEmpty(shortName))
continue;
realm.Add(new RealmRulesetSetting
{
Key = dkb.Key,
Value = dkb.StringValue,
// important: this RulesetStore must be the EF one.
RulesetName = shortName,
Variant = dkb.Variant ?? 0,
});
}
}
db.Context.RemoveRange(existingSettings);
transaction.Commit();
}
}
}
private void onMigration(Migration migration, ulong lastSchemaVersion)
{
for (ulong i = lastSchemaVersion + 1; i <= schema_version; i++)

View File

@ -262,8 +262,6 @@ List<ScoreInfo> getBeatmapScores(BeatmapSetInfo set)
dependencies.Cache(scorePerformanceManager);
AddInternal(scorePerformanceManager);
migrateDataToRealm();
dependencies.Cache(rulesetConfigCache = new RulesetConfigCache(realmFactory, RulesetStore));
var powerStatus = CreateBatteryInfo();
@ -439,35 +437,6 @@ public void Migrate(string path)
private void migrateDataToRealm()
{
using (var db = contextFactory.GetForWrite())
using (var realm = realmFactory.CreateContext())
using (var transaction = realm.BeginWrite())
{
// migrate ruleset settings. can be removed 20220315.
var existingSettings = db.Context.DatabasedSetting;
// only migrate data if the realm database is empty.
if (!realm.All<RealmRulesetSetting>().Any())
{
foreach (var dkb in existingSettings)
{
if (dkb.RulesetID == null) continue;
realm.Add(new RealmRulesetSetting
{
Key = dkb.Key,
Value = dkb.StringValue,
// important: this RulesetStore must be the EF one.
RulesetName = RulesetStore.GetRuleset(dkb.RulesetID.Value).ShortName,
Variant = dkb.Variant ?? 0,
});
}
}
db.Context.RemoveRange(existingSettings);
transaction.Commit();
}
}
private void onRulesetChanged(ValueChangedEvent<RulesetInfo> r)

View File

@ -311,6 +311,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AABB/@EntryIndexedValue">AABB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=API/@EntryIndexedValue">API</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BPM/@EntryIndexedValue">BPM</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EF/@EntryIndexedValue">EF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FPS/@EntryIndexedValue">FPS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GC/@EntryIndexedValue">GC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GL/@EntryIndexedValue">GL</s:String>