Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
Created February 6, 2010 07:22
Show Gist options
  • Save eladmeidar/296598 to your computer and use it in GitHub Desktop.
Save eladmeidar/296598 to your computer and use it in GitHub Desktop.
Rake task to get current database size
# Run rake db:size to get a print of your database size in bytes.
# Works for Mysql, as for the others not really sure.
namespace :db do
desc 'Print data size'
task :size => :environment do
database_name = ActiveRecord::Base.connection.instance_variable_get("@config")[:database]
adapter = ActiveRecord::Base.connection.adapter_name.downcase
sql = case adapter
when "mysql"
"select SUM(data_length + index_length) as bytes from information_schema.TABLES where table_schema = '#{database_name}'"
when "postgres"
"SELECT pg_size_pretty(pg_database_size('#{database_name}'));"
when "oracle", "oci"
"select a.data_size+b.temp_size+c.redo_size+d.controlfile_size from ( select sum(bytes) data_size from dba_data_files) a, ( select nvl(sum(bytes),0) temp_size from dba_temp_files ) b, ( select sum(bytes) redo_size from sys.v_$log ) c, ( select sum(BLOCK_SIZE*FILE_SIZE_BLKS) controlfile_size from v$controlfile) d;"
else
raise "#{adapter} is not supported"
end
puts ActiveRecord::Base.connection.execute(sql).fetch_hash.values.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment