mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-04 03:27:09 -05:00 
			
		
		
		
	fix(server): face search migration sometimes failing (#10827)
* turn it off and back on * handle missing smart search embedding column * handle missing face embedding column * simplify * Revert "simplify" This reverts commit 8322af0baf2be8d59f868db9868b9694f999e615. * fix migration
This commit is contained in:
		
							parent
							
								
									2b8942026c
								
							
						
					
					
						commit
						0b88bef157
					
				@ -9,6 +9,20 @@ export class AddFaceSearchRelation1718486162779 implements MigrationInterface {
 | 
				
			|||||||
      await queryRunner.query(`SET vectors.pgvector_compatibility=on`);
 | 
					      await queryRunner.query(`SET vectors.pgvector_compatibility=on`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const hasEmbeddings = async (tableName: string): Promise<boolean> => {
 | 
				
			||||||
 | 
					      const columns = await queryRunner.query(
 | 
				
			||||||
 | 
					        `SELECT column_name as name
 | 
				
			||||||
 | 
					        FROM information_schema.columns
 | 
				
			||||||
 | 
					        WHERE table_name = '${tableName}'`);
 | 
				
			||||||
 | 
					      return columns.some((column: { name: string }) => column.name === 'embedding');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const hasAssetEmbeddings = await hasEmbeddings('smart_search');
 | 
				
			||||||
 | 
					    if (!hasAssetEmbeddings) {
 | 
				
			||||||
 | 
					      await queryRunner.query(`TRUNCATE smart_search`);
 | 
				
			||||||
 | 
					      await queryRunner.query(`ALTER TABLE smart_search ADD COLUMN IF NOT EXISTS embedding vector(512) NOT NULL`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await queryRunner.query(`
 | 
					    await queryRunner.query(`
 | 
				
			||||||
      CREATE TABLE face_search (
 | 
					      CREATE TABLE face_search (
 | 
				
			||||||
      "faceId"  uuid PRIMARY KEY REFERENCES asset_faces(id) ON DELETE CASCADE,
 | 
					      "faceId"  uuid PRIMARY KEY REFERENCES asset_faces(id) ON DELETE CASCADE,
 | 
				
			||||||
@ -17,12 +31,23 @@ export class AddFaceSearchRelation1718486162779 implements MigrationInterface {
 | 
				
			|||||||
    await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET STORAGE EXTERNAL`);
 | 
					    await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET STORAGE EXTERNAL`);
 | 
				
			||||||
    await queryRunner.query(`ALTER TABLE smart_search ALTER COLUMN embedding SET STORAGE EXTERNAL`);
 | 
					    await queryRunner.query(`ALTER TABLE smart_search ALTER COLUMN embedding SET STORAGE EXTERNAL`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const hasFaceEmbeddings = await hasEmbeddings('asset_faces')
 | 
				
			||||||
 | 
					    if (hasFaceEmbeddings) {
 | 
				
			||||||
      await queryRunner.query(`
 | 
					      await queryRunner.query(`
 | 
				
			||||||
        INSERT INTO face_search("faceId", embedding)
 | 
					        INSERT INTO face_search("faceId", embedding)
 | 
				
			||||||
        SELECT id, embedding
 | 
					        SELECT id, embedding
 | 
				
			||||||
        FROM asset_faces faces`);
 | 
					        FROM asset_faces faces`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await queryRunner.query(`ALTER TABLE asset_faces DROP COLUMN "embedding"`);
 | 
					    await queryRunner.query(`ALTER TABLE asset_faces DROP COLUMN IF EXISTS embedding`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET DATA TYPE real[]`);
 | 
				
			||||||
 | 
					    await queryRunner.query(`ALTER TABLE face_search ALTER COLUMN embedding SET DATA TYPE vector(512)`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await queryRunner.query(`
 | 
				
			||||||
 | 
					      CREATE INDEX IF NOT EXISTS clip_index ON smart_search
 | 
				
			||||||
 | 
					      USING hnsw (embedding vector_cosine_ops)
 | 
				
			||||||
 | 
					      WITH (ef_construction = 300, m = 16)`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await queryRunner.query(`
 | 
					    await queryRunner.query(`
 | 
				
			||||||
      CREATE INDEX face_index ON face_search
 | 
					      CREATE INDEX face_index ON face_search
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user