// // ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ // ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ // █__ --█ █__ --█ ◄█ - █ // █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ // ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ // │ Copyright (c) JP Dillingham. // │ // │ This program is free software: you can redistribute it and/or modify // │ it under the terms of the GNU Affero General Public License as published // │ by the Free Software Foundation, version 3. // │ // │ This program is distributed in the hope that it will be useful, // │ but WITHOUT ANY WARRANTY; without even the implied warranty of // │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // │ GNU Affero General Public License for more details. // │ // │ You should have received a copy of the GNU Affero General Public License // │ along with this program. If not, see https://www.gnu.org/licenses/. // │ // │ This program is distributed with Additional Terms pursuant to Section 7 // │ of the AGPLv3. See the LICENSE file in the root directory of this // │ project for the complete terms and conditions. // │ // │ https://slskd.org // │ // ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ // │ SPDX-FileCopyrightText: JP Dillingham // │ SPDX-License-Identifier: AGPL-3.0-only // ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ // namespace slskd.Transfers { using System; using System.Text.Json; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; public class TransfersDbContext : DbContext { public TransfersDbContext(DbContextOptions options) : base(options) { } public DbSet Batches { get; set; } public DbSet Transfers { get; set; } public override int SaveChanges(bool acceptAllChangesOnSuccess) { // this is absolutely NOT IDEAL and will accellerate the move away from EF // we have to do this to store both the integer and the string value in the database // well, we don't *HAVE* to, but storing the string allows external apps to understand transfer history // and not need to work out the flags. storing the integer allows us to use bitwise operations in queries, // which is the main driver for this. foreach (var entry in ChangeTracker.Entries()) { if (entry.State == EntityState.Added || entry.State == EntityState.Modified) { #pragma warning disable CS0618 // Type or member is obsolete entry.Entity.StateDescription = entry.Entity.State.ToString(); #pragma warning restore CS0618 // Type or member is obsolete } } return base.SaveChanges(acceptAllChangesOnSuccess); } protected override void OnModelCreating(ModelBuilder modelBuilder) { ConfigureTransfers(modelBuilder); ConfigureBatches(modelBuilder); } private void ConfigureTransfers(ModelBuilder modelBuilder) { modelBuilder .Entity() .Property(e => e.StartedAt) .HasConversion(v => v, v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : null); modelBuilder .Entity() .Property(e => e.EndedAt) .HasConversion(v => v, v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : null); modelBuilder .Entity() .Property(d => d.Direction) .HasConversion(new EnumToStringConverter()); modelBuilder .Entity() .HasIndex(t => t.Direction) .HasDatabaseName("IDX_Transfers_Direction"); modelBuilder .Entity() .HasIndex(t => t.State) .HasDatabaseName("IDX_Transfers_State"); modelBuilder .Entity() .HasIndex(t => t.Removed) .HasDatabaseName("IDX_Transfers_Removed"); modelBuilder .Entity() .Property(e => e.Attempts) .HasDefaultValue(0); // force EF to match the migration modelBuilder .Entity() .HasIndex(t => t.BatchId) .HasDatabaseName("IDX_Transfers_BatchId"); // covers the check for existing records when enqueueing uploads and downloads modelBuilder .Entity() .HasIndex(t => new { t.Username, t.Filename }) .HasDatabaseName("IDX_Transfers_UsernameFilename"); // covers the GetUserStatistics method that backs limit checks // check every so often with EXPLAIN to make sure it's being shown as a covering index modelBuilder .Entity() .HasIndex(e => new { e.Username, e.Direction, e.EndedAt, e.StartedAt, e.State, e.Size }) .HasDatabaseName("IDX_Transfers_UserUploadStatistics"); } private void ConfigureBatches(ModelBuilder modelBuilder) { modelBuilder .Entity() .Property(e => e.CreatedAt) .HasConversion(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); modelBuilder .Entity() .HasMany(b => b.Transfers) .WithOne() .HasForeignKey(t => t.BatchId) .IsRequired(false) .OnDelete(DeleteBehavior.Cascade); modelBuilder .Entity() .Property(b => b.Options) .HasConversion( convertToProviderExpression: v => JsonSerializer.Serialize(v, new JsonSerializerOptions().WithStandardOptions()), convertFromProviderExpression: v => JsonSerializer.Deserialize(v, new JsonSerializerOptions().WithStandardOptions())) .HasColumnType("TEXT"); modelBuilder .Entity() .HasIndex(b => b.SearchId) .HasDatabaseName("IDX_Batches_SearchId"); } } }