Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 356ea77

Browse files
authored
FIX: Split backfill into separate migrations to use independent transactions (#1063)
1 parent 09ca123 commit 356ea77

5 files changed

+56
-58
lines changed

db/migrate/20241230153300_new_embeddings_tables.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ def up
6565
WHERE model_id = #{model_id} AND strategy_id = 1;
6666
SQL
6767
end
68-
69-
# Copy data from old tables to new tables
70-
execute <<~SQL
71-
INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
72-
SELECT * FROM ai_topic_embeddings;
73-
SQL
74-
75-
execute <<~SQL
76-
INSERT INTO ai_posts_embeddings (post_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
77-
SELECT * FROM ai_post_embeddings;
78-
SQL
79-
80-
execute <<~SQL
81-
INSERT INTO ai_document_fragments_embeddings (rag_document_fragment_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
82-
SELECT * FROM ai_document_fragment_embeddings;
83-
SQL
8468
end
8569

8670
def down
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
class BackfillTopicEmbeddings < ActiveRecord::Migration[7.2]
3+
def up
4+
not_backfilled = DB.query_single("SELECT COUNT(*) FROM ai_topics_embeddings").first.to_i == 0
5+
6+
if not_backfilled
7+
# Copy data from old tables to new tables
8+
execute <<~SQL
9+
INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
10+
SELECT * FROM ai_topic_embeddings;
11+
SQL
12+
end
13+
end
14+
15+
def down
16+
raise ActiveRecord::IrreversibleMigration
17+
end
18+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
class BackfillPostEmbeddings < ActiveRecord::Migration[7.2]
3+
def up
4+
not_backfilled = DB.query_single("SELECT COUNT(*) FROM ai_posts_embeddings").first.to_i == 0
5+
6+
if not_backfilled
7+
# Copy data from old tables to new tables
8+
execute <<~SQL
9+
INSERT INTO ai_posts_embeddings (post_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
10+
SELECT * FROM ai_post_embeddings;
11+
SQL
12+
end
13+
end
14+
15+
def down
16+
raise ActiveRecord::IrreversibleMigration
17+
end
18+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
class BackfillRagEmbeddings < ActiveRecord::Migration[7.2]
3+
def up
4+
not_backfilled =
5+
DB.query_single("SELECT COUNT(*) FROM ai_document_fragments_embeddings").first.to_i == 0
6+
7+
if not_backfilled
8+
# Copy data from old tables to new tables
9+
execute <<~SQL
10+
INSERT INTO ai_document_fragments_embeddings (rag_document_fragment_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
11+
SELECT * FROM ai_document_fragment_embeddings;
12+
SQL
13+
end
14+
end
15+
16+
def down
17+
raise ActiveRecord::IrreversibleMigration
18+
end
19+
end

db/post_migrate/20250113171444_drop_old_embedding_tables.rb

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,7 @@
22
class DropOldEmbeddingTables < ActiveRecord::Migration[7.2]
33
def up
44
# Copy rag embeddings created during deploy.
5-
execute <<~SQL
6-
INSERT INTO ai_document_fragments_embeddings (rag_document_fragment_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
7-
(
8-
SELECT ai_document_fragment_embeddings.*
9-
FROM ai_document_fragment_embeddings
10-
LEFT OUTER JOIN ai_document_fragments_embeddings ON ai_document_fragment_embeddings.rag_document_fragment_id = ai_document_fragments_embeddings.rag_document_fragment_id
11-
WHERE ai_document_fragments_embeddings.rag_document_fragment_id IS NULL
12-
)
13-
SQL
14-
15-
execute <<~SQL
16-
DROP INDEX IF EXISTS ai_topic_embeddings_1_1_search_bit;
17-
DROP INDEX IF EXISTS ai_topic_embeddings_2_1_search_bit;
18-
DROP INDEX IF EXISTS ai_topic_embeddings_3_1_search_bit;
19-
DROP INDEX IF EXISTS ai_topic_embeddings_4_1_search_bit;
20-
DROP INDEX IF EXISTS ai_topic_embeddings_5_1_search_bit;
21-
DROP INDEX IF EXISTS ai_topic_embeddings_6_1_search_bit;
22-
DROP INDEX IF EXISTS ai_topic_embeddings_7_1_search_bit;
23-
DROP INDEX IF EXISTS ai_topic_embeddings_8_1_search_bit;
24-
25-
DROP INDEX IF EXISTS ai_post_embeddings_1_1_search_bit;
26-
DROP INDEX IF EXISTS ai_post_embeddings_2_1_search_bit;
27-
DROP INDEX IF EXISTS ai_post_embeddings_3_1_search_bit;
28-
DROP INDEX IF EXISTS ai_post_embeddings_4_1_search_bit;
29-
DROP INDEX IF EXISTS ai_post_embeddings_5_1_search_bit;
30-
DROP INDEX IF EXISTS ai_post_embeddings_6_1_search_bit;
31-
DROP INDEX IF EXISTS ai_post_embeddings_7_1_search_bit;
32-
DROP INDEX IF EXISTS ai_post_embeddings_8_1_search_bit;
33-
34-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_1_1_search_bit;
35-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_2_1_search_bit;
36-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_3_1_search_bit;
37-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_4_1_search_bit;
38-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_5_1_search_bit;
39-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_6_1_search_bit;
40-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_7_1_search_bit;
41-
DROP INDEX IF EXISTS ai_document_fragment_embeddings_8_1_search_bit;
42-
SQL
43-
44-
drop_table :ai_topic_embeddings
45-
drop_table :ai_post_embeddings
46-
drop_table :ai_document_fragment_embeddings
5+
# noop. TODO(roman): Will follow-up with a new migration to drop these tables.
476
end
487

498
def down

0 commit comments

Comments
 (0)