2018-01-05 11:21:19 +00:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-10-15 15:53:59 +00:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-10-14 06:16:08 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-10-22 07:18:19 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2017-10-20 00:25:54 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2017-10-14 06:16:08 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using osu.Framework.Logging;
|
2017-10-04 19:52:12 +00:00
|
|
|
|
using osu.Game.Beatmaps;
|
2018-01-24 08:35:37 +00:00
|
|
|
|
using osu.Game.Configuration;
|
2017-10-04 19:52:12 +00:00
|
|
|
|
using osu.Game.IO;
|
|
|
|
|
using osu.Game.Rulesets;
|
2018-01-24 08:35:37 +00:00
|
|
|
|
using DatabasedKeyBinding = osu.Game.Input.Bindings.DatabasedKeyBinding;
|
2017-10-14 06:16:08 +00:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2018-02-15 04:45:39 +00:00
|
|
|
|
using osu.Game.Skinning;
|
2017-10-04 19:52:12 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public class OsuDbContext : DbContext
|
|
|
|
|
{
|
2017-10-15 15:53:59 +00:00
|
|
|
|
public DbSet<BeatmapInfo> BeatmapInfo { get; set; }
|
2017-10-19 08:50:46 +00:00
|
|
|
|
public DbSet<BeatmapDifficulty> BeatmapDifficulty { get; set; }
|
|
|
|
|
public DbSet<BeatmapMetadata> BeatmapMetadata { get; set; }
|
2017-10-15 15:53:59 +00:00
|
|
|
|
public DbSet<BeatmapSetInfo> BeatmapSetInfo { get; set; }
|
|
|
|
|
public DbSet<DatabasedKeyBinding> DatabasedKeyBinding { get; set; }
|
2018-01-24 08:35:37 +00:00
|
|
|
|
public DbSet<DatabasedSetting> DatabasedSetting { get; set; }
|
2017-10-15 15:53:59 +00:00
|
|
|
|
public DbSet<FileInfo> FileInfo { get; set; }
|
|
|
|
|
public DbSet<RulesetInfo> RulesetInfo { get; set; }
|
2018-02-15 04:45:39 +00:00
|
|
|
|
public DbSet<SkinInfo> SkinInfo { get; set; }
|
2017-10-23 07:35:35 +00:00
|
|
|
|
|
2017-10-04 19:52:12 +00:00
|
|
|
|
private readonly string connectionString;
|
|
|
|
|
|
2017-10-19 08:29:39 +00:00
|
|
|
|
private static readonly Lazy<OsuDbLoggerFactory> logger = new Lazy<OsuDbLoggerFactory>(() => new OsuDbLoggerFactory());
|
|
|
|
|
|
2017-10-16 02:06:19 +00:00
|
|
|
|
static OsuDbContext()
|
2017-10-04 19:52:12 +00:00
|
|
|
|
{
|
2017-10-16 02:06:19 +00:00
|
|
|
|
// required to initialise native SQLite libraries on some platforms.
|
|
|
|
|
SQLitePCL.Batteries_V2.Init();
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 08:52:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new in-memory OsuDbContext instance.
|
|
|
|
|
/// </summary>
|
2017-10-19 07:35:06 +00:00
|
|
|
|
public OsuDbContext()
|
|
|
|
|
: this("DataSource=:memory:")
|
2017-10-17 08:52:02 +00:00
|
|
|
|
{
|
|
|
|
|
// required for tooling (see https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0).
|
2017-10-23 10:29:47 +00:00
|
|
|
|
|
|
|
|
|
Migrate();
|
2017-10-17 08:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-16 02:06:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new OsuDbContext instance.
|
|
|
|
|
/// </summary>
|
2017-10-17 08:52:02 +00:00
|
|
|
|
/// <param name="connectionString">A valid SQLite connection string.</param>
|
|
|
|
|
public OsuDbContext(string connectionString)
|
2017-10-04 19:52:12 +00:00
|
|
|
|
{
|
|
|
|
|
this.connectionString = connectionString;
|
2017-10-16 02:06:19 +00:00
|
|
|
|
|
|
|
|
|
var connection = Database.GetDbConnection();
|
|
|
|
|
connection.Open();
|
|
|
|
|
using (var cmd = connection.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = "PRAGMA journal_mode=WAL;";
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnConfiguring(optionsBuilder);
|
2017-10-20 00:25:54 +00: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 07:18:19 +00:00
|
|
|
|
.UseSqlite(connectionString, sqliteOptions => sqliteOptions.CommandTimeout(10))
|
2017-10-20 00:25:54 +00:00
|
|
|
|
.UseLoggerFactory(logger.Value);
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
2017-10-19 05:05:11 +00:00
|
|
|
|
|
2017-11-18 16:42:13 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.OnlineBeatmapID).IsUnique();
|
2017-10-25 06:51:39 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.MD5Hash).IsUnique();
|
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.Hash).IsUnique();
|
2017-10-19 05:05:11 +00:00
|
|
|
|
|
2017-10-25 06:51:39 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.OnlineBeatmapSetID).IsUnique();
|
2017-10-04 19:52:12 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.DeletePending);
|
2017-10-25 06:51:39 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.Hash).IsUnique();
|
2017-10-19 05:05:11 +00:00
|
|
|
|
|
2018-01-24 08:35:37 +00:00
|
|
|
|
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => new { b.RulesetID, b.Variant });
|
2017-10-04 19:52:12 +00:00
|
|
|
|
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => b.IntAction);
|
2017-10-19 05:05:11 +00:00
|
|
|
|
|
2018-01-25 14:41:03 +00:00
|
|
|
|
modelBuilder.Entity<DatabasedSetting>().HasIndex(b => new { b.RulesetID, b.Variant });
|
2018-01-24 08:35:37 +00:00
|
|
|
|
|
2017-10-04 19:52:12 +00:00
|
|
|
|
modelBuilder.Entity<FileInfo>().HasIndex(b => b.Hash).IsUnique();
|
|
|
|
|
modelBuilder.Entity<FileInfo>().HasIndex(b => b.ReferenceCount);
|
2017-10-19 05:05:11 +00:00
|
|
|
|
|
2017-10-04 19:52:12 +00:00
|
|
|
|
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.Available);
|
2017-12-09 03:56:44 +00:00
|
|
|
|
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.ShortName).IsUnique();
|
2017-10-17 09:26:28 +00:00
|
|
|
|
|
2017-10-19 05:05:11 +00:00
|
|
|
|
modelBuilder.Entity<BeatmapInfo>().HasOne(b => b.BaseDifficulty);
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|
2017-10-14 06:16:08 +00:00
|
|
|
|
|
2017-10-23 05:53:13 +00:00
|
|
|
|
public IDbContextTransaction BeginTransaction()
|
|
|
|
|
{
|
|
|
|
|
// return Database.BeginTransaction();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 13:24:11 +00:00
|
|
|
|
public int SaveChanges(IDbContextTransaction transaction = null)
|
2017-10-22 07:17:40 +00:00
|
|
|
|
{
|
|
|
|
|
var ret = base.SaveChanges();
|
2018-02-12 10:57:21 +00:00
|
|
|
|
if (ret > 0) transaction?.Commit();
|
2017-10-22 07:17:40 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-14 06:16:08 +00:00
|
|
|
|
private class OsuDbLoggerFactory : ILoggerFactory
|
|
|
|
|
{
|
2017-10-15 15:53:59 +00:00
|
|
|
|
#region Disposal
|
|
|
|
|
|
2017-10-14 06:16:08 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 15:53:59 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2017-10-14 06:16:08 +00:00
|
|
|
|
public ILogger CreateLogger(string categoryName) => new OsuDbLogger();
|
|
|
|
|
|
2017-10-16 03:51:46 +00:00
|
|
|
|
public void AddProvider(ILoggerProvider provider)
|
|
|
|
|
{
|
2017-10-17 08:52:02 +00:00
|
|
|
|
// no-op. called by tooling.
|
2017-10-16 03:51:46 +00:00
|
|
|
|
}
|
2017-10-14 06:16:08 +00:00
|
|
|
|
|
|
|
|
|
private class OsuDbLoggerProvider : ILoggerProvider
|
|
|
|
|
{
|
2017-10-15 15:53:59 +00:00
|
|
|
|
#region Disposal
|
|
|
|
|
|
2017-10-14 06:16:08 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 15:53:59 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2017-10-14 06:16:08 +00: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 07:35:06 +00: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 06:16:08 +00:00
|
|
|
|
|
2017-10-19 07:35:06 +00:00
|
|
|
|
public bool IsEnabled(LogLevel logLevel)
|
|
|
|
|
{
|
2017-10-22 10:46:08 +00:00
|
|
|
|
#if DEBUG_DATABASE
|
2017-10-19 07:35:06 +00:00
|
|
|
|
return logLevel > LogLevel.Debug;
|
|
|
|
|
#else
|
|
|
|
|
return logLevel > LogLevel.Information;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2017-10-14 06:16:08 +00:00
|
|
|
|
|
|
|
|
|
public IDisposable BeginScope<TState>(TState state) => null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-19 10:41:16 +00:00
|
|
|
|
|
|
|
|
|
public void Migrate()
|
|
|
|
|
{
|
2017-10-19 15:48:27 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Database.Migrate();
|
|
|
|
|
}
|
2017-10-19 23:01:45 +00:00
|
|
|
|
catch (Exception e)
|
2017-10-19 15:48:27 +00:00
|
|
|
|
{
|
2017-10-19 23:01:45 +00:00
|
|
|
|
throw new MigrationFailedException(e);
|
2017-10-19 15:48:27 +00:00
|
|
|
|
}
|
2017-10-19 10:41:16 +00:00
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|
2017-10-19 12:37:09 +00:00
|
|
|
|
|
|
|
|
|
public class MigrationFailedException : Exception
|
|
|
|
|
{
|
2017-10-19 23:01:45 +00:00
|
|
|
|
public MigrationFailedException(Exception exception)
|
|
|
|
|
: base("sqlite-net migration failed", exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-10-19 12:37:09 +00:00
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|