[backend/database] Fix votersCount getting set for all polls

This commit is contained in:
Laura Hausmann 2024-06-05 15:59:27 +02:00
parent 1e761f5008
commit 39a6ed2215
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 26 additions and 1 deletions

View file

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;
#nullable disable
namespace Iceshrimp.Backend.Core.Database.Migrations
{
/// <inheritdoc />
[DbContext(typeof(DatabaseContext))]
[Migration("20240605135805_FixupPollVotersCounts")]
public partial class FixupPollVotersCounts : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql($"""UPDATE "poll" SET "votersCount" = NULL;""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View file

@ -20,7 +20,7 @@ public class PollService(
{ {
if (!updateVotersCount) return; if (!updateVotersCount) return;
await db.Database 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; return;
} }