Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<!--
|
||||
System.Security.Cryptography.Xml тянется транзитивно из EF Core Design.
|
||||
EF Core Design — build-time only (PrivateAssets=all), в рантайм не попадает.
|
||||
Подавляем advisory до выхода пофикшенного standalone-пакета.
|
||||
-->
|
||||
<NuGetAuditSuppress>
|
||||
https://github.com/advisories/GHSA-23rf-6693-g89p;
|
||||
https://github.com/advisories/GHSA-37gx-xxp4-5rgx;
|
||||
https://github.com/advisories/GHSA-6588-8gv4-xfgh;
|
||||
https://github.com/advisories/GHSA-8q5v-6pqq-x66h;
|
||||
https://github.com/advisories/GHSA-cvvh-rhrc-wg4q;
|
||||
https://github.com/advisories/GHSA-g8r8-53c2-pm3f;
|
||||
https://github.com/advisories/GHSA-mmjf-rqrv-855v;
|
||||
https://github.com/advisories/GHSA-w3x6-4m5h-cxqf
|
||||
</NuGetAuditSuppress>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MemeVault.Infrastructure\MemeVault.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.12">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.12" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,48 @@
|
||||
using MemeVault.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(cfg => cfg.AddEnvironmentVariables())
|
||||
.ConfigureServices((ctx, services) =>
|
||||
{
|
||||
services.AddDbContext<AppDbContext>(opts =>
|
||||
opts.UseNpgsql(ctx.Configuration.GetConnectionString("Postgres")));
|
||||
})
|
||||
.Build();
|
||||
|
||||
var logger = host.Services.GetRequiredService<ILogger<Program>>();
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Starting database migration...");
|
||||
|
||||
using var scope = host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
var pending = await db.Database.GetPendingMigrationsAsync();
|
||||
var list = pending.ToList();
|
||||
|
||||
if (list.Count == 0)
|
||||
{
|
||||
logger.LogInformation("No pending migrations.");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("Applying {Count} migration(s): {Names}",
|
||||
list.Count, string.Join(", ", list));
|
||||
|
||||
await db.Database.MigrateAsync();
|
||||
logger.LogInformation("Migration completed successfully.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogCritical(ex, "Migration failed.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
Reference in New Issue
Block a user