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

Commit 0bb8f71

Browse files
committed
FIX: Split backfill into separate migrations to use independent transactions
1 parent 09ca123 commit 0bb8f71

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
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

0 commit comments

Comments
 (0)