Module: Judoscale::JobMetricsCollector::ActiveRecordHelper

Defined in:
lib/judoscale/job_metrics_collector/active_record_helper.rb

Class Method Summary collapse

Class Method Details

.cleanse_sql(sql) ⇒ Object

Cleanup any whitespace characters (including new lines) from the SQL for simpler logging. Reference: ActiveSupport’s squish! method. api.rubyonrails.org/classes/String.html#method-i-squish



8
9
10
11
12
13
# File 'lib/judoscale/job_metrics_collector/active_record_helper.rb', line 8

def self.cleanse_sql(sql)
  sql = sql.dup
  sql.gsub!(/[[:space:]]+/, " ")
  sql.strip!
  sql
end

.table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/judoscale/job_metrics_collector/active_record_helper.rb', line 22

def self.table_exists?(table_name)
  ::ActiveRecord::Base.connection.table_exists?(table_name)
rescue ActiveRecord::NoDatabaseError
  false
end

.table_exists_for_model?(model) ⇒ Boolean

This will respect a multiple-database setup, unlike the table_exists? method.

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/judoscale/job_metrics_collector/active_record_helper.rb', line 16

def self.table_exists_for_model?(model)
  model.connection.schema_cache.data_source_exists?(model.table_name)
rescue ActiveRecord::NoDatabaseError
  false
end