Harden owner and suggestion invariants for concurrent writes
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user