From 63fe569afaead04c01fe7ca33891be05b72afce5 Mon Sep 17 00:00:00 2001 From: TocoToucan Date: Wed, 25 Oct 2017 14:53:20 +0300 Subject: [PATCH 1/2] Remove DatabaseBackedStore Prepare functions --- osu.Game/Beatmaps/BeatmapStore.cs | 27 +++++++++++------------- osu.Game/Database/DatabaseBackedStore.cs | 18 +--------------- osu.Game/IO/FileStore.cs | 19 +++++++---------- osu.Game/Input/KeyBindingStore.cs | 11 +++++----- osu.Game/Rulesets/RulesetStore.cs | 13 ++++++------ osu.Game/Rulesets/Scoring/ScoreStore.cs | 3 ++- 6 files changed, 35 insertions(+), 56 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapStore.cs b/osu.Game/Beatmaps/BeatmapStore.cs index 3322a4c236..6248dcc359 100644 --- a/osu.Game/Beatmaps/BeatmapStore.cs +++ b/osu.Game/Beatmaps/BeatmapStore.cs @@ -25,21 +25,6 @@ public BeatmapStore(Func factory) { } - protected override void Prepare(bool reset = false) - { - if (reset) - { - var context = GetContext(); - - // https://stackoverflow.com/a/10450893 - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapMetadata"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapDifficulty"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetInfo"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetFileInfo"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapInfo"); - } - } - /// /// Add a to the database. /// @@ -151,6 +136,18 @@ public override void Cleanup() context.SaveChanges(); } + public override void Reset() + { + var context = GetContext(); + + // https://stackoverflow.com/a/10450893 + context.Database.ExecuteSqlCommand("DELETE FROM BeatmapMetadata"); + context.Database.ExecuteSqlCommand("DELETE FROM BeatmapDifficulty"); + context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetInfo"); + context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetFileInfo"); + context.Database.ExecuteSqlCommand("DELETE FROM BeatmapInfo"); + } + public IEnumerable BeatmapSets => GetContext().BeatmapSetInfo .Include(s => s.Metadata) .Include(s => s.Beatmaps).ThenInclude(s => s.Ruleset) diff --git a/osu.Game/Database/DatabaseBackedStore.cs b/osu.Game/Database/DatabaseBackedStore.cs index 68f412eee6..f46c388ba6 100644 --- a/osu.Game/Database/DatabaseBackedStore.cs +++ b/osu.Game/Database/DatabaseBackedStore.cs @@ -3,7 +3,6 @@ using System; using System.Threading; -using osu.Framework.Logging; using osu.Framework.Platform; namespace osu.Game.Database @@ -32,16 +31,6 @@ protected DatabaseBackedStore(Func createContext, Storage storage queryContext = new ThreadLocal(CreateContext); Storage = storage; - - try - { - Prepare(); - } - catch (Exception e) - { - Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database..."); - Prepare(true); - } } /// @@ -51,14 +40,9 @@ public virtual void Cleanup() { } - /// - /// Prepare this database for use. Tables should be created here. - /// - protected abstract void Prepare(bool reset = false); - /// /// Reset this database to a default state. Undo all changes to database and storage backings. /// - public void Reset() => Prepare(true); + public abstract void Reset(); } } diff --git a/osu.Game/IO/FileStore.cs b/osu.Game/IO/FileStore.cs index b69916e565..9f63151ed1 100644 --- a/osu.Game/IO/FileStore.cs +++ b/osu.Game/IO/FileStore.cs @@ -27,17 +27,6 @@ public FileStore(Func createContext, Storage storage) : base(creat Store = new StorageBackedResourceStore(Storage); } - protected override void Prepare(bool reset = false) - { - if (reset) - { - if (Storage.ExistsDirectory(string.Empty)) - Storage.DeleteDirectory(string.Empty); - - GetContext().Database.ExecuteSqlCommand("DELETE FROM FileInfo"); - } - } - public FileInfo Add(Stream data, bool reference = true) { var context = GetContext(); @@ -114,5 +103,13 @@ public override void Cleanup() context.SaveChanges(); } + + public override void Reset() + { + if (Storage.ExistsDirectory(string.Empty)) + Storage.DeleteDirectory(string.Empty); + + GetContext().Database.ExecuteSqlCommand("DELETE FROM FileInfo"); + } } } diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index 1e1b1d74d4..ea99f84de9 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -30,12 +30,6 @@ public KeyBindingStore(Func createContext, RulesetStore rulesets, public void Register(KeyBindingInputManager manager) => insertDefaults(manager.DefaultKeyBindings); - protected override void Prepare(bool reset = false) - { - if (reset) - GetContext().Database.ExecuteSqlCommand("DELETE FROM KeyBinding"); - } - private void insertDefaults(IEnumerable defaults, int? rulesetId = null, int? variant = null) { var context = GetContext(); @@ -83,5 +77,10 @@ public void Update(KeyBinding keyBinding) KeyBindingChanged?.Invoke(); } + + public override void Reset() + { + GetContext().Database.ExecuteSqlCommand("DELETE FROM KeyBinding"); + } } } diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 7d982eb39e..958642f9aa 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -29,6 +29,7 @@ static RulesetStore() public RulesetStore(Func factory) : base(factory) { + AddMissingRulesets(); } /// @@ -47,15 +48,10 @@ public RulesetStore(Func factory) private const string ruleset_library_prefix = "osu.Game.Rulesets"; - protected override void Prepare(bool reset = false) + protected void AddMissingRulesets() { var context = GetContext(); - if (reset) - { - context.Database.ExecuteSqlCommand("DELETE FROM RulesetInfo"); - } - var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo())).ToList(); //add all legacy modes in correct order @@ -123,5 +119,10 @@ private static void loadRulesetFromFile(string file) InstantiationInfo = ruleset.GetType().AssemblyQualifiedName, ID = ruleset.LegacyID }; + + public override void Reset() + { + GetContext().Database.ExecuteSqlCommand("DELETE FROM RulesetInfo"); + } } } diff --git a/osu.Game/Rulesets/Scoring/ScoreStore.cs b/osu.Game/Rulesets/Scoring/ScoreStore.cs index 67a8e5372e..b971161f88 100644 --- a/osu.Game/Rulesets/Scoring/ScoreStore.cs +++ b/osu.Game/Rulesets/Scoring/ScoreStore.cs @@ -144,8 +144,9 @@ private Replay createLegacyReplay(StreamReader reader) return new Replay { Frames = frames }; } - protected override void Prepare(bool reset = false) + public override void Reset() { + throw new NotImplementedException(); } } } From 5107489cda8d212b6e4c9f632111c1d2674d804f Mon Sep 17 00:00:00 2001 From: TocoToucan Date: Wed, 25 Oct 2017 15:12:14 +0300 Subject: [PATCH 2/2] Remove DatabaseBackedStore Reset functions --- osu.Game/Beatmaps/BeatmapStore.cs | 12 ------------ osu.Game/Database/DatabaseBackedStore.cs | 5 ----- osu.Game/IO/FileStore.cs | 9 --------- osu.Game/Input/KeyBindingStore.cs | 6 ------ osu.Game/Rulesets/RulesetStore.cs | 6 ------ osu.Game/Rulesets/Scoring/ScoreStore.cs | 5 ----- 6 files changed, 43 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapStore.cs b/osu.Game/Beatmaps/BeatmapStore.cs index 6248dcc359..2e6efee0aa 100644 --- a/osu.Game/Beatmaps/BeatmapStore.cs +++ b/osu.Game/Beatmaps/BeatmapStore.cs @@ -136,18 +136,6 @@ public override void Cleanup() context.SaveChanges(); } - public override void Reset() - { - var context = GetContext(); - - // https://stackoverflow.com/a/10450893 - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapMetadata"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapDifficulty"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetInfo"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetFileInfo"); - context.Database.ExecuteSqlCommand("DELETE FROM BeatmapInfo"); - } - public IEnumerable BeatmapSets => GetContext().BeatmapSetInfo .Include(s => s.Metadata) .Include(s => s.Beatmaps).ThenInclude(s => s.Ruleset) diff --git a/osu.Game/Database/DatabaseBackedStore.cs b/osu.Game/Database/DatabaseBackedStore.cs index f46c388ba6..04aa8f91ae 100644 --- a/osu.Game/Database/DatabaseBackedStore.cs +++ b/osu.Game/Database/DatabaseBackedStore.cs @@ -39,10 +39,5 @@ protected DatabaseBackedStore(Func createContext, Storage storage public virtual void Cleanup() { } - - /// - /// Reset this database to a default state. Undo all changes to database and storage backings. - /// - public abstract void Reset(); } } diff --git a/osu.Game/IO/FileStore.cs b/osu.Game/IO/FileStore.cs index 9f63151ed1..93d1086ee5 100644 --- a/osu.Game/IO/FileStore.cs +++ b/osu.Game/IO/FileStore.cs @@ -4,7 +4,6 @@ using System; using System.IO; using System.Linq; -using Microsoft.EntityFrameworkCore; using osu.Framework.Extensions; using osu.Framework.IO.Stores; using osu.Framework.Logging; @@ -103,13 +102,5 @@ public override void Cleanup() context.SaveChanges(); } - - public override void Reset() - { - if (Storage.ExistsDirectory(string.Empty)) - Storage.DeleteDirectory(string.Empty); - - GetContext().Database.ExecuteSqlCommand("DELETE FROM FileInfo"); - } } } diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index ea99f84de9..53309fc72d 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Microsoft.EntityFrameworkCore; using osu.Framework.Input.Bindings; using osu.Framework.Platform; using osu.Game.Database; @@ -77,10 +76,5 @@ public void Update(KeyBinding keyBinding) KeyBindingChanged?.Invoke(); } - - public override void Reset() - { - GetContext().Database.ExecuteSqlCommand("DELETE FROM KeyBinding"); - } } } diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 958642f9aa..4208d0edad 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Reflection; -using Microsoft.EntityFrameworkCore; using osu.Game.Database; namespace osu.Game.Rulesets @@ -119,10 +118,5 @@ private static void loadRulesetFromFile(string file) InstantiationInfo = ruleset.GetType().AssemblyQualifiedName, ID = ruleset.LegacyID }; - - public override void Reset() - { - GetContext().Database.ExecuteSqlCommand("DELETE FROM RulesetInfo"); - } } } diff --git a/osu.Game/Rulesets/Scoring/ScoreStore.cs b/osu.Game/Rulesets/Scoring/ScoreStore.cs index b971161f88..d8a79d8cfb 100644 --- a/osu.Game/Rulesets/Scoring/ScoreStore.cs +++ b/osu.Game/Rulesets/Scoring/ScoreStore.cs @@ -143,10 +143,5 @@ private Replay createLegacyReplay(StreamReader reader) return new Replay { Frames = frames }; } - - public override void Reset() - { - throw new NotImplementedException(); - } } }