using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using NadekoBot.Core.Services.Database.Models; using NadekoBot.Extensions; using System; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Data.Sqlite; using System.IO; namespace NadekoBot.Core.Services.Database { public class NadekoContextFactory : IDesignTimeDbContextFactory { public NadekoContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder(); var builder = new SqliteConnectionStringBuilder("Data Source=data/NadekoBot.db"); 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 Quotes { get; set; } public DbSet Donators { get; set; } public DbSet GuildConfigs { get; set; } public DbSet Reminders { get; set; } public DbSet SelfAssignableRoles { get; set; } public DbSet BotConfig { get; set; } public DbSet Currency { get; set; } public DbSet MusicPlaylists { get; set; } public DbSet CustomReactions { get; set; } public DbSet CurrencyTransactions { get; set; } public DbSet PokeGame { get; set; } public DbSet WaifuUpdates { get; set; } public DbSet Warnings { get; set; } public DbSet UserXpStats { get; set; } public DbSet Clubs { get; set; } public DbSet LoadedPackages { get; set; } //logging public DbSet LogSettings { get; set; } public DbSet IgnoredLogChannels { get; set; } public DbSet IgnoredVoicePresenceCHannels { get; set; } //orphans xD public DbSet EightBallResponses { get; set; } public DbSet RaceAnimals { get; set; } public DbSet RewardedUsers { get; set; } public NadekoContext(DbContextOptions options) : base(options) { } public void EnsureSeedData() { if (!BotConfig.Any()) { var bc = new BotConfig(); bc.RaceAnimals.AddRange(new HashSet { new RaceAnimal { Icon = "🐼", Name = "Panda" }, new RaceAnimal { Icon = "🐻", Name = "Bear" }, new RaceAnimal { Icon = "🐧", Name = "Pengu" }, new RaceAnimal { Icon = "🐨", Name = "Koala" }, new RaceAnimal { Icon = "🐬", Name = "Dolphin" }, new RaceAnimal { Icon = "🐞", Name = "Ladybird" }, new RaceAnimal { Icon = "🦀", Name = "Crab" }, new RaceAnimal { Icon = "🦄", Name = "Unicorn" } }); bc.EightBallResponses.AddRange(new HashSet { new EightBallResponse() { Text = "Most definitely yes" }, new EightBallResponse() { Text = "For sure" }, new EightBallResponse() { Text = "Totally!" }, new EightBallResponse() { Text = "Of course!" }, new EightBallResponse() { Text = "As I see it, yes" }, new EightBallResponse() { Text = "My sources say yes" }, new EightBallResponse() { Text = "Yes" }, new EightBallResponse() { Text = "Most likely" }, new EightBallResponse() { Text = "Perhaps" }, new EightBallResponse() { Text = "Maybe" }, new EightBallResponse() { Text = "Not sure" }, new EightBallResponse() { Text = "It is uncertain" }, new EightBallResponse() { Text = "Ask me again later" }, new EightBallResponse() { Text = "Don't count on it" }, new EightBallResponse() { Text = "Probably not" }, new EightBallResponse() { Text = "Very doubtful" }, new EightBallResponse() { Text = "Most likely no" }, new EightBallResponse() { Text = "Nope" }, new EightBallResponse() { Text = "No" }, new EightBallResponse() { Text = "My sources say no" }, new EightBallResponse() { Text = "Dont even think about it" }, new EightBallResponse() { Text = "Definitely no" }, new EightBallResponse() { Text = "NO - It may cause disease contraction" } }); BotConfig.Add(bc); this.SaveChanges(); } } protected override void OnModelCreating(ModelBuilder modelBuilder) { #region QUOTES //var quoteEntity = modelBuilder.Entity(); #endregion #region Donators var donatorEntity = modelBuilder.Entity(); donatorEntity .HasIndex(d => d.UserId) .IsUnique(); #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() .HasAlternateKey(x => new { x.GuildConfigId, x.Url }); //modelBuilder.Entity() // .HasAlternateKey(c => new { c.ChannelId, c.ProtectionType }); #endregion #region streamrole modelBuilder.Entity() .HasOne(x => x.GuildConfig) .WithOne(x => x.StreamRole); #endregion #region BotConfig var botConfigEntity = modelBuilder.Entity(); botConfigEntity.Property(x => x.XpMinutesTimeout) .HasDefaultValue(5); botConfigEntity.Property(x => x.XpPerMessage) .HasDefaultValue(3); //botConfigEntity // .HasMany(c => c.ModulePrefixes) // .WithOne(mp => mp.BotConfig) // .HasForeignKey(mp => mp.BotConfigId); #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 Currency var currencyEntity = modelBuilder.Entity(); currencyEntity .HasIndex(c => c.UserId) .IsUnique(); #endregion #region Permission var permissionEntity = modelBuilder.Entity(); permissionEntity .HasOne(p => p.Next) .WithOne(p => p.Previous) .IsRequired(false); #endregion #region LogSettings //var logSettingEntity = modelBuilder.Entity(); //logSettingEntity // .HasMany(ls => ls.IgnoredChannels) // .WithOne(ls => ls.LogSetting) // .HasPrincipalKey(ls => ls.id; //logSettingEntity // .HasMany(ls => ls.IgnoredVoicePresenceChannelIds) // .WithOne(ls => ls.LogSetting); #endregion #region MusicPlaylists var musicPlaylistEntity = modelBuilder.Entity(); musicPlaylistEntity .HasMany(p => p.Songs) .WithOne() .OnDelete(DeleteBehavior.Cascade); #endregion #region PokeGame var pokeGameEntity = modelBuilder.Entity(); pokeGameEntity .HasIndex(pt => pt.UserId) .IsUnique(); #endregion #region CommandPrice //well, i failed modelBuilder.Entity() .HasIndex(cp => cp.Price) .IsUnique(); //modelBuilder.Entity() // .HasIndex(cp => cp.CommandName) // .IsUnique(); #endregion #region Waifus var wi = modelBuilder.Entity(); wi.HasOne(x => x.Waifu) .WithOne(); // //.HasForeignKey(w => w.WaifuId) // //.IsRequired(true); //wi.HasOne(x => x.Claimer) // .WithOne(); // //.HasForeignKey(w => w.ClaimerId) // //.IsRequired(false); #endregion #region DiscordUser var du = modelBuilder.Entity(); du.HasAlternateKey(w => w.UserId); du.HasOne(x => x.Club) .WithMany(x => x.Users) .IsRequired(false); modelBuilder.Entity() .Property(x => x.LastLevelUp) .HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local)); #endregion #region Warnings var warn = modelBuilder.Entity(); #endregion #region PatreonRewards var pr = modelBuilder.Entity(); pr.HasIndex(x => x.UserId) .IsUnique(); #endregion #region XpStats modelBuilder.Entity() .HasIndex(x => new { x.UserId, x.GuildId }) .IsUnique(); modelBuilder.Entity() .Property(x => x.LastLevelUp) .HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 307, DateTimeKind.Local)); #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 } } }