48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace GameList.Data.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class HardenOwnerAndSuggestionInvariants : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
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;
|
|
"""
|
|
);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_Suggestions_MaxFivePerPlayer;");
|
|
|
|
migrationBuilder.DropIndex(
|
|
name: "IX_Players_IsOwner",
|
|
table: "Players");
|
|
}
|
|
}
|
|
}
|