[backend/database] Fix poll votersCount query for PostgreSQL versions prior to 16

This commit is contained in:
Laura Hausmann 2024-06-14 18:59:36 +02:00
parent c4190781e3
commit ed38dc3788
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 27 additions and 2 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("20240614164941_FixupPollVotersCounts2")]
public partial class FixupPollVotersCounts2 : Migration
{
/// <inheritdoc />
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;""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View file

@ -19,7 +19,7 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "8.0.5") .HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "antenna_src_enum", new[] { "home", "all", "users", "list", "group", "instances" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "antenna_src_enum", new[] { "home", "all", "users", "list", "group", "instances" });

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})) 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; return;
} }