|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class NewEmbeddingsTables < ActiveRecord::Migration[7.2] |
| 4 | + def up |
| 5 | + # We need "model_id" to be a bigint now that it's no longer a hardcoded ID. |
| 6 | + |
| 7 | + create_table :ai_topics_embeddings, id: false do |t| |
| 8 | + t.bigint :topic_id, null: false |
| 9 | + t.bigint :model_id, null: false |
| 10 | + t.integer :model_version, null: false |
| 11 | + t.integer :strategy_id, null: false |
| 12 | + t.integer :strategy_version, null: false |
| 13 | + t.text :digest, null: false |
| 14 | + t.column :embeddings, "halfvec", null: false |
| 15 | + t.timestamps |
| 16 | + |
| 17 | + t.index %i[model_id strategy_id topic_id], |
| 18 | + unique: true, |
| 19 | + name: "index_ai_topics_embeddings_on_model_strategy_topic" |
| 20 | + end |
| 21 | + |
| 22 | + create_table :ai_posts_embeddings, id: false do |t| |
| 23 | + t.bigint :post_id, null: false |
| 24 | + t.bigint :model_id, null: false |
| 25 | + t.integer :model_version, null: false |
| 26 | + t.integer :strategy_id, null: false |
| 27 | + t.integer :strategy_version, null: false |
| 28 | + t.text :digest, null: false |
| 29 | + t.column :embeddings, "halfvec", null: false |
| 30 | + t.timestamps |
| 31 | + |
| 32 | + t.index %i[model_id strategy_id post_id], |
| 33 | + unique: true, |
| 34 | + name: "index_ai_posts_embeddings_on_model_strategy_post" |
| 35 | + end |
| 36 | + |
| 37 | + create_table :ai_document_fragments_embeddings, id: false do |t| |
| 38 | + t.bigint :rag_document_fragment_id, null: false |
| 39 | + t.bigint :model_id, null: false |
| 40 | + t.integer :model_version, null: false |
| 41 | + t.integer :strategy_id, null: false |
| 42 | + t.integer :strategy_version, null: false |
| 43 | + t.text :digest, null: false |
| 44 | + t.column :embeddings, "halfvec", null: false |
| 45 | + t.timestamps |
| 46 | + |
| 47 | + t.index %i[model_id strategy_id rag_document_fragment_id], |
| 48 | + unique: true, |
| 49 | + name: "index_ai_fragments_embeddings_on_model_strategy_fragment" |
| 50 | + end |
| 51 | + |
| 52 | + # Copy data from old tables to new tables |
| 53 | + execute <<-SQL |
| 54 | + INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 55 | + SELECT * FROM ai_topic_embeddings; |
| 56 | +
|
| 57 | + INSERT INTO ai_posts_embeddings (post_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 58 | + SELECT * FROM ai_post_embeddings; |
| 59 | +
|
| 60 | + INSERT INTO ai_document_fragments_embeddings (rag_document_fragment_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 61 | + SELECT * FROM ai_document_fragment_embeddings; |
| 62 | + SQL |
| 63 | + end |
| 64 | + |
| 65 | + def down |
| 66 | + raise ActiveRecord::IrreversibleMigration |
| 67 | + end |
| 68 | +end |
0 commit comments