[backend/database] Add migration that fixes up poll voter counts using the new algorithm (ISH-377)

This commit is contained in:
Laura Hausmann 2024-06-20 20:05:51 +02:00
parent 7f14c3df81
commit f5c0cc9a38
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;
#nullable disable
namespace Iceshrimp.Backend.Core.Database.Migrations
{
/// <inheritdoc />
[DbContext(typeof(DatabaseContext))]
[Migration("20240620180339_FixupPollVotersCounts3")]
public partial class FixupPollVotersCounts3 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""
UPDATE "poll" SET "votersCount" = GREATEST("votersCount", (SELECT MAX("total") FROM UNNEST("votes") AS "total")::integer, (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = "poll"."noteId") AS sq)::integer) WHERE "votersCount" IS NOT NULL AND "multiple" = TRUE;
UPDATE "poll" SET "votersCount" = GREATEST("votersCount", (SELECT SUM("total") FROM UNNEST("votes") AS "total")::integer, (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = "poll"."noteId") AS sq)::integer) WHERE "votersCount" IS NOT NULL AND "multiple" = FALSE;
""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}