Class: RailsConsolePro::Services::TableSizeCalculator
- Inherits:
-
Object
- Object
- RailsConsolePro::Services::TableSizeCalculator
- Defined in:
- lib/rails_console_pro/services/table_size_calculator.rb
Overview
Service for calculating database table sizes
Class Method Summary collapse
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(connection, table_name) ⇒ TableSizeCalculator
constructor
A new instance of TableSizeCalculator.
Constructor Details
#initialize(connection, table_name) ⇒ TableSizeCalculator
Returns a new instance of TableSizeCalculator.
11 12 13 14 |
# File 'lib/rails_console_pro/services/table_size_calculator.rb', line 11 def initialize(connection, table_name) @connection = connection @table_name = table_name end |
Class Method Details
.calculate(connection, table_name) ⇒ Object
7 8 9 |
# File 'lib/rails_console_pro/services/table_size_calculator.rb', line 7 def self.calculate(connection, table_name) new(connection, table_name).calculate end |
Instance Method Details
#calculate ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rails_console_pro/services/table_size_calculator.rb', line 16 def calculate adapter_name = @connection.adapter_name.downcase quoted_table = @connection.quote(@table_name) size_value = case adapter_name when /postgresql/ result = @connection.execute("SELECT pg_total_relation_size(#{quoted_table}) as size") row = result.first row['size'] || row[:size] when /mysql/ result = @connection.execute( "SELECT data_length + index_length as size FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = #{quoted_table}" ) row = result.first row['size'] || row[:size] else nil end return nil unless size_value size_value.is_a?(Numeric) ? size_value : size_value.to_i rescue => e nil end |