Compare commits

..

2 Commits

Author SHA1 Message Date
69bc76cab4 Add idempotent migration script
All checks were successful
continuous-integration/drone/push Build is passing
2025-06-20 22:02:56 +02:00
4b1f9e78df Fix paths in create migrations script 2025-06-20 22:02:25 +02:00
2 changed files with 72 additions and 2 deletions

View File

@@ -1,2 +1,2 @@
dotnet ef migrations add $args[0] --project .\src\WebApi\WebApi.csproj --output-dir Persistence/Migrations
dotnet ef migrations script --idempotent --project .\src\WebApi\WebApi.csproj --output migrations/migration.sql
dotnet ef migrations add $args[0] --project .\src\Vegasco.Server.Api\Vegasco.Server.Api.csproj --output-dir Persistence/Migrations
dotnet ef migrations script --idempotent --project .\src\Vegasco.Server.Api\Vegasco.Server.Api.csproj --output ./src/Vegasco.Server.Api/migrations/migration.sql

View File

@@ -0,0 +1,70 @@
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', '9.0.5');
END IF;
END $EF$;
COMMIT;