using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
namespace ManagerService.Data
{
///
/// Fabrique utilisée **uniquement par les outils EF** (`dotnet ef migrations add`).
///
/// ⚠️ Sans elle, EF construit tout l'hôte pour trouver le contexte — et l'hôte ouvre une
/// connexion PostgreSQL au démarrage (stockage Hangfire). Générer une migration exigeait
/// donc une base joignable, alors qu'écrire un fichier de migration n'a besoin d'aucune
/// base. Sur une machine sans Postgres ni Docker, c'était tout simplement impossible.
///
/// La chaîne de connexion n'est jamais utilisée pour se connecter à ce stade ; elle doit
/// seulement être syntaxiquement valide. MIGRATIONS_CONNECTION permet de la
/// surcharger pour un database update ponctuel.
///
public class MyInfoMateDbContextFactory : IDesignTimeDbContextFactory
{
public MyInfoMateDbContext CreateDbContext(string[] args)
{
var connectionString = Environment.GetEnvironmentVariable("MIGRATIONS_CONNECTION")
?? "Host=localhost;Port=5432;Database=my_info_mate;Username=postgres;Password=postgres";
var options = new DbContextOptionsBuilder()
.UseNpgsql(connectionString, o =>
{
o.UseNetTopologySuite();
o.UseVector();
})
.Options;
return new MyInfoMateDbContext(options, new HttpContextAccessor());
}
}
}