Skip to content

Commit

Permalink
remove log context and merge with default
Browse files Browse the repository at this point in the history
  • Loading branch information
trembon committed Dec 16, 2024
1 parent 18ee54e commit 87a6bff
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 240 deletions.
4 changes: 2 additions & 2 deletions HomeAutomation.Core/Logging/DatabaseLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

try
{
using LogContext db = serviceScope.ServiceProvider.GetRequiredService<LogContext>();
using DefaultContext db = serviceScope.ServiceProvider.GetRequiredService<DefaultContext>();
using IDbContextTransaction transaction = db.Database.BeginTransaction();

LogRow row = new LogRow
Expand Down Expand Up @@ -65,4 +65,4 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}
}
}
}
}
5 changes: 2 additions & 3 deletions HomeAutomation.Core/ScheduledJobs/CleanupLogScheduleJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace HomeAutomation.ScheduledJobs;

public class CleanupLogScheduleJob(LogContext logContext, ILogger<CleanupLogScheduleJob> logger) : IScheduledJob
public class CleanupLogScheduleJob(DefaultContext logContext, ILogger<CleanupLogScheduleJob> logger) : IScheduledJob
{
public async Task Execute(DateTime currentExecution, DateTime? lastExecution, CancellationToken cancellationToken)
{
Expand All @@ -14,10 +14,9 @@ public async Task Execute(DateTime currentExecution, DateTime? lastExecution, Ca
DateTime loglimit = DateTime.UtcNow.AddDays(-7); // TODO: place in configuration?
_ = await logContext.Rows.Where(x => loglimit > x.Timestamp).ExecuteDeleteAsync();


DateTime maillimit = DateTime.UtcNow.AddDays(-3); // TODO: place in configuration?
_ = await logContext.MailMessages.Where(x => maillimit > x.Timestamp).ExecuteDeleteAsync();

logger.LogInformation("Schedule.Cleanup :: done");
}
}
}
4 changes: 2 additions & 2 deletions HomeAutomation.Core/Services/EmailReceiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ private async Task SaveToEml(IServiceScope scope, string messageId, byte[] emlDa
EmlData = emlData
};

var context = scope.ServiceProvider.GetService<LogContext>();
var context = scope.ServiceProvider.GetService<DefaultContext>();

context.Add(mailMessage);
await context.SaveChangesAsync();
}
}
}
}
4 changes: 4 additions & 0 deletions HomeAutomation.Database/Contexts/DefaultContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class DefaultContext(DbContextOptions<DefaultContext> options) : DbContex

public DbSet<WeatherForecast> WeatherForecast { get; set; }

public DbSet<LogRow> Rows { get; set; }

public DbSet<MailMessage> MailMessages { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand Down
23 changes: 0 additions & 23 deletions HomeAutomation.Database/Contexts/LogContext.cs

This file was deleted.

3 changes: 1 addition & 2 deletions HomeAutomation.Database/Extensions/IHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static class IHostExtensions
public static void ApplyDatabaseMigrations(this IHost host)
{
host.ApplyDatabaseMigration<DefaultContext>();
host.ApplyDatabaseMigration<LogContext>();
}

private static void ApplyDatabaseMigration<TContext>(this IHost host) where TContext : DbContext
Expand All @@ -19,4 +18,4 @@ private static void ApplyDatabaseMigration<TContext>(this IHost host) where TCon
var context = scope.ServiceProvider.GetRequiredService<TContext>();
context.Database.Migrate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ public static void AddDefaultDatabaseContext(this IServiceCollection services, s
services.AddDatabaseContext<DefaultContext>(postgressConnectionString);
}

public static void AddLoggingDatabaseContext(this IServiceCollection services, string postgressConnectionString)
{
services.AddDatabaseContext<LogContext>(postgressConnectionString);
}

private static void AddDatabaseContext<TContext>(this IServiceCollection services, string postgressConnectionString) where TContext : DbContext
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name!;
services.AddDbContext<TContext>(options => options.UseNpgsql(postgressConnectionString, x => x.MigrationsAssembly(assemblyName)), ServiceLifetime.Transient);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

#nullable disable

namespace HomeAutomation.Database.Migrations.LogMigrations
namespace HomeAutomation.Database.Migrations.DefaultMigrations
{
/// <inheritdoc />
public partial class postgres_init : Migration
public partial class mergeWithLog : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "PhoneCalls");

migrationBuilder.CreateTable(
name: "MailMessages",
columns: table => new
Expand Down Expand Up @@ -56,6 +59,22 @@ protected override void Down(MigrationBuilder migrationBuilder)

migrationBuilder.DropTable(
name: "Rows");

migrationBuilder.CreateTable(
name: "PhoneCalls",
columns: table => new
{
ID = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Length = table.Column<TimeSpan>(type: "interval", nullable: false),
Number = table.Column<string>(type: "text", nullable: false),
Timestamp = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
Type = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PhoneCalls", x => x.ID);
});
}
}
}
Loading

0 comments on commit 87a6bff

Please sign in to comment.