From 090224b19f8bd5d696be1a8f717ead6a75f867f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Thu, 20 Nov 2025 20:34:04 +0100 Subject: [PATCH] DB Schema FIX --- src/Backend/.claude/settings.local.json | 13 + .../Data/AppDbContext.cs | 22 +- ...20193110_FixLayerDefaultValues.Designer.cs | 430 ++++++++++++++++++ .../20251120193110_FixLayerDefaultValues.cs | 47 ++ .../Migrations/AppDbContextModelSnapshot.cs | 4 +- src/Backend/RESTRequests/test.rest | 6 +- 6 files changed, 517 insertions(+), 5 deletions(-) create mode 100644 src/Backend/.claude/settings.local.json create mode 100644 src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.Designer.cs create mode 100644 src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.cs diff --git a/src/Backend/.claude/settings.local.json b/src/Backend/.claude/settings.local.json new file mode 100644 index 0000000..90cea92 --- /dev/null +++ b/src/Backend/.claude/settings.local.json @@ -0,0 +1,13 @@ +{ + "permissions": { + "allow": [ + "Bash(dotnet --version:*)", + "Bash(dotnet restore:*)", + "Bash(dotnet build:*)", + "Bash(dotnet ef:*)", + "Bash(dotnet tool install:*)" + ], + "deny": [], + "ask": [] + } +} diff --git a/src/Backend/DiunaBI.Infrastructure/Data/AppDbContext.cs b/src/Backend/DiunaBI.Infrastructure/Data/AppDbContext.cs index 28d6528..fa2ad99 100644 --- a/src/Backend/DiunaBI.Infrastructure/Data/AppDbContext.cs +++ b/src/Backend/DiunaBI.Infrastructure/Data/AppDbContext.cs @@ -25,7 +25,7 @@ public class AppDbContext(DbContextOptions options) : DbContext(op modelBuilder.Entity().Property(x => x.CreatedAt).IsRequired(); modelBuilder.Entity().Property(x => x.ModifiedAt).IsRequired(); modelBuilder.Entity().Property(x => x.IsDeleted).IsRequired().HasDefaultValue(false); - modelBuilder.Entity().Property(x => x.IsCancelled).IsRequired(); + modelBuilder.Entity().Property(x => x.IsCancelled).IsRequired().HasDefaultValue(false); modelBuilder.Entity().Property(x => x.CreatedById).IsRequired(); modelBuilder.Entity().Property(x => x.ModifiedById).IsRequired(); @@ -159,7 +159,7 @@ public class AppDbContext(DbContextOptions options) : DbContext(op { var entities = ChangeTracker.Entries() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified); - + foreach (var entity in entities) { // Try to set CreatedAt for new entities @@ -170,8 +170,24 @@ public class AppDbContext(DbContextOptions options) : DbContext(op { createdAtProperty.CurrentValue = DateTime.UtcNow; } + + // Ensure IsDeleted and IsCancelled have default values for Layer entities + if (entity.Entity is Layer) + { + var isDeletedProperty = entity.Properties.FirstOrDefault(p => p.Metadata.Name == "IsDeleted"); + if (isDeletedProperty != null && isDeletedProperty.CurrentValue == null) + { + isDeletedProperty.CurrentValue = false; + } + + var isCancelledProperty = entity.Properties.FirstOrDefault(p => p.Metadata.Name == "IsCancelled"); + if (isCancelledProperty != null && isCancelledProperty.CurrentValue == null) + { + isCancelledProperty.CurrentValue = false; + } + } } - + // Always update ModifiedAt var modifiedAtProperty = entity.Properties.FirstOrDefault(p => p.Metadata.Name == "ModifiedAt"); if (modifiedAtProperty != null) diff --git a/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.Designer.cs b/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.Designer.cs new file mode 100644 index 0000000..5b4dee9 --- /dev/null +++ b/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.Designer.cs @@ -0,0 +1,430 @@ +// +using System; +using DiunaBI.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DiunaBI.Infrastructure.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20251120193110_FixLayerDefaultValues")] + partial class FixLayerDefaultValues + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DiunaBI.Domain.Entities.DataInbox", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("Data") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.ToTable("DataInbox"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.Layer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("CreatedById") + .HasColumnType("uniqueidentifier"); + + b.Property("IsCancelled") + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); + + b.Property("IsDeleted") + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); + + b.Property("ModifiedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("ModifiedById") + .HasColumnType("uniqueidentifier"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Number") + .HasColumnType("int"); + + b.Property("ParentId") + .HasColumnType("uniqueidentifier"); + + b.Property("Type") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CreatedById"); + + b.HasIndex("ModifiedById"); + + b.ToTable("Layers"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.ProcessSource", b => + { + b.Property("LayerId") + .HasColumnType("uniqueidentifier"); + + b.Property("SourceId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("LayerId", "SourceId"); + + b.HasIndex("SourceId"); + + b.ToTable("ProcessSources"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.QueueJob", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CompletedAt") + .HasColumnType("datetime2"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("CreatedAtUtc") + .HasColumnType("datetime2"); + + b.Property("CreatedById") + .HasColumnType("uniqueidentifier"); + + b.Property("JobType") + .HasColumnType("int"); + + b.Property("LastAttemptAt") + .HasColumnType("datetime2"); + + b.Property("LastError") + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.Property("LayerId") + .HasColumnType("uniqueidentifier"); + + b.Property("LayerName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("MaxRetries") + .HasColumnType("int"); + + b.Property("ModifiedAtUtc") + .HasColumnType("datetime2"); + + b.Property("ModifiedById") + .HasColumnType("uniqueidentifier"); + + b.Property("PluginName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("Priority") + .HasColumnType("int"); + + b.Property("RetryCount") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("QueueJobs"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.Record", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Code") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("CreatedById") + .HasColumnType("uniqueidentifier"); + + b.Property("Desc1") + .HasMaxLength(10000) + .HasColumnType("nvarchar(max)"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("LayerId") + .HasColumnType("uniqueidentifier"); + + b.Property("ModifiedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("ModifiedById") + .HasColumnType("uniqueidentifier"); + + b.Property("Value1") + .HasColumnType("float"); + + b.Property("Value10") + .HasColumnType("float"); + + b.Property("Value11") + .HasColumnType("float"); + + b.Property("Value12") + .HasColumnType("float"); + + b.Property("Value13") + .HasColumnType("float"); + + b.Property("Value14") + .HasColumnType("float"); + + b.Property("Value15") + .HasColumnType("float"); + + b.Property("Value16") + .HasColumnType("float"); + + b.Property("Value17") + .HasColumnType("float"); + + b.Property("Value18") + .HasColumnType("float"); + + b.Property("Value19") + .HasColumnType("float"); + + b.Property("Value2") + .HasColumnType("float"); + + b.Property("Value20") + .HasColumnType("float"); + + b.Property("Value21") + .HasColumnType("float"); + + b.Property("Value22") + .HasColumnType("float"); + + b.Property("Value23") + .HasColumnType("float"); + + b.Property("Value24") + .HasColumnType("float"); + + b.Property("Value25") + .HasColumnType("float"); + + b.Property("Value26") + .HasColumnType("float"); + + b.Property("Value27") + .HasColumnType("float"); + + b.Property("Value28") + .HasColumnType("float"); + + b.Property("Value29") + .HasColumnType("float"); + + b.Property("Value3") + .HasColumnType("float"); + + b.Property("Value30") + .HasColumnType("float"); + + b.Property("Value31") + .HasColumnType("float"); + + b.Property("Value32") + .HasColumnType("float"); + + b.Property("Value4") + .HasColumnType("float"); + + b.Property("Value5") + .HasColumnType("float"); + + b.Property("Value6") + .HasColumnType("float"); + + b.Property("Value7") + .HasColumnType("float"); + + b.Property("Value8") + .HasColumnType("float"); + + b.Property("Value9") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CreatedById"); + + b.HasIndex("LayerId"); + + b.HasIndex("ModifiedById"); + + b.ToTable("Records"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETUTCDATE()"); + + b.Property("Email") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("UserName") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.Layer", b => + { + b.HasOne("DiunaBI.Domain.Entities.User", "CreatedBy") + .WithMany() + .HasForeignKey("CreatedById") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("DiunaBI.Domain.Entities.User", "ModifiedBy") + .WithMany() + .HasForeignKey("ModifiedById") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("CreatedBy"); + + b.Navigation("ModifiedBy"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.ProcessSource", b => + { + b.HasOne("DiunaBI.Domain.Entities.Layer", null) + .WithMany() + .HasForeignKey("LayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DiunaBI.Domain.Entities.Layer", "Source") + .WithMany() + .HasForeignKey("SourceId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Source"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.Record", b => + { + b.HasOne("DiunaBI.Domain.Entities.User", "CreatedBy") + .WithMany() + .HasForeignKey("CreatedById") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("DiunaBI.Domain.Entities.Layer", null) + .WithMany("Records") + .HasForeignKey("LayerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DiunaBI.Domain.Entities.User", "ModifiedBy") + .WithMany() + .HasForeignKey("ModifiedById") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("CreatedBy"); + + b.Navigation("ModifiedBy"); + }); + + modelBuilder.Entity("DiunaBI.Domain.Entities.Layer", b => + { + b.Navigation("Records"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.cs b/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.cs new file mode 100644 index 0000000..ccb3615 --- /dev/null +++ b/src/Backend/DiunaBI.Infrastructure/Migrations/20251120193110_FixLayerDefaultValues.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DiunaBI.Infrastructure.Migrations +{ + /// + public partial class FixLayerDefaultValues : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + // Ensure IsDeleted has default constraint (in case previous migration didn't apply it correctly) + migrationBuilder.Sql(@" + IF NOT EXISTS (SELECT 1 FROM sys.default_constraints + WHERE parent_object_id = OBJECT_ID('Layers') + AND COL_NAME(parent_object_id, parent_column_id) = 'IsDeleted') + BEGIN + ALTER TABLE [Layers] ADD CONSTRAINT [DF_Layers_IsDeleted] DEFAULT 0 FOR [IsDeleted]; + END + "); + + // Add default constraint for IsCancelled + migrationBuilder.AlterColumn( + name: "IsCancelled", + table: "Layers", + type: "bit", + nullable: false, + defaultValue: false, + oldClrType: typeof(bool), + oldType: "bit"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "IsCancelled", + table: "Layers", + type: "bit", + nullable: false, + oldClrType: typeof(bool), + oldType: "bit", + oldDefaultValue: false); + } + } +} diff --git a/src/Backend/DiunaBI.Infrastructure/Migrations/AppDbContextModelSnapshot.cs b/src/Backend/DiunaBI.Infrastructure/Migrations/AppDbContextModelSnapshot.cs index add6e42..08c9ecc 100644 --- a/src/Backend/DiunaBI.Infrastructure/Migrations/AppDbContextModelSnapshot.cs +++ b/src/Backend/DiunaBI.Infrastructure/Migrations/AppDbContextModelSnapshot.cs @@ -67,7 +67,9 @@ namespace DiunaBI.Infrastructure.Migrations .HasColumnType("uniqueidentifier"); b.Property("IsCancelled") - .HasColumnType("bit"); + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); b.Property("IsDeleted") .ValueGeneratedOnAdd() diff --git a/src/Backend/RESTRequests/test.rest b/src/Backend/RESTRequests/test.rest index b3f3c54..78a46ae 100644 --- a/src/Backend/RESTRequests/test.rest +++ b/src/Backend/RESTRequests/test.rest @@ -2,4 +2,8 @@ GET http://localhost:5400/tests/ping ### -GET https://morska.diunabi.com/api/layers/autoImport/godojshf8704i357yhgdsahjdfh/K5- \ No newline at end of file +GET https://morska.diunabi.com/api/layers/autoImport/godojshf8704i357yhgdsahjdfh/K5- + +### + +GET http://localhost:5400/layers/autoImport/10763478CB738D4ecb2h76g803478CB738D4e/K5-