Skip to content

Commit

Permalink
fix: Change ID to always
Browse files Browse the repository at this point in the history
  • Loading branch information
SenexCrenshaw committed Feb 9, 2024
1 parent 33420cf commit dc56aa9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
11 changes: 6 additions & 5 deletions StreamMaster.API/Services/PostStartup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using StreamMaster.Application.Services;
using StreamMaster.Infrastructure.EF.PGSQL;
using StreamMaster.SchedulesDirect.Domain.Interfaces;

namespace StreamMaster.API.Services;

Expand All @@ -16,6 +15,12 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)

logger.LogInformation($"Stream Master is starting.");

using IServiceScope scope = serviceProvider.CreateScope();
PGSQLRepositoryContext repositoryContext = scope.ServiceProvider.GetRequiredService<PGSQLRepositoryContext>();

//ISchedulesDirectDataService schedulesDirectService = scope.ServiceProvider.GetRequiredService<ISchedulesDirectDataService>();
await repositoryContext.MigrateData();

await taskQueue.EPGSync(cancellationToken).ConfigureAwait(false);

await taskQueue.ReadDirectoryLogos(cancellationToken).ConfigureAwait(false);
Expand All @@ -37,10 +42,6 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)

await taskQueue.SetIsSystemReady(true, cancellationToken).ConfigureAwait(false);

using IServiceScope scope = serviceProvider.CreateScope();
PGSQLRepositoryContext repositoryContext = scope.ServiceProvider.GetRequiredService<PGSQLRepositoryContext>();

ISchedulesDirectDataService schedulesDirectService = scope.ServiceProvider.GetRequiredService<ISchedulesDirectDataService>();
await repositoryContext.MigrateData(schedulesDirectService.AllServices);
}
}
2 changes: 1 addition & 1 deletion StreamMaster.Domain/Repository/IRepositoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ DbSet<ChannelGroup> ChannelGroups

void Dispose();
bool IsEntityTracked<TEntity>(TEntity entity) where TEntity : class;
Task MigrateData(List<MxfService> allServices);
Task MigrateData(List<MxfService>? allServices = null);
}
}

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 @@ -22,7 +22,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "citext");
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
NpgsqlModelBuilderExtensions.UseIdentityAlwaysColumns(modelBuilder);

modelBuilder.Entity("Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.DataProtectionKey", b =>
{
Expand Down
34 changes: 33 additions & 1 deletion StreamMaster.Infrastructure.EF.PGSQL/PGSQLRepositoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,47 @@ public bool IsEntityTracked<TEntity>(TEntity entity) where TEntity : class
return ChangeTracker.Entries<TEntity>().Any(e => e.Entity == entity);
}

public async Task MigrateData(List<MxfService> allServices)
public async Task MigrateData(List<MxfService>? allServices = null)
{
string? currentMigration = Database.GetAppliedMigrations().LastOrDefault();
if (currentMigration == null)
{
return;
}

if (!SystemKeyValues.Any(a => a.Key == "ChangeIDAlways"))
{
await FixIDs().ConfigureAwait(false);
SystemKeyValues.Add(new SystemKeyValue { Key = "ChangeIDAlways", Value = "1" });
await SaveChangesAsync().ConfigureAwait(false);
}
}

private async Task FixIDs()
{
int startValue = ChannelGroups.Max(a => a.Id) + 1;
await DoFixID("ChannelGroups", startValue).ConfigureAwait(false);
startValue = EPGFiles.Max(a => a.Id) + 1;
await DoFixID("EPGFiles", startValue).ConfigureAwait(false);
startValue = M3UFiles.Max(a => a.Id) + 1;
await DoFixID("M3UFiles", startValue).ConfigureAwait(false);
startValue = StreamGroups.Max(a => a.Id) + 1;
await DoFixID("StreamGroups", startValue).ConfigureAwait(false);
}

private async Task DoFixID(string tableName, int startValue)
{
ExecuteSqlRaw($"ALTER TABLE public.\"{tableName}\" ALTER COLUMN \"Id\" SET GENERATED ALWAYS;");

string sequenceName = $"{tableName}_Id_seq";
string alterSequenceCmd = $"ALTER SEQUENCE \"{sequenceName}\" RESTART WITH {startValue}";

ExecuteSqlRaw(alterSequenceCmd);

await SaveChangesAsync().ConfigureAwait(false);
}



public DbSet<SystemKeyValue> SystemKeyValues { get; set; }

Expand Down

0 comments on commit dc56aa9

Please sign in to comment.