2021-11-30 14:41:18 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-01-18 13:19:25 +08:00
|
|
|
using System;
|
2022-01-19 15:31:27 +08:00
|
|
|
using System.IO;
|
2021-11-30 14:41:18 +08:00
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-01-18 13:19:31 +08:00
|
|
|
using osu.Framework.Logging;
|
2022-01-19 15:31:27 +08:00
|
|
|
using osu.Framework.Platform;
|
2022-01-13 16:51:23 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-11-30 14:41:18 +08:00
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Models;
|
2022-01-13 16:51:23 +08:00
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Scoring;
|
2021-11-30 14:41:18 +08:00
|
|
|
using osu.Game.Skinning;
|
2022-01-13 16:51:23 +08:00
|
|
|
using Realms;
|
2021-11-30 14:41:18 +08:00
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
internal class EFToRealmMigrator
|
|
|
|
{
|
|
|
|
private readonly DatabaseContextFactory efContextFactory;
|
|
|
|
private readonly RealmContextFactory realmContextFactory;
|
|
|
|
private readonly OsuConfigManager config;
|
2022-01-19 15:31:27 +08:00
|
|
|
private readonly Storage storage;
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-19 15:31:27 +08:00
|
|
|
public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config, Storage storage)
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
|
|
|
this.efContextFactory = efContextFactory;
|
|
|
|
this.realmContextFactory = realmContextFactory;
|
|
|
|
this.config = config;
|
2022-01-19 15:31:27 +08:00
|
|
|
this.storage = storage;
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
{
|
2022-01-20 23:01:34 +08:00
|
|
|
createBackup();
|
|
|
|
|
2022-01-19 14:52:59 +08:00
|
|
|
using (var ef = efContextFactory.Get())
|
2022-01-18 19:47:53 +08:00
|
|
|
{
|
2022-01-18 13:06:22 +08:00
|
|
|
migrateSettings(ef);
|
|
|
|
migrateSkins(ef);
|
|
|
|
migrateBeatmaps(ef);
|
|
|
|
migrateScores(ef);
|
2022-01-18 19:47:53 +08:00
|
|
|
}
|
2022-01-18 13:17:43 +08:00
|
|
|
|
|
|
|
// Delete the database permanently.
|
|
|
|
// Will cause future startups to not attempt migration.
|
2022-01-18 13:41:02 +08:00
|
|
|
Logger.Log("Migration successful, deleting EF database", LoggingTarget.Database);
|
2022-01-18 13:17:43 +08:00
|
|
|
efContextFactory.ResetDatabase();
|
2022-01-13 16:51:23 +08:00
|
|
|
}
|
|
|
|
|
2022-01-19 14:52:59 +08:00
|
|
|
private void migrateBeatmaps(OsuDbContext ef)
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
|
|
|
// can be removed 20220730.
|
2022-01-19 14:52:59 +08:00
|
|
|
var existingBeatmapSets = ef.EFBeatmapSetInfo
|
2022-01-18 18:12:10 +08:00
|
|
|
.Include(s => s.Beatmaps).ThenInclude(b => b.RulesetInfo)
|
|
|
|
.Include(s => s.Beatmaps).ThenInclude(b => b.Metadata)
|
|
|
|
.Include(s => s.Beatmaps).ThenInclude(b => b.BaseDifficulty)
|
|
|
|
.Include(s => s.Files).ThenInclude(f => f.FileInfo)
|
|
|
|
.Include(s => s.Metadata);
|
2022-01-13 17:02:08 +08:00
|
|
|
|
2022-01-18 13:41:02 +08:00
|
|
|
Logger.Log("Beginning beatmaps migration to realm", LoggingTarget.Database);
|
|
|
|
|
2022-01-13 17:02:08 +08:00
|
|
|
// previous entries in EF are removed post migration.
|
|
|
|
if (!existingBeatmapSets.Any())
|
2022-01-18 13:41:02 +08:00
|
|
|
{
|
|
|
|
Logger.Log("No beatmaps found to migrate", LoggingTarget.Database);
|
2022-01-13 17:02:08 +08:00
|
|
|
return;
|
2022-01-18 13:41:02 +08:00
|
|
|
}
|
2022-01-18 13:19:31 +08:00
|
|
|
|
2022-01-18 18:12:10 +08:00
|
|
|
int count = existingBeatmapSets.Count();
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
realmContextFactory.Run(realm =>
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
2022-01-18 18:12:10 +08:00
|
|
|
Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database);
|
2022-01-19 09:16:54 +08:00
|
|
|
|
2022-01-13 17:02:08 +08:00
|
|
|
// only migrate data if the realm database is empty.
|
2022-01-19 09:20:43 +08:00
|
|
|
// note that this cannot be written as: `realm.All<BeatmapSetInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
|
2022-01-18 13:41:02 +08:00
|
|
|
if (realm.All<BeatmapSetInfo>().Any(s => !s.Protected))
|
|
|
|
{
|
|
|
|
Logger.Log("Skipping migration as realm already has beatmaps loaded", LoggingTarget.Database);
|
|
|
|
}
|
|
|
|
else
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
2022-01-19 15:01:17 +08:00
|
|
|
var transaction = realm.BeginWrite();
|
|
|
|
int written = 0;
|
|
|
|
|
|
|
|
try
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
2022-01-18 13:41:02 +08:00
|
|
|
foreach (var beatmapSet in existingBeatmapSets)
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
2022-01-19 15:01:17 +08:00
|
|
|
if (++written % 1000 == 0)
|
|
|
|
{
|
|
|
|
transaction.Commit();
|
|
|
|
transaction = realm.BeginWrite();
|
|
|
|
Logger.Log($"Migrated {written}/{count} beatmaps...", LoggingTarget.Database);
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:41:02 +08:00
|
|
|
var realmBeatmapSet = new BeatmapSetInfo
|
2022-01-13 17:02:08 +08:00
|
|
|
{
|
2022-01-18 13:41:02 +08:00
|
|
|
OnlineID = beatmapSet.OnlineID ?? -1,
|
|
|
|
DateAdded = beatmapSet.DateAdded,
|
|
|
|
Status = beatmapSet.Status,
|
|
|
|
DeletePending = beatmapSet.DeletePending,
|
|
|
|
Hash = beatmapSet.Hash,
|
|
|
|
Protected = beatmapSet.Protected,
|
2022-01-13 17:02:08 +08:00
|
|
|
};
|
|
|
|
|
2022-01-18 13:41:02 +08:00
|
|
|
migrateFiles(beatmapSet, realm, realmBeatmapSet);
|
|
|
|
|
|
|
|
foreach (var beatmap in beatmapSet.Beatmaps)
|
|
|
|
{
|
2022-01-19 15:22:17 +08:00
|
|
|
var ruleset = realm.Find<RulesetInfo>(beatmap.RulesetInfo.ShortName);
|
|
|
|
var metadata = getBestMetadata(beatmap.Metadata, beatmapSet.Metadata);
|
|
|
|
|
|
|
|
var realmBeatmap = new BeatmapInfo(ruleset, new BeatmapDifficulty(beatmap.BaseDifficulty), metadata)
|
2022-01-18 13:41:02 +08:00
|
|
|
{
|
|
|
|
DifficultyName = beatmap.DifficultyName,
|
|
|
|
Status = beatmap.Status,
|
|
|
|
OnlineID = beatmap.OnlineID ?? -1,
|
|
|
|
Length = beatmap.Length,
|
|
|
|
BPM = beatmap.BPM,
|
|
|
|
Hash = beatmap.Hash,
|
|
|
|
StarRating = beatmap.StarRating,
|
|
|
|
MD5Hash = beatmap.MD5Hash,
|
|
|
|
Hidden = beatmap.Hidden,
|
|
|
|
AudioLeadIn = beatmap.AudioLeadIn,
|
|
|
|
StackLeniency = beatmap.StackLeniency,
|
|
|
|
SpecialStyle = beatmap.SpecialStyle,
|
|
|
|
LetterboxInBreaks = beatmap.LetterboxInBreaks,
|
|
|
|
WidescreenStoryboard = beatmap.WidescreenStoryboard,
|
|
|
|
EpilepsyWarning = beatmap.EpilepsyWarning,
|
|
|
|
SamplesMatchPlaybackRate = beatmap.SamplesMatchPlaybackRate,
|
|
|
|
DistanceSpacing = beatmap.DistanceSpacing,
|
|
|
|
BeatDivisor = beatmap.BeatDivisor,
|
|
|
|
GridSize = beatmap.GridSize,
|
|
|
|
TimelineZoom = beatmap.TimelineZoom,
|
|
|
|
Countdown = beatmap.Countdown,
|
|
|
|
CountdownOffset = beatmap.CountdownOffset,
|
|
|
|
MaxCombo = beatmap.MaxCombo,
|
|
|
|
Bookmarks = beatmap.Bookmarks,
|
|
|
|
BeatmapSet = realmBeatmapSet,
|
|
|
|
};
|
|
|
|
|
|
|
|
realmBeatmapSet.Beatmaps.Add(realmBeatmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.Add(realmBeatmapSet);
|
2022-01-13 17:02:08 +08:00
|
|
|
}
|
2022-01-19 15:01:17 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2022-01-18 13:41:02 +08:00
|
|
|
transaction.Commit();
|
2022-01-13 17:02:08 +08:00
|
|
|
}
|
2022-01-19 15:01:17 +08:00
|
|
|
|
|
|
|
Logger.Log($"Successfully migrated {count} beatmaps to realm", LoggingTarget.Database);
|
2022-01-13 17:02:08 +08:00
|
|
|
}
|
2022-01-21 16:08:20 +08:00
|
|
|
});
|
2022-01-13 17:02:08 +08:00
|
|
|
}
|
|
|
|
|
2022-01-14 22:31:42 +08:00
|
|
|
private BeatmapMetadata getBestMetadata(EFBeatmapMetadata? beatmapMetadata, EFBeatmapMetadata? beatmapSetMetadata)
|
|
|
|
{
|
|
|
|
var metadata = beatmapMetadata ?? beatmapSetMetadata ?? new EFBeatmapMetadata();
|
|
|
|
|
|
|
|
return new BeatmapMetadata
|
|
|
|
{
|
|
|
|
Title = metadata.Title,
|
|
|
|
TitleUnicode = metadata.TitleUnicode,
|
|
|
|
Artist = metadata.Artist,
|
|
|
|
ArtistUnicode = metadata.ArtistUnicode,
|
2022-01-19 15:22:17 +08:00
|
|
|
Author =
|
2022-01-14 22:31:42 +08:00
|
|
|
{
|
|
|
|
OnlineID = metadata.Author.Id,
|
|
|
|
Username = metadata.Author.Username,
|
|
|
|
},
|
|
|
|
Source = metadata.Source,
|
|
|
|
Tags = metadata.Tags,
|
|
|
|
PreviewTime = metadata.PreviewTime,
|
|
|
|
AudioFile = metadata.AudioFile,
|
|
|
|
BackgroundFile = metadata.BackgroundFile,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:52:59 +08:00
|
|
|
private void migrateScores(OsuDbContext db)
|
2022-01-13 16:51:23 +08:00
|
|
|
{
|
|
|
|
// can be removed 20220730.
|
2022-01-19 14:52:59 +08:00
|
|
|
var existingScores = db.ScoreInfo
|
2022-01-18 18:12:10 +08:00
|
|
|
.Include(s => s.Ruleset)
|
|
|
|
.Include(s => s.BeatmapInfo)
|
|
|
|
.Include(s => s.Files)
|
|
|
|
.ThenInclude(f => f.FileInfo);
|
2022-01-13 16:51:23 +08:00
|
|
|
|
2022-01-18 13:41:02 +08:00
|
|
|
Logger.Log("Beginning scores migration to realm", LoggingTarget.Database);
|
|
|
|
|
2022-01-13 16:51:23 +08:00
|
|
|
// previous entries in EF are removed post migration.
|
|
|
|
if (!existingScores.Any())
|
2022-01-18 13:41:02 +08:00
|
|
|
{
|
|
|
|
Logger.Log("No scores found to migrate", LoggingTarget.Database);
|
2022-01-13 16:51:23 +08:00
|
|
|
return;
|
2022-01-18 13:41:02 +08:00
|
|
|
}
|
2022-01-18 13:19:31 +08:00
|
|
|
|
2022-01-18 18:12:10 +08:00
|
|
|
int count = existingScores.Count();
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
realmContextFactory.Run(realm =>
|
2022-01-13 16:51:23 +08:00
|
|
|
{
|
2022-01-18 18:12:10 +08:00
|
|
|
Logger.Log($"Found {count} scores in EF", LoggingTarget.Database);
|
2022-01-19 09:16:54 +08:00
|
|
|
|
2022-01-13 16:51:23 +08:00
|
|
|
// only migrate data if the realm database is empty.
|
2022-01-18 13:41:02 +08:00
|
|
|
if (realm.All<ScoreInfo>().Any())
|
|
|
|
{
|
|
|
|
Logger.Log("Skipping migration as realm already has scores loaded", LoggingTarget.Database);
|
|
|
|
}
|
|
|
|
else
|
2022-01-13 16:51:23 +08:00
|
|
|
{
|
2022-01-19 15:01:17 +08:00
|
|
|
var transaction = realm.BeginWrite();
|
|
|
|
int written = 0;
|
|
|
|
|
|
|
|
try
|
2022-01-13 16:51:23 +08:00
|
|
|
{
|
2022-01-18 13:41:02 +08:00
|
|
|
foreach (var score in existingScores)
|
2022-01-13 16:51:23 +08:00
|
|
|
{
|
2022-01-19 15:01:17 +08:00
|
|
|
if (++written % 1000 == 0)
|
|
|
|
{
|
|
|
|
transaction.Commit();
|
|
|
|
transaction = realm.BeginWrite();
|
|
|
|
Logger.Log($"Migrated {written}/{count} scores...", LoggingTarget.Database);
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:22:17 +08:00
|
|
|
var beatmap = realm.All<BeatmapInfo>().First(b => b.Hash == score.BeatmapInfo.Hash);
|
|
|
|
var ruleset = realm.Find<RulesetInfo>(score.Ruleset.ShortName);
|
|
|
|
var user = new RealmUser
|
|
|
|
{
|
|
|
|
OnlineID = score.User.OnlineID,
|
|
|
|
Username = score.User.Username
|
|
|
|
};
|
|
|
|
|
|
|
|
var realmScore = new ScoreInfo(beatmap, ruleset, user)
|
2022-01-18 13:41:02 +08:00
|
|
|
{
|
|
|
|
Hash = score.Hash,
|
|
|
|
DeletePending = score.DeletePending,
|
|
|
|
OnlineID = score.OnlineID ?? -1,
|
|
|
|
ModsJson = score.ModsJson,
|
|
|
|
StatisticsJson = score.StatisticsJson,
|
|
|
|
TotalScore = score.TotalScore,
|
|
|
|
MaxCombo = score.MaxCombo,
|
|
|
|
Accuracy = score.Accuracy,
|
|
|
|
HasReplay = ((IScoreInfo)score).HasReplay,
|
|
|
|
Date = score.Date,
|
|
|
|
PP = score.PP,
|
|
|
|
Rank = score.Rank,
|
|
|
|
HitEvents = score.HitEvents,
|
|
|
|
Passed = score.Passed,
|
|
|
|
Combo = score.Combo,
|
|
|
|
Position = score.Position,
|
|
|
|
Statistics = score.Statistics,
|
|
|
|
Mods = score.Mods,
|
|
|
|
APIMods = score.APIMods,
|
|
|
|
};
|
|
|
|
|
|
|
|
migrateFiles(score, realm, realmScore);
|
|
|
|
|
|
|
|
realm.Add(realmScore);
|
|
|
|
}
|
2022-01-19 15:01:17 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2022-01-18 13:41:02 +08:00
|
|
|
transaction.Commit();
|
2022-01-13 16:51:23 +08:00
|
|
|
}
|
2022-01-19 15:01:17 +08:00
|
|
|
|
|
|
|
Logger.Log($"Successfully migrated {count} scores to realm", LoggingTarget.Database);
|
2022-01-13 16:51:23 +08:00
|
|
|
}
|
2022-01-21 16:08:20 +08:00
|
|
|
});
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-19 14:52:59 +08:00
|
|
|
private void migrateSkins(OsuDbContext db)
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2021-12-07 03:12:02 +08:00
|
|
|
// can be removed 20220530.
|
2022-01-19 14:52:59 +08:00
|
|
|
var existingSkins = db.SkinInfo
|
2021-12-07 03:12:02 +08:00
|
|
|
.Include(s => s.Files)
|
|
|
|
.ThenInclude(f => f.FileInfo)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
// previous entries in EF are removed post migration.
|
|
|
|
if (!existingSkins.Any())
|
|
|
|
return;
|
|
|
|
|
2021-11-30 14:41:18 +08:00
|
|
|
var userSkinChoice = config.GetBindable<string>(OsuSetting.Skin);
|
|
|
|
int.TryParse(userSkinChoice.Value, out int userSkinInt);
|
|
|
|
|
|
|
|
switch (userSkinInt)
|
|
|
|
{
|
|
|
|
case EFSkinInfo.DEFAULT_SKIN:
|
|
|
|
userSkinChoice.Value = SkinInfo.DEFAULT_SKIN.ToString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EFSkinInfo.CLASSIC_SKIN:
|
|
|
|
userSkinChoice.Value = SkinInfo.CLASSIC_SKIN.ToString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
realmContextFactory.Run(realm =>
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
using (var transaction = realm.BeginWrite())
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
// only migrate data if the realm database is empty.
|
|
|
|
// note that this cannot be written as: `realm.All<SkinInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
|
|
|
|
if (!realm.All<SkinInfo>().Any(s => !s.Protected))
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
Logger.Log($"Migrating {existingSkins.Count} skins", LoggingTarget.Database);
|
|
|
|
|
|
|
|
foreach (var skin in existingSkins)
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
var realmSkin = new SkinInfo
|
|
|
|
{
|
|
|
|
Name = skin.Name,
|
|
|
|
Creator = skin.Creator,
|
|
|
|
Hash = skin.Hash,
|
|
|
|
Protected = false,
|
|
|
|
InstantiationInfo = skin.InstantiationInfo,
|
|
|
|
};
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
migrateFiles(skin, realm, realmSkin);
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
realm.Add(realmSkin);
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
if (skin.ID == userSkinInt)
|
|
|
|
userSkinChoice.Value = realmSkin.ID.ToString();
|
|
|
|
}
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
});
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:51:23 +08:00
|
|
|
private static void migrateFiles<T>(IHasFiles<T> fileSource, Realm realm, IHasRealmFiles realmObject) where T : INamedFileInfo
|
|
|
|
{
|
|
|
|
foreach (var file in fileSource.Files)
|
|
|
|
{
|
|
|
|
var realmFile = realm.Find<RealmFile>(file.FileInfo.Hash);
|
|
|
|
|
|
|
|
if (realmFile == null)
|
|
|
|
realm.Add(realmFile = new RealmFile { Hash = file.FileInfo.Hash });
|
|
|
|
|
|
|
|
realmObject.Files.Add(new RealmNamedFileUsage(realmFile, file.Filename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:52:59 +08:00
|
|
|
private void migrateSettings(OsuDbContext db)
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
|
|
|
// migrate ruleset settings. can be removed 20220315.
|
2022-01-19 14:52:59 +08:00
|
|
|
var existingSettings = db.DatabasedSetting.ToList();
|
2021-11-30 14:41:18 +08:00
|
|
|
|
|
|
|
// previous entries in EF are removed post migration.
|
|
|
|
if (!existingSettings.Any())
|
|
|
|
return;
|
|
|
|
|
2022-01-18 13:19:31 +08:00
|
|
|
Logger.Log("Beginning settings migration to realm", LoggingTarget.Database);
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
realmContextFactory.Run(realm =>
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
using (var transaction = realm.BeginWrite())
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
// only migrate data if the realm database is empty.
|
|
|
|
if (!realm.All<RealmRulesetSetting>().Any())
|
2021-11-30 14:41:18 +08:00
|
|
|
{
|
2022-01-21 16:08:20 +08:00
|
|
|
Logger.Log($"Migrating {existingSettings.Count} settings", LoggingTarget.Database);
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
foreach (var dkb in existingSettings)
|
|
|
|
{
|
|
|
|
if (dkb.RulesetID == null)
|
|
|
|
continue;
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
string? shortName = getRulesetShortNameFromLegacyID(dkb.RulesetID.Value);
|
2021-11-30 14:41:18 +08:00
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
if (string.IsNullOrEmpty(shortName))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
realm.Add(new RealmRulesetSetting
|
|
|
|
{
|
|
|
|
Key = dkb.Key,
|
|
|
|
Value = dkb.StringValue,
|
|
|
|
RulesetName = shortName,
|
|
|
|
Variant = dkb.Variant ?? 0,
|
|
|
|
});
|
|
|
|
}
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
});
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
|
|
|
|
efContextFactory.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
|
2022-01-19 15:28:16 +08:00
|
|
|
|
2022-01-20 23:01:34 +08:00
|
|
|
private void createBackup()
|
2022-01-19 15:28:16 +08:00
|
|
|
{
|
2022-01-20 23:01:34 +08:00
|
|
|
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
|
2022-01-19 15:28:16 +08:00
|
|
|
|
2022-01-20 23:01:34 +08:00
|
|
|
efContextFactory.CreateBackup($"client.{migration}.db");
|
|
|
|
realmContextFactory.CreateBackup($"client.{migration}.realm");
|
2022-01-19 15:28:16 +08:00
|
|
|
|
2022-01-20 23:01:34 +08:00
|
|
|
using (var source = storage.GetStream("collection.db"))
|
|
|
|
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
|
|
|
|
source.CopyTo(destination);
|
2022-01-19 15:28:16 +08:00
|
|
|
}
|
2021-11-30 14:41:18 +08:00
|
|
|
}
|
|
|
|
}
|