1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-23 12:12:45 +01:00
immich/server/src/infra/migrations/1677613712565-AlbumThumbnailRelation.ts
Jason Rasmussen 8ebac41318
refactor(server)*: tsconfigs (#2689)
* refactor(server): tsconfigs

* chore: dummy commit

* fix: start.sh

* chore: restore original entry scripts
2023-06-08 10:01:07 -05:00

60 lines
2.1 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class AlbumThumbnailRelation1677613712565 implements MigrationInterface {
name = 'AlbumThumbnailRelation1677613712565';
public async up(queryRunner: QueryRunner): Promise<void> {
// Make sure all albums have a valid albumThumbnailAssetId UUID or NULL.
await queryRunner.query(`
UPDATE "albums"
SET
"albumThumbnailAssetId" = (
SELECT
"albums_assets2"."assetsId"
FROM
"assets" "assets",
"albums_assets_assets" "albums_assets2"
WHERE
"albums_assets2"."assetsId" = "assets"."id"
AND "albums_assets2"."albumsId" = "albums"."id"
ORDER BY
"assets"."fileCreatedAt" DESC
LIMIT 1
),
"updatedAt" = CURRENT_TIMESTAMP
WHERE
"albums"."albumThumbnailAssetId" IS NULL
AND EXISTS (
SELECT 1
FROM "albums_assets_assets" "albums_assets"
WHERE "albums"."id" = "albums_assets"."albumsId"
)
OR "albums"."albumThumbnailAssetId" IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM "albums_assets_assets" "albums_assets"
WHERE
"albums"."id" = "albums_assets"."albumsId"
AND "albums"."albumThumbnailAssetId" = "albums_assets"."assetsId"::varchar
)
`);
await queryRunner.query(`
ALTER TABLE "albums"
ALTER COLUMN "albumThumbnailAssetId"
TYPE uuid USING "albumThumbnailAssetId"::uuid
`);
await queryRunner.query(`
ALTER TABLE "albums" ADD CONSTRAINT "FK_05895aa505a670300d4816debce" FOREIGN KEY ("albumThumbnailAssetId") REFERENCES "assets"("id") ON DELETE SET NULL ON UPDATE CASCADE
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "albums" DROP CONSTRAINT "FK_05895aa505a670300d4816debce"`);
await queryRunner.query(`
ALTER TABLE "albums" ALTER COLUMN "albumThumbnailAssetId" TYPE varchar USING "albumThumbnailAssetId"::varchar
`);
}
}