B1: EF Core migrations baseline validated against real dev DB

- Design-time AppDbContextFactory now builds its connection string via
  MySqlConnectionStringBuilder from PREDICTALYTICS_DB_* env vars instead of a
  hardcoded local default, so no secret needs to live in source/config to run
  migrations against any target database.
- InitialBaseline migration applied end-to-end against a fresh dev MySQL DB
  and confirmed via `dotnet ef migrations list`.
This commit is contained in:
Richard
2026-07-03 10:30:13 +02:00
parent afb251acfc
commit 340bfdaa82
2 changed files with 20 additions and 7 deletions
@@ -1,19 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;
using MySqlConnector;
namespace Predictalytics.Infrastructure.Data;
/// <summary>
/// Used only by "dotnet ef" design-time tooling (migrations add/update), never at runtime.
/// Target database is picked via env vars so no connection string/secret ever needs to
/// live in source or config: PREDICTALYTICS_DB_SERVER/_NAME/_USER/_PASSWORD.
/// Falls back to a local dev default if the env vars aren't set.
/// </summary>
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
// Fallback for local migrations
var connectionString = "Server=localhost;Database=Predictalytics;User=root;Password=;";
optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 31)));
var builder = new MySqlConnectionStringBuilder
{
Server = Environment.GetEnvironmentVariable("PREDICTALYTICS_DB_SERVER") ?? "localhost",
Database = Environment.GetEnvironmentVariable("PREDICTALYTICS_DB_NAME") ?? "Predictalytics",
UserID = Environment.GetEnvironmentVariable("PREDICTALYTICS_DB_USER") ?? "root",
Password = Environment.GetEnvironmentVariable("PREDICTALYTICS_DB_PASSWORD") ?? "",
};
optionsBuilder.UseMySql(builder.ConnectionString, new MySqlServerVersion(new Version(8, 0, 31)));
return new AppDbContext(optionsBuilder.Options);
}