Class: DbHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/db_helper.rb

Class Method Summary collapse

Class Method Details

.find(needle, anchor_left: false, anchor_right: false, excluded_tables: []) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/db_helper.rb', line 100

def self.find(needle, anchor_left: false, anchor_right: false, excluded_tables: [])
  found = {}
  like = "#{anchor_left ? "" : "%"}#{needle}#{anchor_right ? "" : "%"}"

  DB
    .query(REMAP_SQL)
    .each do |r|
      next if excluded_tables.include?(r.table_name)

      rows = DB.query(<<~SQL, like: like)
      SELECT \"#{r.column_name}\"
        FROM \"#{r.table_name}\"
       WHERE \"#{r.column_name}\" LIKE :like
    SQL

      if rows.size > 0
        found["#{r.table_name}.#{r.column_name}"] = rows.map do |row|
          row.public_send(r.column_name)
        end
      end
    end

  found
end

.regexp_replace(pattern, replacement, flags: "gi", match: "~*", excluded_tables: [], verbose: false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/db_helper.rb', line 61

def self.regexp_replace(
  pattern,
  replacement,
  flags: "gi",
  match: "~*",
  excluded_tables: [],
  verbose: false
)
  text_columns = find_text_columns(excluded_tables)

  text_columns.each do |table, columns|
    set =
      columns
        .map do |column|
          replace = "REGEXP_REPLACE(\"#{column[:name]}\", :pattern, :replacement, :flags)"
          replace = truncate(replace, table, column)
          "\"#{column[:name]}\" = #{replace}"
        end
        .join(", ")

    where =
      columns
        .map do |column|
          "\"#{column[:name]}\" IS NOT NULL AND \"#{column[:name]}\" #{match} :pattern"
        end
        .join(" OR ")

    rows = DB.exec(<<~SQL, pattern: pattern, replacement: replacement, flags: flags, match: match)
      UPDATE \"#{table}\"
         SET #{set}
       WHERE #{where}
    SQL

    puts "#{table}=#{rows}" if verbose && rows > 0
  end

  finish!
end

.remap(from, to, anchor_left: false, anchor_right: false, excluded_tables: [], verbose: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/db_helper.rb', line 23

def self.remap(
  from,
  to,
  anchor_left: false,
  anchor_right: false,
  excluded_tables: [],
  verbose: false
)
  like = "#{anchor_left ? "" : "%"}#{from}#{anchor_right ? "" : "%"}"
  text_columns = find_text_columns(excluded_tables)

  text_columns.each do |table, columns|
    set =
      columns
        .map do |column|
          replace = "REPLACE(\"#{column[:name]}\", :from, :to)"
          replace = truncate(replace, table, column)
          "\"#{column[:name]}\" = #{replace}"
        end
        .join(", ")

    where =
      columns
        .map { |column| "\"#{column[:name]}\" IS NOT NULL AND \"#{column[:name]}\" LIKE :like" }
        .join(" OR ")

    rows = DB.exec(<<~SQL, from: from, to: to, like: like)
      UPDATE \"#{table}\"
         SET #{set}
       WHERE #{where}
    SQL

    puts "#{table}=#{rows}" if verbose && rows > 0
  end

  finish!
end