From 9353a55cff9cc56050b2a6cf9d84a5fac3d1ac95 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Fri, 13 Jun 2025 18:12:25 +0300 Subject: [PATCH] added config db --- code/Data/DataContext.cs | 31 +----------- code/Data/EventsContext.cs | 49 +++++++++++++++++++ ... 20250528224750_InitialEvents.Designer.cs} | 6 +-- ...ial.cs => 20250528224750_InitialEvents.cs} | 2 +- ...pshot.cs => EventsContextModelSnapshot.cs} | 2 +- .../Controllers/EventsController.cs | 6 +-- .../DependencyInjection/LoggingDI.cs | 4 +- .../DependencyInjection/ServicesDI.cs | 2 +- code/Executable/HostExtensions.cs | 12 +++-- .../Events/EventCleanupService.cs | 2 +- code/Infrastructure/Events/EventPublisher.cs | 4 +- code/Infrastructure/Hubs/AppHub.cs | 4 +- 12 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 code/Data/EventsContext.cs rename code/Data/Migrations/{20250528224750_Initial.Designer.cs => 20250528224750_InitialEvents.Designer.cs} (93%) rename code/Data/Migrations/{20250528224750_Initial.cs => 20250528224750_InitialEvents.cs} (97%) rename code/Data/Migrations/{DataContextModelSnapshot.cs => EventsContextModelSnapshot.cs} (97%) diff --git a/code/Data/DataContext.cs b/code/Data/DataContext.cs index a53cce14..0761e2e8 100644 --- a/code/Data/DataContext.cs +++ b/code/Data/DataContext.cs @@ -1,18 +1,13 @@ -using System.Globalization; -using Common.Helpers; -using Data.Models.Events; +using Common.Helpers; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Data; /// -/// Database context for events +/// Database context for configuration data /// public class DataContext : DbContext { - public DbSet Events { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) @@ -24,27 +19,5 @@ public class DataContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity(entity => - { - entity.Property(e => e.Timestamp) - .HasConversion(new UtcDateTimeConverter()); - }); - } - - public class UtcDateTimeConverter : ValueConverter - { - public UtcDateTimeConverter() : base( - v => v, - v => DateTime.SpecifyKind(v, DateTimeKind.Utc) - ) {} - } - - public static string GetLikePattern(string input) - { - input = input.Replace("[", "[[]") - .Replace("%", "[%]") - .Replace("_", "[_]"); - - return $"%{input}%"; } } \ No newline at end of file diff --git a/code/Data/EventsContext.cs b/code/Data/EventsContext.cs new file mode 100644 index 00000000..932821bd --- /dev/null +++ b/code/Data/EventsContext.cs @@ -0,0 +1,49 @@ +using Common.Helpers; +using Data.Models.Events; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Data; + +/// +/// Database context for events +/// +public class EventsContext : DbContext +{ + public DbSet Events { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + var dbPath = Path.Combine(ConfigurationPathProvider.GetConfigPath(), "events.db"); + optionsBuilder.UseSqlite($"Data Source={dbPath}"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(entity => + { + entity.Property(e => e.Timestamp) + .HasConversion(new UtcDateTimeConverter()); + }); + } + + public class UtcDateTimeConverter : ValueConverter + { + public UtcDateTimeConverter() : base( + v => v, + v => DateTime.SpecifyKind(v, DateTimeKind.Utc) + ) {} + } + + public static string GetLikePattern(string input) + { + input = input.Replace("[", "[[]") + .Replace("%", "[%]") + .Replace("_", "[_]"); + + return $"%{input}%"; + } +} \ No newline at end of file diff --git a/code/Data/Migrations/20250528224750_Initial.Designer.cs b/code/Data/Migrations/20250528224750_InitialEvents.Designer.cs similarity index 93% rename from code/Data/Migrations/20250528224750_Initial.Designer.cs rename to code/Data/Migrations/20250528224750_InitialEvents.Designer.cs index 809f2239..1e7321f9 100644 --- a/code/Data/Migrations/20250528224750_Initial.Designer.cs +++ b/code/Data/Migrations/20250528224750_InitialEvents.Designer.cs @@ -10,9 +10,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Data.Migrations { - [DbContext(typeof(DataContext))] - [Migration("20250528224750_Initial")] - partial class Initial + [DbContext(typeof(EventsContext))] + [Migration("20250528224750_InitialEvents")] + partial class InitialEvents { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/code/Data/Migrations/20250528224750_Initial.cs b/code/Data/Migrations/20250528224750_InitialEvents.cs similarity index 97% rename from code/Data/Migrations/20250528224750_Initial.cs rename to code/Data/Migrations/20250528224750_InitialEvents.cs index 66ad01c5..0b293691 100644 --- a/code/Data/Migrations/20250528224750_Initial.cs +++ b/code/Data/Migrations/20250528224750_InitialEvents.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace Data.Migrations { /// - public partial class Initial : Migration + public partial class InitialEvents : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/code/Data/Migrations/DataContextModelSnapshot.cs b/code/Data/Migrations/EventsContextModelSnapshot.cs similarity index 97% rename from code/Data/Migrations/DataContextModelSnapshot.cs rename to code/Data/Migrations/EventsContextModelSnapshot.cs index 47a59dcf..e2c1861c 100644 --- a/code/Data/Migrations/DataContextModelSnapshot.cs +++ b/code/Data/Migrations/EventsContextModelSnapshot.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Data.Migrations { - [DbContext(typeof(DataContext))] + [DbContext(typeof(EventsContext))] partial class DataContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) diff --git a/code/Executable/Controllers/EventsController.cs b/code/Executable/Controllers/EventsController.cs index 85d2617d..02d7dc3e 100644 --- a/code/Executable/Controllers/EventsController.cs +++ b/code/Executable/Controllers/EventsController.cs @@ -12,9 +12,9 @@ namespace Executable.Controllers; [Route("api/[controller]")] public class EventsController : ControllerBase { - private readonly DataContext _context; + private readonly EventsContext _context; - public EventsController(DataContext context) + public EventsController(EventsContext context) { _context = context; } @@ -66,7 +66,7 @@ public class EventsController : ControllerBase // Apply search filter if provided if (!string.IsNullOrWhiteSpace(search)) { - string pattern = DataContext.GetLikePattern(search); + string pattern = EventsContext.GetLikePattern(search); query = query.Where(e => EF.Functions.Like(e.Message, pattern) || EF.Functions.Like(e.Data, pattern) || diff --git a/code/Executable/DependencyInjection/LoggingDI.cs b/code/Executable/DependencyInjection/LoggingDI.cs index 7de518af..db31446b 100644 --- a/code/Executable/DependencyInjection/LoggingDI.cs +++ b/code/Executable/DependencyInjection/LoggingDI.cs @@ -1,7 +1,5 @@ -using Common.Helpers; +using Common.Helpers; using Data.Enums; -using Infrastructure.Configuration; -using Infrastructure.Verticals.ContentBlocker; using Infrastructure.Verticals.DownloadCleaner; using Infrastructure.Verticals.QueueCleaner; using Serilog; diff --git a/code/Executable/DependencyInjection/ServicesDI.cs b/code/Executable/DependencyInjection/ServicesDI.cs index 3deb1039..9d7eba9a 100644 --- a/code/Executable/DependencyInjection/ServicesDI.cs +++ b/code/Executable/DependencyInjection/ServicesDI.cs @@ -25,7 +25,7 @@ public static class ServicesDI services .AddSingleton() .AddTransient() - .AddTransient() + .AddTransient() .AddTransient() .AddHostedService() // API services diff --git a/code/Executable/HostExtensions.cs b/code/Executable/HostExtensions.cs index e10305b1..dce93f87 100644 --- a/code/Executable/HostExtensions.cs +++ b/code/Executable/HostExtensions.cs @@ -34,10 +34,16 @@ public static class HostExtensions } // Apply db migrations - var dbContext = host.Services.GetRequiredService(); - if ((await dbContext.Database.GetPendingMigrationsAsync()).Any()) + var eventsContext = host.Services.GetRequiredService(); + if ((await eventsContext.Database.GetPendingMigrationsAsync()).Any()) { - await dbContext.Database.MigrateAsync(); + await eventsContext.Database.MigrateAsync(); + } + + var configContext = host.Services.GetRequiredService(); + if ((await configContext.Database.GetPendingMigrationsAsync()).Any()) + { + await configContext.Database.MigrateAsync(); } return host; diff --git a/code/Infrastructure/Events/EventCleanupService.cs b/code/Infrastructure/Events/EventCleanupService.cs index 0cd73052..5b82233b 100644 --- a/code/Infrastructure/Events/EventCleanupService.cs +++ b/code/Infrastructure/Events/EventCleanupService.cs @@ -59,7 +59,7 @@ public class EventCleanupService : BackgroundService try { using var scope = _serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); + var context = scope.ServiceProvider.GetRequiredService(); var cutoffDate = DateTime.UtcNow.AddDays(-_retentionDays); await context.Events diff --git a/code/Infrastructure/Events/EventPublisher.cs b/code/Infrastructure/Events/EventPublisher.cs index f58f228d..af8a3563 100644 --- a/code/Infrastructure/Events/EventPublisher.cs +++ b/code/Infrastructure/Events/EventPublisher.cs @@ -17,14 +17,14 @@ namespace Infrastructure.Events; /// public class EventPublisher { - private readonly DataContext _context; + private readonly EventsContext _context; private readonly IHubContext _appHubContext; private readonly ILogger _logger; private readonly INotificationPublisher _notificationPublisher; private readonly IDryRunInterceptor _dryRunInterceptor; public EventPublisher( - DataContext context, + EventsContext context, IHubContext appHubContext, ILogger logger, INotificationPublisher notificationPublisher, diff --git a/code/Infrastructure/Hubs/AppHub.cs b/code/Infrastructure/Hubs/AppHub.cs index 87c22224..c82e1f38 100644 --- a/code/Infrastructure/Hubs/AppHub.cs +++ b/code/Infrastructure/Hubs/AppHub.cs @@ -11,11 +11,11 @@ namespace Infrastructure.Hubs; /// public class AppHub : Hub { - private readonly DataContext _context; + private readonly EventsContext _context; private readonly ILogger _logger; private readonly SignalRLogSink _logSink; - public AppHub(DataContext context, ILogger logger, SignalRLogSink logSink) + public AppHub(EventsContext context, ILogger logger, SignalRLogSink logSink) { _context = context; _logger = logger;