From a3b5bda668194600708db7e97238508f8e997e1e Mon Sep 17 00:00:00 2001 From: Alejandro Huertas Herrero Date: Mon, 9 Sep 2019 19:45:13 +0200 Subject: [PATCH] F #3530: add command to create FTS idx (#3671) (cherry picked from commit eb54b9b50119e9f802bbf9b3f3e74b740bbf776b) --- src/onedb/onedb | 25 +++++++++++++++++++++++++ src/onedb/onedb.rb | 11 +++++++++++ src/onedb/onedb_backend.rb | 17 +++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/onedb/onedb b/src/onedb/onedb index a0464d665b..5818885dbd 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -73,6 +73,12 @@ FEDERATED = { :description => "Limit backup/restore to federated tables" } +RECREATE = { + :name => 'recreate', + :large => '--recreate', + :description => 'Delete FTS index and create a new one' +} + ############################################################################### # SQLite options ############################################################################### @@ -739,4 +745,23 @@ cmd=CommandParser::CmdParser.new(ARGV) do 0 # exit code end + + ########################################################################### + # Create and recreate FTS index in vmpool + ########################################################################### + fts_desc = <<-EOT.unindent + Create or recreate FTS index in vm pool to be able to search VMs + EOT + + command :'create-index', fts_desc, :options => [RECREATE] do + begin + helper = OneDB.new(options) + helper.fts_index(options.has_key? :recreate) + rescue StandardError => e + STDERR.puts e.message + [-1, e.message] + end + + 0 + end end diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index b3fcc0b731..782a6f4b82 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -505,6 +505,17 @@ class OneDB end end + # Create or recreate FTS index on vm_pool search_token column + # + # @param recreate [Boolean] True to delete the index and create it again + def fts_index(recreate = false) + if backend.is_a? BackEndSQLite + raise 'This is operation is not supported for sqlite backend' + end + + backend.fts_index(recreate) + end + private def one_not_running() diff --git a/src/onedb/onedb_backend.rb b/src/onedb/onedb_backend.rb index f5c410ea67..73a05fa081 100644 --- a/src/onedb/onedb_backend.rb +++ b/src/onedb/onedb_backend.rb @@ -296,6 +296,23 @@ class BackEndMySQL < OneDBBacKEnd puts "MySQL DB #{@db_name} at #{@server} restored." end + # Create or recreate FTS index on vm_pool search_token column + # + # @param recreate [Boolean] True to delete the index and create it again + def fts_index(recreate = false) + connect_db + + if recreate + @db.alter_table(:vm_pool) do + drop_index :search_token, name: 'ftidx' + end + end + + @db.alter_table(:vm_pool) do + add_full_text_index :search_token, name: 'ftidx' + end + end + private def connect_db