Class: BetterRecord::TableSize
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- BetterRecord::TableSize
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
Methods inherited from Base
#dup, full_table_name, gender_enum, #get_hashed_string, get_hashed_string, #indifferent_attributes, reset_all_schemas, reset_qualified_schema, saved_qualified_schema, schema_qualified, set_audit_methods!, table_name_only
Class Method Details
63
64
65
66
|
# File 'app/models/better_record/table_size.rb', line 63
def self.all
reload_data if self.last_updated.blank? || (self.last_updated < 1.hour.ago)
super
end
|
.find_by(*args) ⇒ Object
58
59
60
61
|
# File 'app/models/better_record/table_size.rb', line 58
def self.find_by(*args)
reload_data
super *args
end
|
.last_updated ⇒ Object
73
74
75
|
# File 'app/models/better_record/table_size.rb', line 73
def self.last_updated
@@last_updated ||= super_all.first&.updated_at
end
|
.reload_data ⇒ Object
68
69
70
71
|
# File 'app/models/better_record/table_size.rb', line 68
def self.reload_data
connection.execute UPDATE_TABLE_SIZES_SQL
self.last_updated = Time.now
end
|
.super_all ⇒ Object
55
|
# File 'app/models/better_record/table_size.rb', line 55
alias :super_all :all
|