Compare commits

...

3 Commits

Author SHA1 Message Date
de7e9a7131 Tweak log levels for non-dev environments
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-24 13:44:23 +02:00
6b422545d9 Remove migrations sql script 2024-08-24 13:43:58 +02:00
4a1f1a5a67 Apply migrations on startup 2024-08-24 13:43:43 +02:00
4 changed files with 27 additions and 73 deletions

View File

@@ -1,71 +0,0 @@
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
"MigrationId" character varying(150) NOT NULL,
"ProductVersion" character varying(32) NOT NULL,
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);
START TRANSACTION;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
CREATE TABLE "Users" (
"Id" text NOT NULL,
CONSTRAINT "PK_Users" PRIMARY KEY ("Id")
);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
CREATE TABLE "Cars" (
"Id" uuid NOT NULL,
"Name" character varying(50) NOT NULL,
"UserId" text NOT NULL,
CONSTRAINT "PK_Cars" PRIMARY KEY ("Id"),
CONSTRAINT "FK_Cars_Users_UserId" FOREIGN KEY ("UserId") REFERENCES "Users" ("Id") ON DELETE CASCADE
);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
CREATE TABLE "Consumptions" (
"Id" uuid NOT NULL,
"DateTime" timestamp with time zone NOT NULL,
"Distance" double precision NOT NULL,
"Amount" double precision NOT NULL,
"IgnoreInCalculation" boolean NOT NULL,
"CarId" uuid NOT NULL,
CONSTRAINT "PK_Consumptions" PRIMARY KEY ("Id"),
CONSTRAINT "FK_Consumptions_Cars_CarId" FOREIGN KEY ("CarId") REFERENCES "Cars" ("Id") ON DELETE CASCADE
);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
CREATE INDEX "IX_Cars_UserId" ON "Cars" ("UserId");
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
CREATE INDEX "IX_Consumptions_CarId" ON "Consumptions" ("CarId");
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240818105918_Initial') THEN
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20240818105918_Initial', '8.0.8');
END IF;
END $EF$;
COMMIT;

View File

@@ -45,6 +45,8 @@ public static class DependencyInjectionExtensions
services.AddHttpContextAccessor(); services.AddHttpContextAccessor();
services.AddHostedService<ApplyMigrationsService>();
return services; return services;
} }

View File

@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
namespace Vegasco.WebApi.Persistence;
public class ApplyMigrationsService : IHostedService
{
private readonly IServiceScopeFactory _scopeFactory;
public ApplyMigrationsService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using IServiceScope scope = _scopeFactory.CreateScope();
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

View File

@@ -1,8 +1,9 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Warning",
"Microsoft.AspNetCore": "Warning" "Vegasco": "Information",
"Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*"