2017-10-15 23:53:59 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-10-14 14:16:08 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-10-22 15:18:19 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2017-10-20 08:25:54 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2017-10-14 14:16:08 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using osu.Framework.Logging;
|
2017-10-05 03:52:12 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
using osu.Game.Input.Bindings;
|
|
|
|
|
using osu.Game.IO;
|
|
|
|
|
using osu.Game.Rulesets;
|
2017-10-14 14:16:08 +08:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2017-10-05 03:52:12 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public class OsuDbContext : DbContext
|
|
|
|
|
{
|
2017-10-15 23:53:59 +08:00
|
|
|
|
public DbSet<BeatmapInfo> BeatmapInfo { get; set; }
|
2017-10-19 16:50:46 +08:00
|
|
|
|
public DbSet<BeatmapDifficulty> BeatmapDifficulty { get; set; }
|
|
|
|
|
public DbSet<BeatmapMetadata> BeatmapMetadata { get; set; }
|
2017-10-15 23:53:59 +08:00
|
|
|
|
public DbSet<BeatmapSetInfo> BeatmapSetInfo { get; set; }
|
|
|
|
|
public DbSet<DatabasedKeyBinding> DatabasedKeyBinding { get; set; }
|
|
|
|
|
public DbSet<FileInfo> FileInfo { get; set; }
|
|
|
|
|
public DbSet<RulesetInfo> RulesetInfo { get; set; }
|
2017-10-23 15:35:35 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
private readonly string connectionString;
|
|
|
|
|
|
2017-10-19 16:29:39 +08:00
|
|
|
|
private static readonly Lazy<OsuDbLoggerFactory> logger = new Lazy<OsuDbLoggerFactory>(() => new OsuDbLoggerFactory());
|
|
|
|
|
|
2017-10-16 10:06:19 +08:00
|
|
|
|
static OsuDbContext()
|
2017-10-05 03:52:12 +08:00
|
|
|
|
{
|
2017-10-16 10:06:19 +08:00
|
|
|
|
// required to initialise native SQLite libraries on some platforms.
|
|
|
|
|
SQLitePCL.Batteries_V2.Init();
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 16:52:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new in-memory OsuDbContext instance.
|
|
|
|
|
/// </summary>
|
2017-10-19 15:35:06 +08:00
|
|
|
|
public OsuDbContext()
|
|
|
|
|
: this("DataSource=:memory:")
|
2017-10-17 16:52:02 +08:00
|
|
|
|
{
|
|
|
|
|
// required for tooling (see https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0).
|
2017-10-23 18:29:47 +08:00
|
|
|
|
|
|
|
|
|
Migrate();
|
2017-10-17 16:52:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-16 10:06:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new OsuDbContext instance.
|
|
|
|
|
/// </summary>
|
2017-10-17 16:52:02 +08:00
|
|
|
|
/// <param name="connectionString">A valid SQLite connection string.</param>
|
|
|
|
|
public OsuDbContext(string connectionString)
|
2017-10-05 03:52:12 +08:00
|
|
|
|
{
|
|
|
|
|
this.connectionString = connectionString;
|
2017-10-16 10:06:19 +08:00
|
|
|
|
|
|
|
|
|
var connection = Database.GetDbConnection();
|
|
|
|
|
connection.Open();
|
|
|
|
|
using (var cmd = connection.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = "PRAGMA journal_mode=WAL;";
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnConfiguring(optionsBuilder);
|
2017-10-20 08:25:54 +08:00
|
|
|
|
optionsBuilder
|
|
|
|
|
// this is required for the time being due to the way we are querying in places like BeatmapStore.
|
|
|
|
|
// if we ever move to having consumers file their own .Includes, or get eager loading support, this could be re-enabled.
|
|
|
|
|
.ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.IncludeIgnoredWarning))
|
2017-10-22 15:18:19 +08:00
|
|
|
|
.UseSqlite(connectionString, sqliteOptions => sqliteOptions.CommandTimeout(10))
|
2017-10-20 08:25:54 +08:00
|
|
|
|
.UseLoggerFactory(logger.Value);
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
2017-10-19 13:05:11 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.MD5Hash);
|
2017-10-17 18:59:22 +08:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.Hash);
|
2017-10-19 13:05:11 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.DeletePending);
|
2017-10-17 18:59:22 +08:00
|
|
|
|
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.Hash);
|
2017-10-19 13:05:11 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => b.Variant);
|
|
|
|
|
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => b.IntAction);
|
2017-10-19 13:05:11 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
modelBuilder.Entity<FileInfo>().HasIndex(b => b.Hash).IsUnique();
|
|
|
|
|
modelBuilder.Entity<FileInfo>().HasIndex(b => b.ReferenceCount);
|
2017-10-19 13:05:11 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.Available);
|
2017-10-17 17:26:28 +08:00
|
|
|
|
|
2017-10-19 13:05:11 +08:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasOne(b => b.BaseDifficulty);
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
2017-10-14 14:16:08 +08:00
|
|
|
|
|
2017-10-23 13:53:13 +08:00
|
|
|
|
public IDbContextTransaction BeginTransaction()
|
|
|
|
|
{
|
|
|
|
|
// return Database.BeginTransaction();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 15:17:40 +08:00
|
|
|
|
public new int SaveChanges(IDbContextTransaction transaction = null)
|
|
|
|
|
{
|
|
|
|
|
var ret = base.SaveChanges();
|
|
|
|
|
transaction?.Commit();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-14 14:16:08 +08:00
|
|
|
|
private class OsuDbLoggerFactory : ILoggerFactory
|
|
|
|
|
{
|
2017-10-15 23:53:59 +08:00
|
|
|
|
#region Disposal
|
|
|
|
|
|
2017-10-14 14:16:08 +08:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 23:53:59 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2017-10-14 14:16:08 +08:00
|
|
|
|
public ILogger CreateLogger(string categoryName) => new OsuDbLogger();
|
|
|
|
|
|
2017-10-16 11:51:46 +08:00
|
|
|
|
public void AddProvider(ILoggerProvider provider)
|
|
|
|
|
{
|
2017-10-17 16:52:02 +08:00
|
|
|
|
// no-op. called by tooling.
|
2017-10-16 11:51:46 +08:00
|
|
|
|
}
|
2017-10-14 14:16:08 +08:00
|
|
|
|
|
|
|
|
|
private class OsuDbLoggerProvider : ILoggerProvider
|
|
|
|
|
{
|
2017-10-15 23:53:59 +08:00
|
|
|
|
#region Disposal
|
|
|
|
|
|
2017-10-14 14:16:08 +08:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 23:53:59 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2017-10-14 14:16:08 +08:00
|
|
|
|
public ILogger CreateLogger(string categoryName) => new OsuDbLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class OsuDbLogger : ILogger
|
|
|
|
|
{
|
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
2017-10-19 15:35:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (logLevel < LogLevel.Information)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Framework.Logging.LogLevel frameworkLogLevel;
|
|
|
|
|
|
|
|
|
|
switch (logLevel)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
frameworkLogLevel = Framework.Logging.LogLevel.Debug;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Warning:
|
|
|
|
|
frameworkLogLevel = Framework.Logging.LogLevel.Important;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Error:
|
|
|
|
|
case LogLevel.Critical:
|
|
|
|
|
frameworkLogLevel = Framework.Logging.LogLevel.Error;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Log(formatter(state, exception), LoggingTarget.Database, frameworkLogLevel);
|
|
|
|
|
}
|
2017-10-14 14:16:08 +08:00
|
|
|
|
|
2017-10-19 15:35:06 +08:00
|
|
|
|
public bool IsEnabled(LogLevel logLevel)
|
|
|
|
|
{
|
2017-10-22 18:46:08 +08:00
|
|
|
|
#if DEBUG_DATABASE
|
2017-10-19 15:35:06 +08:00
|
|
|
|
return logLevel > LogLevel.Debug;
|
|
|
|
|
#else
|
|
|
|
|
return logLevel > LogLevel.Information;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2017-10-14 14:16:08 +08:00
|
|
|
|
|
|
|
|
|
public IDisposable BeginScope<TState>(TState state) => null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
|
|
|
|
public void Migrate()
|
|
|
|
|
{
|
|
|
|
|
migrateFromSqliteNet();
|
2017-10-20 07:01:45 +08:00
|
|
|
|
|
2017-10-19 23:48:27 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Database.Migrate();
|
|
|
|
|
}
|
2017-10-20 07:01:45 +08:00
|
|
|
|
catch (Exception e)
|
2017-10-19 23:48:27 +08:00
|
|
|
|
{
|
2017-10-20 07:01:45 +08:00
|
|
|
|
throw new MigrationFailedException(e);
|
2017-10-19 23:48:27 +08:00
|
|
|
|
}
|
2017-10-19 18:41:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void migrateFromSqliteNet()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-10-22 14:32:47 +08:00
|
|
|
|
// will fail if the database isn't in a sane EF-migrated state.
|
2017-10-22 14:27:16 +08:00
|
|
|
|
Database.ExecuteSqlCommand("SELECT MetadataID FROM BeatmapSetInfo LIMIT 1");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-10-22 14:27:16 +08:00
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE IF EXISTS __EFMigrationsHistory");
|
|
|
|
|
|
2017-10-19 18:41:16 +08:00
|
|
|
|
// will fail (intentionally) if we don't have sqlite-net data present.
|
|
|
|
|
Database.ExecuteSqlCommand("SELECT OnlineBeatmapSetId FROM BeatmapMetadata LIMIT 1");
|
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-10-20 07:01:45 +08:00
|
|
|
|
Logger.Log("Performing migration from sqlite-net to EF...", LoggingTarget.Database, Framework.Logging.LogLevel.Important);
|
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
// we are good to perform messy migration of data!.
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE BeatmapDifficulty RENAME TO BeatmapDifficulty_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE BeatmapMetadata RENAME TO BeatmapMetadata_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE FileInfo RENAME TO FileInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE KeyBinding RENAME TO KeyBinding_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE BeatmapSetInfo RENAME TO BeatmapSetInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE BeatmapInfo RENAME TO BeatmapInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE BeatmapSetFileInfo RENAME TO BeatmapSetFileInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("ALTER TABLE RulesetInfo RENAME TO RulesetInfo_Old");
|
|
|
|
|
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE StoreVersion");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
// perform EF migrations to create sane table structure.
|
|
|
|
|
Database.Migrate();
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
// copy data table by table to new structure, dropping old tables as we go.
|
|
|
|
|
Database.ExecuteSqlCommand("INSERT INTO FileInfo SELECT * FROM FileInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE FileInfo_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand("INSERT INTO KeyBinding SELECT ID, [Action], Keys, RulesetID, Variant FROM KeyBinding_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE KeyBinding_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand(
|
|
|
|
|
"INSERT INTO BeatmapMetadata SELECT ID, Artist, ArtistUnicode, AudioFile, Author, BackgroundFile, PreviewTime, Source, Tags, Title, TitleUnicode FROM BeatmapMetadata_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE BeatmapMetadata_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand(
|
|
|
|
|
"INSERT INTO BeatmapDifficulty SELECT `ID`, `ApproachRate`, `CircleSize`, `DrainRate`, `OverallDifficulty`, `SliderMultiplier`, `SliderTickRate` FROM BeatmapDifficulty_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE BeatmapDifficulty_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand("INSERT INTO BeatmapSetInfo SELECT ID, DeletePending, Hash, BeatmapMetadataID, OnlineBeatmapSetID, Protected FROM BeatmapSetInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE BeatmapSetInfo_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand("INSERT INTO BeatmapSetFileInfo SELECT ID, BeatmapSetInfoID, FileInfoID, Filename FROM BeatmapSetFileInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE BeatmapSetFileInfo_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand("INSERT INTO RulesetInfo SELECT ID, Available, InstantiationInfo, Name FROM RulesetInfo_Old");
|
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE RulesetInfo_Old");
|
2017-10-19 18:41:16 +08:00
|
|
|
|
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand(
|
2017-10-19 22:19:30 +08:00
|
|
|
|
"INSERT INTO BeatmapInfo SELECT ID, AudioLeadIn, BaseDifficultyID, BeatDivisor, BeatmapSetInfoID, Countdown, DistanceSpacing, GridSize, Hash, IFNULL(Hidden, 0), LetterboxInBreaks, MD5Hash, NULLIF(BeatmapMetadataID, 0), OnlineBeatmapID, Path, RulesetID, SpecialStyle, StackLeniency, StarDifficulty, StoredBookmarks, TimelineZoom, Version, WidescreenStoryboard FROM BeatmapInfo_Old");
|
2017-10-19 20:11:43 +08:00
|
|
|
|
Database.ExecuteSqlCommand("DROP TABLE BeatmapInfo_Old");
|
2017-10-20 07:01:45 +08:00
|
|
|
|
|
|
|
|
|
Logger.Log("Migration complete!", LoggingTarget.Database, Framework.Logging.LogLevel.Important);
|
2017-10-19 20:11:43 +08:00
|
|
|
|
}
|
2017-10-20 07:01:45 +08:00
|
|
|
|
catch (Exception e)
|
2017-10-19 20:11:43 +08:00
|
|
|
|
{
|
2017-10-20 07:01:45 +08:00
|
|
|
|
throw new MigrationFailedException(e);
|
2017-10-19 20:11:43 +08:00
|
|
|
|
}
|
2017-10-19 18:41:16 +08:00
|
|
|
|
}
|
2017-10-19 20:37:09 +08:00
|
|
|
|
catch (MigrationFailedException e)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2017-10-19 18:41:16 +08:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
2017-10-19 20:37:09 +08:00
|
|
|
|
|
|
|
|
|
public class MigrationFailedException : Exception
|
|
|
|
|
{
|
2017-10-20 07:01:45 +08:00
|
|
|
|
public MigrationFailedException(Exception exception)
|
|
|
|
|
: base("sqlite-net migration failed", exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-10-19 20:37:09 +08:00
|
|
|
|
}
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|