import { MigrationInterface, QueryRunner } from 'typeorm'; export class AddDeviceLogs1744540100000 implements MigrationInterface { name = 'AddDeviceLogs1744540100000'; public async up(queryRunner: QueryRunner): Promise { await queryRunner.query(` CREATE TABLE "device_logs" ( "id" UUID DEFAULT uuid_generate_v4() NOT NULL, "device_id" UUID NOT NULL, "level" SMALLINT NOT NULL, "msg" TEXT NOT NULL, "logger_name" VARCHAR(64), "context" JSONB, "logged_at" TIMESTAMPTZ NOT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT "PK_device_logs" PRIMARY KEY ("id"), CONSTRAINT "FK_device_logs_device" FOREIGN KEY ("device_id") REFERENCES "devices"("id") ON DELETE CASCADE ); -- Primary query pattern: recent logs for a device CREATE INDEX "IDX_device_logs_device_logged" ON "device_logs" ("device_id", "logged_at" DESC); -- Filter by level (e.g. show only errors) CREATE INDEX "IDX_device_logs_device_level" ON "device_logs" ("device_id", "level"); -- Purge old logs CREATE INDEX "IDX_device_logs_logged" ON "device_logs" ("logged_at"); `); } public async down(queryRunner: QueryRunner): Promise { await queryRunner.query(`DROP TABLE IF EXISTS "device_logs";`); } }