From 39a6ed2215488100a2a77ddf3343a6ceb95493e3 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 5 Jun 2024 15:59:27 +0200 Subject: [PATCH] [backend/database] Fix votersCount getting set for all polls --- .../20240605135805_FixupPollVotersCounts.cs | 25 +++++++++++++++++++ .../Core/Services/PollService.cs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Iceshrimp.Backend/Core/Database/Migrations/20240605135805_FixupPollVotersCounts.cs diff --git a/Iceshrimp.Backend/Core/Database/Migrations/20240605135805_FixupPollVotersCounts.cs b/Iceshrimp.Backend/Core/Database/Migrations/20240605135805_FixupPollVotersCounts.cs new file mode 100644 index 00000000..f1cc67b5 --- /dev/null +++ b/Iceshrimp.Backend/Core/Database/Migrations/20240605135805_FixupPollVotersCounts.cs @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Infrastructure; + +#nullable disable + +namespace Iceshrimp.Backend.Core.Database.Migrations +{ + /// + [DbContext(typeof(DatabaseContext))] + [Migration("20240605135805_FixupPollVotersCounts")] + public partial class FixupPollVotersCounts : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql($"""UPDATE "poll" SET "votersCount" = NULL;"""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Iceshrimp.Backend/Core/Services/PollService.cs b/Iceshrimp.Backend/Core/Services/PollService.cs index a82d7fac..446da458 100644 --- a/Iceshrimp.Backend/Core/Services/PollService.cs +++ b/Iceshrimp.Backend/Core/Services/PollService.cs @@ -20,7 +20,7 @@ public class PollService( { if (!updateVotersCount) return; await db.Database - .ExecuteSqlAsync($"""UPDATE "poll" SET "votersCount" = (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = {poll.NoteId}));"""); + .ExecuteSqlAsync($"""UPDATE "poll" SET "votersCount" = (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = {poll.NoteId})) WHERE "noteId" = {poll.NoteId};"""); return; }