Files
lobehub/packages/database/migrations/0007_fix_embedding_table.sql
Arvin Xu af1f71572f ♻️ refactor: move database to packages (#8874)
* move db

* refactor db import

* refactor eval types

* fix tests

* fix tests

* fix tests

* fix db migration issues

* fix docker issue

* fix tests

* update alias

* fix tests

* update db test for client and server

* refactor db

* update codecov

* update codecov

* update codecov

* add docker pr comments
2025-08-22 11:09:03 +08:00

23 lines
696 B
SQL

-- step 1: create a temporary table to store the rows we want to keep
CREATE TEMP TABLE embeddings_temp AS
SELECT DISTINCT ON (chunk_id) *
FROM embeddings
ORDER BY chunk_id, random();
--> statement-breakpoint
-- step 2: delete all rows from the original table
DELETE FROM embeddings;
--> statement-breakpoint
-- step 3: insert the rows we want to keep back into the original table
INSERT INTO embeddings
SELECT * FROM embeddings_temp;
--> statement-breakpoint
-- step 4: drop the temporary table
DROP TABLE embeddings_temp;
--> statement-breakpoint
-- step 5: now it's safe to add the unique constraint
ALTER TABLE "embeddings" ADD CONSTRAINT "embeddings_chunk_id_unique" UNIQUE("chunk_id");