forked from mtusi2026/mtusi2026lab5
Реализация 5 лабораторной работы
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Messenger.Api.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AsyncAttachments : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "uploaded_at",
|
||||
table: "attachments",
|
||||
newName: "updated_at");
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "error_code",
|
||||
table: "attachments",
|
||||
type: "text",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "attachment_jobs",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
attachment_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
source_key = table.Column<string>(type: "text", nullable: false),
|
||||
pipeline_version = table.Column<int>(type: "integer", nullable: false),
|
||||
attempt = table.Column<int>(type: "integer", nullable: false),
|
||||
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
next_attempt_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
published_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
lease_until = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_attachment_jobs", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_attachment_jobs_attachments_attachment_id",
|
||||
column: x => x.attachment_id,
|
||||
principalTable: "attachments",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "attachment_variants",
|
||||
columns: table => new
|
||||
{
|
||||
attachment_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
pipeline_version = table.Column<int>(type: "integer", nullable: false),
|
||||
name = table.Column<string>(type: "text", nullable: false),
|
||||
object_key = table.Column<string>(type: "text", nullable: false),
|
||||
content_type = table.Column<string>(type: "text", nullable: false),
|
||||
width = table.Column<int>(type: "integer", nullable: false),
|
||||
height = table.Column<int>(type: "integer", nullable: false),
|
||||
size_bytes = table.Column<long>(type: "bigint", nullable: false),
|
||||
sha256 = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_attachment_variants", x => new { x.attachment_id, x.pipeline_version, x.name });
|
||||
table.ForeignKey(
|
||||
name: "fk_attachment_variants_attachments_attachment_id",
|
||||
column: x => x.attachment_id,
|
||||
principalTable: "attachments",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_attachment_jobs_attachment_id",
|
||||
table: "attachment_jobs",
|
||||
column: "attachment_id",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_attachment_jobs_lease_until",
|
||||
table: "attachment_jobs",
|
||||
column: "lease_until",
|
||||
filter: "lease_until IS NOT NULL");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_attachment_jobs_next_attempt_at",
|
||||
table: "attachment_jobs",
|
||||
column: "next_attempt_at",
|
||||
filter: "published_at IS NULL");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_attachment_variants_object_key",
|
||||
table: "attachment_variants",
|
||||
column: "object_key",
|
||||
unique: true);
|
||||
|
||||
// Старые originals уже устойчивы. Обрабатываем их на месте, сохраняя ID и ссылки сообщений.
|
||||
migrationBuilder.Sql("""
|
||||
INSERT INTO attachment_jobs (id, attachment_id, source_key, pipeline_version, attempt, created_at, next_attempt_at)
|
||||
SELECT gen_random_uuid(), id, object_key, 1, 1, now(), now() FROM attachments WHERE status = 'ready';
|
||||
UPDATE attachments SET status = 'queued', updated_at = now() WHERE status = 'ready';
|
||||
""");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql("""
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM attachments WHERE status <> 'ready') THEN
|
||||
RAISE EXCEPTION 'Lab4 requires a published original for every attachment';
|
||||
END IF;
|
||||
END $$;
|
||||
""");
|
||||
migrationBuilder.DropTable(
|
||||
name: "attachment_jobs");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "attachment_variants");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "error_code",
|
||||
table: "attachments");
|
||||
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "updated_at",
|
||||
table: "attachments",
|
||||
newName: "uploaded_at");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user