Class: BetterRecord::TableSize

Inherits:
Base
  • Object
show all
Defined in:
app/models/better_record/table_size.rb

Constant Summary collapse

UPDATE_TABLE_SIZES_SQL =

Constants ============================================================

"  BEGIN WORK;\n    LOCK TABLE \#{BetterRecord.db_audit_schema}.table_sizes;\n    TRUNCATE TABLE \#{BetterRecord.db_audit_schema}.table_sizes;\n    INSERT INTO \#{BetterRecord.db_audit_schema}.table_sizes (\n      SELECT\n        *,\n        pg_size_pretty(total_bytes) AS total,\n        pg_size_pretty(idx_bytes) AS idx,\n        pg_size_pretty(toast_bytes) AS toast,\n        pg_size_pretty(tbl_bytes) AS tbl\n      FROM (\n        SELECT\n          *,\n          total_bytes - idx_bytes - COALESCE(toast_bytes,0) AS tbl_bytes\n        FROM (\n          SELECT c.oid,nspname AS schema, relname AS name\n          , c.reltuples AS row_estimate\n          , pg_total_relation_size(c.oid) AS total_bytes\n          , pg_indexes_size(c.oid) AS idx_bytes\n          , pg_total_relation_size(reltoastrelid) AS toast_bytes\n          FROM pg_class c\n          LEFT JOIN pg_namespace n ON n.oid = c.relnamespace\n          WHERE relkind = 'r'\n        ) table_sizes\n      ) table_sizes\n    );\n  COMMIT WORK;\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



55
56
57
58
# File 'app/models/better_record/table_size.rb', line 55

def self.all
  reload_data if last_updated.blank? || (super.first.last_updated > 1.hour.ago)
  super
end

.find_by(*args) ⇒ Object

Class Methods ========================================================



50
51
52
53
# File 'app/models/better_record/table_size.rb', line 50

def self.find_by(*args)
  reload_data
  super *args
end

.reload_dataObject



60
61
62
63
# File 'app/models/better_record/table_size.rb', line 60

def self.reload_data
  @@last_updated ||= Time.now
  connection.execute UPDATE_TABLE_SIZES_SQL
end

Instance Method Details

#action_typeObject



78
79
80
# File 'app/models/better_record/table_size.rb', line 78

def action_type
  ACTIONS[action] || 'UNKNOWN'
end

#changed_columnsObject

Instance Methods =====================================================



74
75
76
# File 'app/models/better_record/table_size.rb', line 74

def changed_columns
  (self.changed_fields || {}).keys.join(', ').presence || 'N/A'
end