Class: DbHelper

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

Constant Summary collapse

REMAP_SQL =
<<~SQL
  SELECT table_name::text, column_name::text, character_maximum_length
    FROM information_schema.columns
   WHERE table_schema = 'public'
     AND is_updatable = 'YES'
     AND (data_type LIKE 'char%' OR data_type LIKE 'text%')
ORDER BY table_name, column_name
SQL
TRIGGERS_SQL =
<<~SQL
  SELECT trigger_name::text
    FROM information_schema.triggers
   WHERE trigger_name LIKE '%_readonly'
SQL
TRUNCATABLE_COLUMNS =
%w[bookmarks.name topic_links.url]

Class Method Summary collapse

Class Method Details

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/db_helper.rb', line 116

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



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/db_helper.rb', line 71

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

  return if text_columns.empty?

  transforms = {
    replacement: ->(column_name) do
      %|REGEXP_REPLACE("#{column_name}", :pattern, :replacement, :flags)|
    end,
    condition: ->(column_name) do
      %|"#{column_name}" IS NOT NULL AND "#{column_name}" #{match} :pattern|
    end,
  }

  query_params = { pattern: pattern, replacement: replacement, flags: flags }

  text_columns.each do |table, columns|
    query_parts =
      build_transform_query_parts(table, columns, skip_max_length_violations, transforms)

    begin
      rows_updated = execute_transform(table, query_parts, query_params)
    rescue PG::StringDataRightTruncation => e
      # Provide more context in the exeption message
      raise_contextualized_transform_exception(e, table, query_parts[:length_constrained_columns])
    end

    if verbose
      skipped_counts =
        skipped_transform_counts(table, query_parts, skip_max_length_violations, query_params)
      log_transform_result(table, rows_updated, skipped_counts)
    end
  end

  finish!
end

.remap(from, to, anchor_left: false, anchor_right: false, excluded_tables: [], verbose: false, skip_max_length_violations: 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
60
61
62
63
64
65
66
67
68
69
# File 'lib/db_helper.rb', line 23

def self.remap(
  from,
  to,
  anchor_left: false,
  anchor_right: false,
  excluded_tables: [],
  verbose: false,
  skip_max_length_violations: false
)
  text_columns = find_text_columns(excluded_tables)

  return if text_columns.empty?

  transforms = {
    replacement: ->(column_name) { %|REPLACE("#{column_name}", :from, :to)| },
    condition: ->(column_name) do
      %|"#{column_name}" IS NOT NULL AND "#{column_name}" LIKE :pattern|
    end,
  }

  query_params = {
    from: from,
    to: to,
    pattern: "#{anchor_left ? "" : "%"}#{from}#{anchor_right ? "" : "%"}",
  }

  text_columns.each do |table, columns|
    query_parts =
      build_transform_query_parts(table, columns, skip_max_length_violations, transforms)

    begin
      rows_updated = execute_transform(table, query_parts, query_params)
    rescue PG::StringDataRightTruncation => e
      # Provide more context in the exeption message
      raise_contextualized_transform_exception(e, table, query_parts[:length_constrained_columns])
    end

    if verbose
      skipped_counts =
        skipped_transform_counts(table, query_parts, skip_max_length_violations, query_params)

      log_transform_result(table, rows_updated, skipped_counts)
    end
  end

  finish!
end