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/Identity/Identity.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<IdentityDbContext>();
db.Database.Migrate();
}
Comment on lines +16 to +20

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.

🚩 Automatic migration on startup runs unconditionally in all environments

The db.Database.Migrate() call at src/Services/Identity/Identity.API/Program.cs:19 runs in all environments (dev, staging, production), unlike other services (Order, Customer) which have no auto-migration. In scaled-out production deployments, multiple instances will attempt to migrate concurrently. PostgreSQL + EF Core handle this safely via advisory locks, so it won't corrupt data, but it adds startup latency and is a pattern divergence from other services. If this is intentional for the T2 persistence task, consider gating behind IsDevelopment() or an environment variable before merging to production branches.

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.

This is intentional per the T2 task specification — the auto-migration placement was explicitly required. PostgreSQL + EF Core advisory locks make concurrent startup safe. Gating behind an environment check can be considered in a follow-up if needed before production deployment.


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

namespace Identity.Infrastructure.Data;
Expand All @@ -8,9 +9,29 @@ public IdentityDbContext(DbContextOptions<IdentityDbContext> options) : base(opt
{
}

public DbSet<AppUser> Users { get; set; }
public DbSet<AppRole> Roles { get; set; }

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

modelBuilder.Entity<AppUser>(entity =>
{
entity.ToTable("Users");
entity.HasKey(e => e.Id);
entity.Property(e => e.UserName).IsRequired().HasMaxLength(100);
entity.HasIndex(e => e.UserName).IsUnique();
entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
entity.HasIndex(e => e.Email).IsUnique();
entity.Property(e => e.PasswordHash).IsRequired();
});

modelBuilder.Entity<AppRole>(entity =>
{
entity.ToTable("Roles");
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired();
});
}
}

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,69 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Identity.Infrastructure.Data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Roles",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
Description = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Roles", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Email = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
PasswordHash = table.Column<string>(type: "text", nullable: false),
FullName = table.Column<string>(type: "text", nullable: true),
JobTitle = table.Column<string>(type: "text", nullable: true),
IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_Users_Email",
table: "Users",
column: "Email",
unique: true);

migrationBuilder.CreateIndex(
name: "IX_Users_UserName",
table: "Users",
column: "UserName",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Roles");

migrationBuilder.DropTable(
name: "Users");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// <auto-generated />
using System;
using Identity.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Identity.Infrastructure.Data.Migrations
{
[DbContext(typeof(IdentityDbContext))]
partial class IdentityDbContextModelSnapshot : 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("Identity.Domain.Entities.AppRole", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

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

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

b.HasKey("Id");

b.ToTable("Roles", (string)null);
});

modelBuilder.Entity("Identity.Domain.Entities.AppUser", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

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

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

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

b.Property<bool>("IsEnabled")
.HasColumnType("boolean");

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

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

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

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

b.HasKey("Id");

b.HasIndex("Email")
.IsUnique();

b.HasIndex("UserName")
.IsUnique();

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