From ed38dc37887ba70d706e6e2826ff8753c0d85142 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 14 Jun 2024 18:59:36 +0200 Subject: [PATCH] [backend/database] Fix poll votersCount query for PostgreSQL versions prior to 16 --- .../20240614164941_FixupPollVotersCounts2.cs | 25 +++++++++++++++++++ .../DatabaseContextModelSnapshot.cs | 2 +- .../Core/Services/PollService.cs | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Iceshrimp.Backend/Core/Database/Migrations/20240614164941_FixupPollVotersCounts2.cs diff --git a/Iceshrimp.Backend/Core/Database/Migrations/20240614164941_FixupPollVotersCounts2.cs b/Iceshrimp.Backend/Core/Database/Migrations/20240614164941_FixupPollVotersCounts2.cs new file mode 100644 index 00000000..c3fe309f --- /dev/null +++ b/Iceshrimp.Backend/Core/Database/Migrations/20240614164941_FixupPollVotersCounts2.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("20240614164941_FixupPollVotersCounts2")] + public partial class FixupPollVotersCounts2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql("""UPDATE "poll" SET "votersCount" = (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = "poll"."noteId") AS sq) WHERE "userHost" IS NULL AND "votersCount" IS NOT NULL;"""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs b/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs index 1843cbfa..f2b38800 100644 --- a/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs +++ b/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs @@ -19,7 +19,7 @@ namespace Iceshrimp.Backend.Core.Database.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "8.0.5") + .HasAnnotation("ProductVersion", "8.0.6") .HasAnnotation("Relational:MaxIdentifierLength", 63); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "antenna_src_enum", new[] { "home", "all", "users", "list", "group", "instances" }); diff --git a/Iceshrimp.Backend/Core/Services/PollService.cs b/Iceshrimp.Backend/Core/Services/PollService.cs index 446da458..11e48361 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})) WHERE "noteId" = {poll.NoteId};"""); + .ExecuteSqlAsync($"""UPDATE "poll" SET "votersCount" = (SELECT COUNT(*) FROM (SELECT DISTINCT "userId" FROM "poll_vote" WHERE "noteId" = {poll.NoteId}) AS sq) WHERE "noteId" = {poll.NoteId};"""); return; }