Module: ActiveRecordVector::MigrationHelper

Defined in:
lib/active_record_vector/migration_helper.rb

Overview

Helper module providing migration DSL extensions for adding vector columns and indexes.

Instance Method Summary collapse

Instance Method Details

#add_vector_column(table_name, column_name, dimensions: 1536, **options) ⇒ Object

Add vector column to a table (supports PostgreSQL pgvector and JSON fallback)



7
8
9
10
11
12
13
14
# File 'lib/active_record_vector/migration_helper.rb', line 7

def add_vector_column(table_name, column_name, dimensions: 1536, **options)
  if postgresql_adapter?
    execute "CREATE EXTENSION IF NOT EXISTS vector;"
    execute "ALTER TABLE #{table_name} ADD COLUMN #{column_name} vector(#{dimensions});"
  else
    add_column table_name, column_name, :text, **options
  end
end

#add_vector_index(table_name, column_name, type: :hnsw, distance: :cosine) ⇒ Object

Add HNSW or IVFFlat vector index to a table



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record_vector/migration_helper.rb', line 17

def add_vector_index(table_name, column_name, type: :hnsw, distance: :cosine)
  return unless postgresql_adapter?

  ops_class = case distance.to_sym
              when :cosine then "vector_cosine_ops"
              when :inner_product, :dot then "vector_ip_ops"
              else "vector_l2_ops"
              end

  index_type = type.to_s.upcase
  index_name = "index_#{table_name}_on_#{column_name}_#{type}"

  execute "CREATE INDEX IF NOT EXISTS #{index_name} ON #{table_name} USING #{index_type} (#{column_name} #{ops_class});"
end