You may think to sum up the size all ActiveStorage files, you could simply execute the following query:
ActiveStorage::Blob.sum(:byte_size)
But this may produce numbers too large for the resulting column type:
ActiveRecord::RangeError (TinyTds::Error: Arithmetic overflow error converting expression to data type int.)
The solution is to cast:
ActiveStorage::Blob.connection.exec_query(
'SELECT SUM(CAST(byte_size AS BIGINT)) from active_storage_blobs'
).first
Top comments (0)