using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace GameList.Data.Migrations { /// public partial class HardenOwnerAndSuggestionInvariants : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateIndex( name: "IX_Players_IsOwner", table: "Players", column: "IsOwner", unique: true, filter: "IsOwner = 1"); migrationBuilder.Sql( """ CREATE TRIGGER IF NOT EXISTS TR_Suggestions_MaxFivePerPlayer BEFORE INSERT ON Suggestions WHEN (SELECT COUNT(1) FROM Suggestions WHERE PlayerId = NEW.PlayerId) >= 5 AND ( COALESCE((SELECT HasJoker FROM Players WHERE Id = NEW.PlayerId), 0) = 0 OR COALESCE((SELECT CurrentPhase FROM Players WHERE Id = NEW.PlayerId), 0) != 2 ) BEGIN SELECT RAISE(ABORT, 'suggestion_limit_exceeded'); END; """ ); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_Suggestions_MaxFivePerPlayer;"); migrationBuilder.DropIndex( name: "IX_Players_IsOwner", table: "Players"); } } }