From 8d57c712c3c06fc7287a7f8be2ff8bb1bf087396 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 7 Jun 2018 10:51:16 -0400 Subject: [PATCH] Add DbHelper.find(needle) * searches the entire database for a text string (such as an old CDN name) --- lib/db_helper.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/db_helper.rb b/lib/db_helper.rb index 9d5e1176962..0f06f66ff08 100644 --- a/lib/db_helper.rb +++ b/lib/db_helper.rb @@ -22,4 +22,21 @@ class DbHelper SiteSetting.refresh! end + def self.find(needle) + connection = ActiveRecord::Base.connection.raw_connection + text_columns = connection.async_exec(REMAP_SQL).to_a + args = ["%#{needle}%"] + found = {} + + text_columns.each do |rc| + table_name = rc["table_name"] + column_name = rc["column_name"] + result = connection.async_exec("SELECT #{column_name} FROM #{table_name} WHERE #{column_name} LIKE $1", args) rescue nil + if result&.ntuples > 0 + found["#{column_name}.#{table_name}"] = result.map {|r| r[column_name]} + end + end + found + end + end