Add Versionning for bundle backtest request
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,38 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Managing.Infrastructure.Databases.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddVersionToBundleBacktestRequests : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "Version",
|
||||
table: "BundleBacktestRequests",
|
||||
type: "integer",
|
||||
nullable: false,
|
||||
defaultValue: 1);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_BundleBacktestRequests_UserId_Name_Version",
|
||||
table: "BundleBacktestRequests",
|
||||
columns: new[] { "UserId", "Name", "Version" });
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_BundleBacktestRequests_UserId_Name_Version",
|
||||
table: "BundleBacktestRequests");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "Version",
|
||||
table: "BundleBacktestRequests");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -448,6 +448,11 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
b.Property<int?>("UserId")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("Version")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasDefaultValue(1);
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RequestId")
|
||||
@@ -459,6 +464,8 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
|
||||
b.HasIndex("UserId", "CreatedAt");
|
||||
|
||||
b.HasIndex("UserId", "Name", "Version");
|
||||
|
||||
b.ToTable("BundleBacktestRequests");
|
||||
});
|
||||
|
||||
|
||||
@@ -68,6 +68,9 @@ public class BundleBacktestRequestEntity
|
||||
[MaxLength(255)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int Version { get; set; } = 1;
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "jsonb")]
|
||||
public string ResultsJson { get; set; } = "[]";
|
||||
|
||||
@@ -199,6 +199,7 @@ public class ManagingDbContext : DbContext
|
||||
entity.Property(e => e.RequestId).IsRequired().HasMaxLength(255);
|
||||
entity.Property(e => e.UserId);
|
||||
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
|
||||
entity.Property(e => e.Version).IsRequired().HasDefaultValue(1);
|
||||
entity.Property(e => e.Status)
|
||||
.IsRequired()
|
||||
.HasConversion<string>(); // Store enum as string
|
||||
@@ -224,6 +225,9 @@ public class ManagingDbContext : DbContext
|
||||
|
||||
// Composite index for user queries ordered by creation date
|
||||
entity.HasIndex(e => new { e.UserId, e.CreatedAt });
|
||||
|
||||
// Composite index for user queries by name and version
|
||||
entity.HasIndex(e => new { e.UserId, e.Name, e.Version });
|
||||
});
|
||||
|
||||
// Configure Scenario entity
|
||||
|
||||
@@ -709,11 +709,22 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
|
||||
public void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
bundleRequest.User = user;
|
||||
var entity = PostgreSqlMappers.Map(bundleRequest);
|
||||
|
||||
// Set the UserId by finding the user entity
|
||||
var userEntity = _context.Users.FirstOrDefault(u => u.Name == user.Name);
|
||||
if (userEntity != null)
|
||||
{
|
||||
// Check for existing bundle requests with the same name for this user
|
||||
var maxVersion = _context.BundleBacktestRequests
|
||||
.Where(b => b.UserId == userEntity.Id && b.Name == bundleRequest.Name)
|
||||
.Max(b => (int?)b.Version);
|
||||
|
||||
// Increment version if a bundle with the same name exists
|
||||
bundleRequest.Version = (maxVersion ?? 0) + 1;
|
||||
}
|
||||
|
||||
var entity = PostgreSqlMappers.Map(bundleRequest);
|
||||
if (userEntity != null)
|
||||
{
|
||||
entity.UserId = userEntity.Id;
|
||||
}
|
||||
@@ -725,11 +736,22 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
|
||||
public async Task InsertBundleBacktestRequestForUserAsync(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
bundleRequest.User = user;
|
||||
var entity = PostgreSqlMappers.Map(bundleRequest);
|
||||
|
||||
// Set the UserId by finding the user entity
|
||||
var userEntity = await _context.Users.FirstOrDefaultAsync(u => u.Name == user.Name);
|
||||
if (userEntity != null)
|
||||
{
|
||||
// Check for existing bundle requests with the same name for this user
|
||||
var maxVersion = await _context.BundleBacktestRequests
|
||||
.Where(b => b.UserId == userEntity.Id && b.Name == bundleRequest.Name)
|
||||
.MaxAsync(b => (int?)b.Version);
|
||||
|
||||
// Increment version if a bundle with the same name exists
|
||||
bundleRequest.Version = (maxVersion ?? 0) + 1;
|
||||
}
|
||||
|
||||
var entity = PostgreSqlMappers.Map(bundleRequest);
|
||||
if (userEntity != null)
|
||||
{
|
||||
entity.UserId = userEntity.Id;
|
||||
}
|
||||
@@ -801,6 +823,7 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
|
||||
entity.ProgressInfo = bundleRequest.ProgressInfo;
|
||||
entity.CurrentBacktest = bundleRequest.CurrentBacktest;
|
||||
entity.EstimatedTimeRemainingSeconds = bundleRequest.EstimatedTimeRemainingSeconds;
|
||||
entity.Version = bundleRequest.Version; // Preserve the version
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
// Serialize Results to JSON
|
||||
@@ -842,6 +865,7 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
|
||||
entity.ProgressInfo = bundleRequest.ProgressInfo;
|
||||
entity.CurrentBacktest = bundleRequest.CurrentBacktest;
|
||||
entity.EstimatedTimeRemainingSeconds = bundleRequest.EstimatedTimeRemainingSeconds;
|
||||
entity.Version = bundleRequest.Version; // Preserve the version
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
// Serialize Results to JSON
|
||||
|
||||
@@ -391,7 +391,8 @@ public static class PostgreSqlMappers
|
||||
ProgressInfo = entity.ProgressInfo,
|
||||
CurrentBacktest = entity.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = entity.EstimatedTimeRemainingSeconds,
|
||||
Name = entity.Name
|
||||
Name = entity.Name,
|
||||
Version = entity.Version
|
||||
};
|
||||
|
||||
// Deserialize Results from JSON
|
||||
@@ -434,6 +435,7 @@ public static class PostgreSqlMappers
|
||||
CurrentBacktest = bundleRequest.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = bundleRequest.EstimatedTimeRemainingSeconds,
|
||||
Name = bundleRequest.Name,
|
||||
Version = bundleRequest.Version,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user