forked from mtusi2026/mtusi2026lab5
86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Messenger.Api.Data.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class VersionedAttachmentJobs : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropIndex(
|
|
name: "ix_attachment_jobs_attachment_id",
|
|
table: "attachment_jobs");
|
|
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "pipeline_version",
|
|
table: "attachments",
|
|
type: "integer",
|
|
nullable: false,
|
|
defaultValue: 1);
|
|
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "first_attempt",
|
|
table: "attachment_jobs",
|
|
type: "integer",
|
|
nullable: false,
|
|
defaultValue: 1);
|
|
|
|
migrationBuilder.AddColumn<DateTimeOffset>(
|
|
name: "replayed_at",
|
|
table: "attachment_jobs",
|
|
type: "timestamp with time zone",
|
|
nullable: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "ix_attachment_jobs_attachment_id_pipeline_version",
|
|
table: "attachment_jobs",
|
|
columns: new[] { "attachment_id", "pipeline_version" },
|
|
unique: true);
|
|
|
|
// Установки с ранним worker могут хранить только thumbnail. Оригинал уже в S3.
|
|
migrationBuilder.Sql("""
|
|
WITH partial AS (
|
|
UPDATE attachments a SET pipeline_version = 2, status = 'queued', updated_at = now()
|
|
WHERE status = 'ready' AND NOT EXISTS (
|
|
SELECT 1 FROM attachment_variants v WHERE v.attachment_id = a.id
|
|
AND v.pipeline_version = 1 AND v.name = 'optimized'
|
|
)
|
|
RETURNING id, object_key
|
|
)
|
|
INSERT INTO attachment_jobs (id, attachment_id, source_key, pipeline_version, attempt, created_at, next_attempt_at)
|
|
SELECT gen_random_uuid(), id, object_key, 2, 1, now(), now() FROM partial;
|
|
""");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropIndex(
|
|
name: "ix_attachment_jobs_attachment_id_pipeline_version",
|
|
table: "attachment_jobs");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "pipeline_version",
|
|
table: "attachments");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "first_attempt",
|
|
table: "attachment_jobs");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "replayed_at",
|
|
table: "attachment_jobs");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "ix_attachment_jobs_attachment_id",
|
|
table: "attachment_jobs",
|
|
column: "attachment_id",
|
|
unique: true);
|
|
}
|
|
}
|
|
}
|