avoid nested transaction in migration

This commit is contained in:
mertalev 2023-11-20 19:00:55 -05:00
parent 375d0320fc
commit a187436be7
No known key found for this signature in database
GPG key ID: 9181CD92C0A1C5E3
2 changed files with 14 additions and 29 deletions

View file

@ -19,43 +19,27 @@ export class UsePgVectors1699746198141 implements MigrationInterface {
await queryRunner.query('DROP EXTENSION IF EXISTS vectors');
await queryRunner.query('CREATE EXTENSION vectors');
await queryRunner.query(`ALTER TABLE asset_faces ALTER COLUMN embedding TYPE vector(${faceDimSize})`);
await queryRunner.query(`
BEGIN;
ALTER TABLE asset_faces ALTER COLUMN embedding TYPE vector(${faceDimSize});
CREATE TABLE smart_search (
"assetId" uuid PRIMARY KEY NOT NULL REFERENCES assets(id) ON DELETE CASCADE,
embedding vector(${clipDimSize}) NOT NULL );
embedding vector(${clipDimSize}) NOT NULL )`);
await queryRunner.query(`
INSERT INTO smart_search("assetId", embedding)
SELECT si."assetId", si."clipEmbedding"
FROM smart_info si
WHERE "clipEmbedding" IS NOT NULL;
ALTER TABLE smart_info DROP COLUMN "clipEmbedding";
COMMIT;
`);
WHERE "clipEmbedding" IS NOT NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE asset_faces ALTER COLUMN embedding TYPE real array`);
await queryRunner.query(`ALTER TABLE smart_info ADD COLUMN IF NOT EXISTS "clipEmbedding" TYPE real array`);
await queryRunner.query(`
BEGIN;
ALTER TABLE asset_faces ALTER COLUMN embedding TYPE real array;
ALTER TABLE smart_info ADD COLUMN IF NOT EXISTS "clipEmbedding" TYPE real array;
INSERT INTO smart_info
("assetId", "clipEmbedding")
SELECT s."assetId", s.embedding
FROM smart_search s
ON CONFLICT (s."assetId") DO UPDATE SET "clipEmbedding" = s.embedding;
DROP TABLE IF EXISTS smart_search;
COMMIT;
`);
ON CONFLICT (s."assetId") DO UPDATE SET "clipEmbedding" = s.embedding`);
await queryRunner.query(`DROP TABLE IF EXISTS smart_search`);
}
}

View file

@ -17,6 +17,7 @@
"strict": true,
"target": "es2020",
"preserveValueImports": false,
"baseUrl": ".",
"paths": {
"$lib": [
"./src/lib"
@ -28,7 +29,7 @@
"./src/api"
],
"@test-data": [
"./src/test-data"
"./src/test-data/*"
]
}
}