Module: ParadeDB::MigrationHelpers

Defined in:
lib/parade_db/migration_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_bm25_index(table, fields:, key_field:, name: nil, index_options: nil, where: nil, if_not_exists: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/parade_db/migration_helpers.rb', line 24

def add_bm25_index(table, fields:, key_field:, name: nil, index_options: nil, where: nil, if_not_exists: false)
  ensure_postgresql_adapter!
  anonymous = Class.new(ParadeDB::Index)
  anonymous.table_name = table
  anonymous.key_field = key_field
  anonymous.index_name = name unless name.nil?
  anonymous.fields = fields
  anonymous.index_options = index_options unless index_options.nil?
  anonymous.where = where unless where.nil?

  create_paradedb_index(anonymous, if_not_exists: if_not_exists)
end

#create_paradedb_index(index_klass, if_not_exists: false) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/parade_db/migration_helpers.rb', line 7

def create_paradedb_index(index_klass, if_not_exists: false)
  ensure_postgresql_adapter!
  resolved = resolve_index_klass(index_klass)
  compiled = resolved.compiled_definition
  execute(build_create_sql(compiled, if_not_exists: if_not_exists))
  remember_schema_index_reference(resolved)
end

#dump_paradedb_indexes(stream) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/parade_db/migration_helpers.rb', line 55

def dump_paradedb_indexes(stream)
  rows = paradedb_bm25_index_rows
  return if rows.empty?

  stream.puts
  rows.each do |row|
    ruby_stmt = bm25_index_to_ruby(row)
    stream.puts "  #{ruby_stmt}"
  end
end

#paradedb_bm25_index_namesObject



70
71
72
# File 'lib/parade_db/migration_helpers.rb', line 70

def paradedb_bm25_index_names
  paradedb_bm25_index_rows.map { |r| r["index_name"] }
end

#paradedb_schema_index_referencesObject



66
67
68
# File 'lib/parade_db/migration_helpers.rb', line 66

def paradedb_schema_index_references
  (@paradedb_schema_index_references || []).uniq.sort
end

#reindex_bm25(table, name: nil, concurrently: false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/parade_db/migration_helpers.rb', line 44

def reindex_bm25(table, name: nil, concurrently: false)
  ensure_postgresql_adapter!
  if concurrently && transaction_open_for_paradedb?
    raise ArgumentError, "reindex_bm25 concurrently: true cannot run inside a transaction"
  end

  index_name = (name || "#{table}_bm25_idx").to_s
  modifier = concurrently ? " CONCURRENTLY" : ""
  execute("REINDEX INDEX#{modifier} #{quote_table_name(index_name)}")
end

#remove_bm25_index(table, name: nil, if_exists: false) ⇒ Object



37
38
39
40
41
42
# File 'lib/parade_db/migration_helpers.rb', line 37

def remove_bm25_index(table, name: nil, if_exists: false)
  ensure_postgresql_adapter!
  index_name = (name || "#{table}_bm25_idx").to_s
  prefix = if_exists ? "IF EXISTS " : ""
  execute("DROP INDEX #{prefix}#{quote_table_name(index_name)}")
end

#replace_paradedb_index(index_klass) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/parade_db/migration_helpers.rb', line 15

def replace_paradedb_index(index_klass)
  ensure_postgresql_adapter!
  resolved = resolve_index_klass(index_klass)
  compiled = resolved.compiled_definition
  remove_bm25_index(compiled.table_name, name: compiled.index_name, if_exists: true)
  execute(build_create_sql(compiled, if_not_exists: false))
  remember_schema_index_reference(resolved)
end