Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Services/Customer/Customer.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<CustomerDbContext>();
db.Database.EnsureCreated();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Database setup method conflicts with migrations, making future schema updates fail

The database is created using a shortcut method (EnsureCreated() at src/Services/Customer/Customer.API/Program.cs:19) instead of applying the migrations that the PR also introduces, so the migration history table is never populated and any future migration will crash because the tables already exist.

Impact: After the first startup, all subsequent schema migrations will fail with "table already exists" errors, making the migration files in this PR useless.

EnsureCreated vs Migrate conflict mechanism

db.Database.EnsureCreated() creates the schema directly without recording anything in the __EFMigrationsHistory table. The PR also adds a full EF Core migration (src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.cs) which expects to be tracked in that history table.

When a developer later adds a second migration and runs db.Database.Migrate() or dotnet ef database update, EF Core will see no migration history and attempt to apply InitialCreate, which will fail because the AppCustomers table already exists.

The RECON.md at src/Services/Customer/RECON.md:115 states the T2 validation gate requires: "EF migration applies against PostgreSQL" — this cannot work with EnsureCreated().

The fix is to replace db.Database.EnsureCreated() with db.Database.Migrate() so the migration is applied and tracked properly.

Suggested change
db.Database.EnsureCreated();
db.Database.Migrate();
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point about EnsureCreated() vs Migrate() for long-term migration tracking. However, this follows the existing pattern used by the Notification service (Notification.API/Program.cs:26) and was explicitly requested in the task spec. Deferring to the task author on whether to switch to Migrate() in a follow-up.

}

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Entities = Customer.Domain.Entities;

namespace Customer.Infrastructure.Data;

Expand All @@ -8,9 +9,23 @@ public CustomerDbContext(DbContextOptions<CustomerDbContext> options) : base(opt
{
}

public DbSet<Entities.Customer> Customers => Set<Entities.Customer>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// TODO: Configure entity mappings migrated from monolith

// NOTE: Use `Entities.Customer` to disambiguate from the root `Customer` namespace
modelBuilder.Entity<Entities.Customer>(entity =>
{
entity.ToTable("AppCustomers");
entity.Property(c => c.Name).IsRequired().HasMaxLength(100);
entity.HasIndex(c => c.Name);
entity.Property(c => c.Email).HasMaxLength(100);
entity.Property(c => c.PhoneNumber).IsUnicode(false).HasMaxLength(30);
entity.Property(c => c.City).HasMaxLength(50);
entity.Property(c => c.CreatedBy).HasMaxLength(40);
entity.Property(c => c.UpdatedBy).HasMaxLength(40);
});
}
}

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
@@ -0,0 +1,50 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Customer.Infrastructure.Data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AppCustomers",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Email = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
PhoneNumber = table.Column<string>(type: "character varying(30)", unicode: false, maxLength: 30, nullable: true),
Address = table.Column<string>(type: "text", nullable: true),
City = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
Gender = table.Column<int>(type: "integer", nullable: false),
CreatedBy = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
UpdatedBy = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
CreatedDate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
UpdatedDate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AppCustomers", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_AppCustomers_Name",
table: "AppCustomers",
column: "Name");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "AppCustomers");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// <auto-generated />
using System;
using Customer.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Customer.Infrastructure.Data.Migrations
{
[DbContext(typeof(CustomerDbContext))]
partial class CustomerDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Customer.Domain.Entities.Customer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<string>("Address")
.HasColumnType("text");

b.Property<string>("City")
.HasMaxLength(50)
.HasColumnType("character varying(50)");

b.Property<string>("CreatedBy")
.HasMaxLength(40)
.HasColumnType("character varying(40)");

b.Property<DateTime>("CreatedDate")
.HasColumnType("timestamp with time zone");

b.Property<string>("Email")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");

b.Property<int>("Gender")
.HasColumnType("integer");

b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");

b.Property<string>("PhoneNumber")
.HasMaxLength(30)
.IsUnicode(false)
.HasColumnType("character varying(30)");

b.Property<string>("UpdatedBy")
.HasMaxLength(40)
.HasColumnType("character varying(40)");

b.Property<DateTime>("UpdatedDate")
.HasColumnType("timestamp with time zone");

b.HasKey("Id");

b.HasIndex("Name");

b.ToTable("AppCustomers", (string)null);
});
#pragma warning restore 612, 618
}
}
}