using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using NadekoBot.Services.Database.Models; using NadekoBot.Services; using System; using System.IO; using Microsoft.Extensions.Logging; using NadekoBot.Db.Models; namespace NadekoBot.Services.Database { public class NadekoContextFactory : IDesignTimeDbContextFactory { public NadekoContext CreateDbContext(string[] args) { LogSetup.SetupLogger(-2); var optionsBuilder = new DbContextOptionsBuilder(); IBotCredentials creds = new BotCredentials(); var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString); builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource); optionsBuilder.UseSqlite(builder.ToString()); var ctx = new NadekoContext(optionsBuilder.Options); ctx.Database.SetCommandTimeout(60); return ctx; } } public class NadekoContext : DbContext { public DbSet GuildConfigs { get; set; } public DbSet Quotes { get; set; } public DbSet Reminders { get; set; } public DbSet SelfAssignableRoles { get; set; } public DbSet MusicPlaylists { get; set; } public DbSet CustomReactions { get; set; } public DbSet CurrencyTransactions { get; set; } public DbSet WaifuUpdates { get; set; } public DbSet Warnings { get; set; } public DbSet UserXpStats { get; set; } public DbSet Clubs { get; set; } //logging public DbSet LogSettings { get; set; } public DbSet IgnoredLogChannels { get; set; } public DbSet IgnoredVoicePresenceCHannels { get; set; } public DbSet RotatingStatus { get; set; } public DbSet Blacklist { get; set; } public DbSet AutoCommands { get; set; } public DbSet RewardedUsers { get; set; } public DbSet PlantedCurrency { get; set; } public DbSet BanTemplates { get; set; } public DbSet DiscordPermOverrides { get; set; } public DbSet DiscordUser { get; set; } public DbSet MusicPlayerSettings { get; set; } public DbSet Repeaters { get; set; } public DbSet Poll { get; set; } public DbSet WaifuInfo { get; set; } public NadekoContext(DbContextOptions options) : base(options) { } #if DEBUG public static readonly LoggerFactory _debugLoggerFactory = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseLoggerFactory(_debugLoggerFactory); } #endif protected override void OnModelCreating(ModelBuilder modelBuilder) { #region QUOTES var quoteEntity = modelBuilder.Entity(); quoteEntity.HasIndex(x => x.GuildId); quoteEntity.HasIndex(x => x.Keyword); #endregion #region GuildConfig var configEntity = modelBuilder.Entity(); configEntity .HasIndex(c => c.GuildId) .IsUnique(); modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithOne(x => x.AntiSpamSetting); modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithOne(x => x.AntiRaidSetting); modelBuilder.Entity() .HasOne(x => x.AntiAltSetting) .WithOne() .HasForeignKey(x => x.GuildConfigId) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasAlternateKey(x => new { x.GuildConfigId, x.Url }); modelBuilder.Entity() .HasIndex(x => x.MessageId) .IsUnique(); modelBuilder.Entity() .HasIndex(x => x.ChannelId); configEntity.HasIndex(x => x.WarnExpireHours) .IsUnique(false); #endregion #region streamrole modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithOne(x => x.StreamRole); #endregion #region Self Assignable Roles var selfassignableRolesEntity = modelBuilder.Entity(); selfassignableRolesEntity .HasIndex(s => new { s.GuildId, s.RoleId }) .IsUnique(); selfassignableRolesEntity .Property(x => x.Group) .HasDefaultValue(0); #endregion #region MusicPlaylists var musicPlaylistEntity = modelBuilder.Entity(); musicPlaylistEntity .HasMany(p => p.Songs) .WithOne() .OnDelete(DeleteBehavior.Cascade); #endregion #region Waifus var wi = modelBuilder.Entity(); wi.HasOne(x => x.Waifu) .WithOne(); wi.HasIndex(x => x.Price); wi.HasIndex(x => x.ClaimerId); // wi.HasMany(x => x.Items) // .WithOne() // .OnDelete(DeleteBehavior.Cascade); var wu = modelBuilder.Entity(); #endregion #region DiscordUser var du = modelBuilder.Entity(); du.HasAlternateKey(w => w.UserId); du.HasOne(x => x.Club) .WithMany(x => x.Users) .IsRequired(false); du.Property(x => x.LastLevelUp) .HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local)); du.HasIndex(x => x.TotalXp); du.HasIndex(x => x.CurrencyAmount); du.HasIndex(x => x.UserId); #endregion #region Warnings var warn = modelBuilder.Entity(); warn.HasIndex(x => x.GuildId); warn.HasIndex(x => x.UserId); warn.HasIndex(x => x.DateAdded); #endregion #region PatreonRewards var pr = modelBuilder.Entity(); pr.HasIndex(x => x.PatreonUserId) .IsUnique(); #endregion #region XpStats var xps = modelBuilder.Entity(); xps .HasIndex(x => new { x.UserId, x.GuildId }) .IsUnique(); xps .Property(x => x.LastLevelUp) .HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 307, DateTimeKind.Local)); xps.HasIndex(x => x.UserId); xps.HasIndex(x => x.GuildId); xps.HasIndex(x => x.Xp); xps.HasIndex(x => x.AwardedXp); #endregion #region XpSettings modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithOne(x => x.XpSettings); #endregion #region XpRoleReward modelBuilder.Entity() .HasIndex(x => new { x.XpSettingsId, x.Level }) .IsUnique(); #endregion #region Club var ci = modelBuilder.Entity(); ci.HasOne(x => x.Owner) .WithOne() .HasForeignKey(x => x.OwnerId); ci.HasAlternateKey(x => new { x.Name, x.Discrim }); #endregion #region ClubManytoMany modelBuilder.Entity() .HasKey(t => new { t.ClubId, t.UserId }); modelBuilder.Entity() .HasOne(pt => pt.User) .WithMany(); modelBuilder.Entity() .HasOne(pt => pt.Club) .WithMany(x => x.Applicants); modelBuilder.Entity() .HasKey(t => new { t.ClubId, t.UserId }); modelBuilder.Entity() .HasOne(pt => pt.User) .WithMany(); modelBuilder.Entity() .HasOne(pt => pt.Club) .WithMany(x => x.Bans); #endregion #region Polls modelBuilder.Entity() .HasIndex(x => x.GuildId) .IsUnique(); #endregion #region CurrencyTransactions modelBuilder.Entity() .HasIndex(x => x.UserId) .IsUnique(false); #endregion #region Reminders modelBuilder.Entity() .HasIndex(x => x.When); #endregion #region GroupName modelBuilder.Entity() .HasIndex(x => new { x.GuildConfigId, x.Number }) .IsUnique(); modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithMany(x => x.SelfAssignableRoleGroupNames) .IsRequired(); #endregion #region BanTemplate modelBuilder.Entity() .HasIndex(x => x.GuildId) .IsUnique(); #endregion #region Perm Override modelBuilder.Entity() .HasIndex(x => new {x.GuildId, x.Command}) .IsUnique(); #endregion #region Music modelBuilder.Entity() .HasIndex(x => x.GuildId) .IsUnique(); modelBuilder.Entity() .Property(x => x.Volume) .HasDefaultValue(100); #endregion } } }