feat(api): Postgres provider uses EnsureCreated (idempotent schema bootstrap, no migration history)

This commit is contained in:
2026-10-09 23:34:30 +03:30
parent 6bbc831ae0
commit 446423d1a6

View File

@@ -1,10 +1,13 @@
using FluentValidation;
using GreenHome.AI.DeepSeek;
using Microsoft.AspNetCore.Mvc.Versioning;
using GreenHome.Application;
using GreenHome.Infrastructure;
using GreenHome.Infrastructure.Data;
using GreenHome.Sms.Ippanel;
using GreenHome.VoiceCall.Avanak;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
@@ -42,7 +45,17 @@ app.Run();
void ConfigureCoreServices(WebApplicationBuilder builder)
{
builder.Services.AddControllers();
// API versioning: existing controllers stay unversioned (default v1.0, same URLs).
// New v2 controllers use [ApiVersion("2.0")] + [Route("api/v2/[controller]")] (or [Route("api/v2")] prefix).
builder.Services
.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
})
.AddMvc();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
@@ -110,8 +123,20 @@ void ApplyMigrations(WebApplication app)
try
{
var context = services.GetRequiredService<GreenHomeDbContext>();
var provider = app.Configuration["Database:Provider"] ?? "SqlServer";
if (provider == "Postgres")
{
// Soft-migration path: Postgres starts empty. EnsureCreated() is
// idempotent — it builds the full schema from the model only when no
// tables exist yet, and is a no-op once schema + data are in place.
context.Database.EnsureCreated();
}
else
{
context.Database.Migrate();
}
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();