// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Logging; using osu.Framework.Logging; using osu.Game.Beatmaps; using osu.Game.Configuration; using osu.Game.IO; using osu.Game.Rulesets; using DatabasedKeyBinding = osu.Game.Input.Bindings.DatabasedKeyBinding; using LogLevel = Microsoft.Extensions.Logging.LogLevel; using osu.Game.Skinning; namespace osu.Game.Database { public class OsuDbContext : DbContext { public DbSet BeatmapInfo { get; set; } public DbSet BeatmapDifficulty { get; set; } public DbSet BeatmapMetadata { get; set; } public DbSet BeatmapSetInfo { get; set; } public DbSet DatabasedKeyBinding { get; set; } public DbSet DatabasedSetting { get; set; } public DbSet FileInfo { get; set; } public DbSet RulesetInfo { get; set; } public DbSet SkinInfo { get; set; } private readonly string connectionString; private static readonly Lazy logger = new Lazy(() => new OsuDbLoggerFactory()); /// /// Create a new in-memory OsuDbContext instance. /// public OsuDbContext() : this("DataSource=:memory:") { // required for tooling (see https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0). Migrate(); } /// /// Create a new OsuDbContext instance. /// /// A valid SQLite connection string. public OsuDbContext(string connectionString) { this.connectionString = connectionString; var connection = Database.GetDbConnection(); connection.Open(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = "PRAGMA journal_mode=WAL;"; cmd.ExecuteNonQuery(); } } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); 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)) .UseSqlite(connectionString, sqliteOptions => sqliteOptions.CommandTimeout(10)) .UseLoggerFactory(logger.Value); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasIndex(b => b.OnlineBeatmapID).IsUnique(); modelBuilder.Entity().HasIndex(b => b.MD5Hash).IsUnique(); modelBuilder.Entity().HasIndex(b => b.Hash).IsUnique(); modelBuilder.Entity().HasIndex(b => b.OnlineBeatmapSetID).IsUnique(); modelBuilder.Entity().HasIndex(b => b.DeletePending); modelBuilder.Entity().HasIndex(b => b.Hash).IsUnique(); modelBuilder.Entity().HasIndex(b => new { b.RulesetID, b.Variant }); modelBuilder.Entity().HasIndex(b => b.IntAction); modelBuilder.Entity().HasIndex(b => new { b.RulesetID, b.Variant }); modelBuilder.Entity().HasIndex(b => b.Hash).IsUnique(); modelBuilder.Entity().HasIndex(b => b.ReferenceCount); modelBuilder.Entity().HasIndex(b => b.Available); modelBuilder.Entity().HasIndex(b => b.ShortName).IsUnique(); modelBuilder.Entity().HasOne(b => b.BaseDifficulty); } public IDbContextTransaction BeginTransaction() { // return Database.BeginTransaction(); return null; } public int SaveChanges(IDbContextTransaction transaction = null) { var ret = base.SaveChanges(); if (ret > 0) transaction?.Commit(); return ret; } private class OsuDbLoggerFactory : ILoggerFactory { #region Disposal public void Dispose() { } #endregion public ILogger CreateLogger(string categoryName) => new OsuDbLogger(); public void AddProvider(ILoggerProvider provider) { // no-op. called by tooling. } private class OsuDbLoggerProvider : ILoggerProvider { #region Disposal public void Dispose() { } #endregion public ILogger CreateLogger(string categoryName) => new OsuDbLogger(); } private class OsuDbLogger : ILogger { public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { 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); } public bool IsEnabled(LogLevel logLevel) { #if DEBUG_DATABASE return logLevel > LogLevel.Debug; #else return logLevel > LogLevel.Information; #endif } public IDisposable BeginScope(TState state) => null; } } public void Migrate() { try { Database.Migrate(); } catch (Exception e) { throw new MigrationFailedException(e); } } } public class MigrationFailedException : Exception { public MigrationFailedException(Exception exception) : base("sqlite-net migration failed", exception) { } } }