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, concurrently: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parade_db/migration_helpers.rb', line 28

def add_bm25_index(table, fields:, key_field:, name: nil, index_options: nil, where: nil, if_not_exists: false, concurrently: 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, concurrently: concurrently)
end

#create_paradedb_index(index_klass, if_not_exists: false, concurrently: false) ⇒ Object



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

def create_paradedb_index(index_klass, if_not_exists: false, concurrently: false)
  ensure_postgresql_adapter!
  if concurrently && transaction_open_for_paradedb?
    raise ArgumentError, "create_paradedb_index concurrently: true cannot run inside a transaction"
  end

  resolved = resolve_index_klass(index_klass)
  compiled = resolved.compiled_definition
  execute(build_create_sql(compiled, if_not_exists: if_not_exists, concurrently: concurrently))
  remember_schema_index_reference(resolved)
end

#dump_paradedb_indexes(stream) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/parade_db/migration_helpers.rb', line 59

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



74
75
76
# File 'lib/parade_db/migration_helpers.rb', line 74

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

#paradedb_schema_index_referencesObject



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

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

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/parade_db/migration_helpers.rb', line 48

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



41
42
43
44
45
46
# File 'lib/parade_db/migration_helpers.rb', line 41

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



19
20
21
22
23
24
25
26
# File 'lib/parade_db/migration_helpers.rb', line 19

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