[backend/database] Switch job identifier generation scheme to from UUIDv4 to ULID

This commit is contained in:
Laura Hausmann 2024-05-27 21:35:09 +02:00
parent 55250e59f0
commit e77c768882
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
6 changed files with 36 additions and 3 deletions

View file

@ -1181,6 +1181,7 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options)
modelBuilder.Entity<Job>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Status).HasDefaultValue(Job.JobStatus.Queued);
entity.Property(e => e.QueuedAt).HasDefaultValueSql("now()");
entity.HasOne<Worker>().WithMany().HasForeignKey(d => d.WorkerId).OnDelete(DeleteBehavior.SetNull);

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("20240527200201_MigrateJobIdToUlid")]
public partial class MigrateJobIdToUlid : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""UPDATE "jobs" SET "id" = concat('00000000-0', substring("id"::text, 11, 36))::uuid;""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View file

@ -19,7 +19,7 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.4")
.HasAnnotation("ProductVersion", "8.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "antenna_src_enum", new[] { "home", "all", "users", "list", "group", "instances" });
@ -1539,7 +1539,6 @@ namespace Iceshrimp.Backend.Core.Database.Migrations
modelBuilder.Entity("Iceshrimp.Backend.Core.Database.Tables.Job", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("id");

View file

@ -3,4 +3,5 @@ namespace Iceshrimp.Backend.Core.Extensions;
public static class GuidExtensions
{
public static string ToStringLower(this Guid guid) => guid.ToString().ToLowerInvariant();
public static string ToStringLower(this Ulid ulid) => ulid.ToString().ToLowerInvariant();
}

View file

@ -502,7 +502,12 @@ public class PostgresJobQueue<T>(
await using var scope = GetScope();
await using var db = GetDbContext(scope);
var job = new Job { Queue = name, Data = JsonSerializer.Serialize(jobData) };
var job = new Job
{
Id = Ulid.NewUlid().ToGuid(),
Queue = name,
Data = JsonSerializer.Serialize(jobData)
};
db.Add(job);
await db.SaveChangesAsync();
await RaiseJobQueuedEvent(db);
@ -515,6 +520,7 @@ public class PostgresJobQueue<T>(
var job = new Job
{
Id = Ulid.NewUlid().ToGuid(),
Queue = name,
Data = JsonSerializer.Serialize(jobData),
Status = Job.JobStatus.Delayed,

View file

@ -58,6 +58,7 @@
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="Ulid" Version="1.3.3" />
<PackageReference Include="WebPush" Version="1.0.24-iceshrimp" />
</ItemGroup>