using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Messenger.Api.Data.Migrations { /// public partial class VersionedAttachmentJobs : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropIndex( name: "ix_attachment_jobs_attachment_id", table: "attachment_jobs"); migrationBuilder.AddColumn( name: "pipeline_version", table: "attachments", type: "integer", nullable: false, defaultValue: 1); migrationBuilder.AddColumn( name: "first_attempt", table: "attachment_jobs", type: "integer", nullable: false, defaultValue: 1); migrationBuilder.AddColumn( 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; """); } /// 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); } } }