Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,74 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.6)
activesupport (= 5.2.6)
activerecord (5.2.6)
activemodel (= 5.2.6)
activesupport (= 5.2.6)
arel (>= 9.0)
activesupport (5.2.6)
activemodel (7.0.4.2)
activesupport (= 7.0.4.2)
activerecord (7.0.4.2)
activemodel (= 7.0.4.2)
activesupport (= 7.0.4.2)
activesupport (7.0.4.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
byebug (11.1.3)
concurrent-ruby (1.1.9)
diff-lcs (1.4.4)
i18n (1.8.10)
concurrent-ruby (1.2.2)
diff-lcs (1.5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
minitest (5.14.4)
parallel (1.21.0)
parser (3.0.2.0)
json (2.6.3)
minitest (5.17.0)
parallel (1.22.1)
parser (3.2.1.0)
ast (~> 2.4.1)
pg (1.2.3)
rainbow (3.0.0)
pg (1.4.5)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.1.1)
regexp_parser (2.7.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.21.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.44.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.9.1, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.24.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
thread_safe (0.3.6)
tzinfo (1.2.9)
thread_safe (~> 0.1)
unicode-display_width (2.1.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.27.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.12.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)

PLATFORMS
arm64-darwin-22
x86_64-linux

DEPENDENCIES
activerecord (= 5.2.6)
activerecord (= 7.0.4.2)
byebug
pg (= 1.2.3)
pg (= 1.4.5)
postgres_utility!
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.7)
rake
rspec
rubocop (~> 1.44.1)

BUNDLED WITH
2.2.11
13 changes: 13 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ require 'postgres_utility'
# Pry.start

require 'irb'

database_config = {
adapter: 'postgresql',
host: 'localhost',
username: 'postgres',
password: 'postgres',
port: 5432,
database: 'pg_utility_db'
}
database_config = YAML.load_file('config/database.yml')&.dig('development') if File.exist?('config/database.yml')

ActiveRecord::Base.establish_connection( database_config )

IRB.start(__FILE__)
24 changes: 23 additions & 1 deletion lib/postgres_utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def db_name
end

def db_connection_config
config = ActiveRecord::Base.connection_config
config = ActiveRecord::Base.connection_db_config.configuration_hash
config = rails_connection.config if config[:adapter].match(/makara/i)
config
end
Expand All @@ -68,6 +68,28 @@ def db_size
rails_connection.select_value("select pg_database_size('#{db_name}');").to_i
end

# Returns the table size and the size of its associated indexes : https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE
def table_sizes(to_human: false)
query = "SELECT table_name, "

query += 'pg_size_pretty(' if to_human
query += "pg_table_size(quote_ident(table_name))"
query += ')' if to_human
query += ', '

query += 'pg_size_pretty(' if to_human
query += 'pg_indexes_size(quote_ident(table_name))'
query += ')' if to_human

query += "FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;"

puts query

rails_connection.select_rows(query).inject({}) {|memo, row| memo[row[0]] = {table: row[1], indexes: row[2]}; memo}
end

# Returns true if created, false if already exists, raise if failed.
def create_database
# Stolen from activerecord-3.2.3\lib\active_record\railties\databases.rake
Expand Down
10 changes: 5 additions & 5 deletions postgres_utility.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Gem::Specification.new do |spec|
# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html

spec.add_development_dependency 'activerecord', '5.2.6'
spec.add_development_dependency 'activerecord', '7.0.4.2'
spec.add_development_dependency 'byebug'
spec.add_development_dependency 'pg', '1.2.3'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.7'
spec.add_development_dependency 'pg', '1.4.5'
spec.add_development_dependency 'rake'#, '~> 13.0'
spec.add_development_dependency 'rspec'#, '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.44.1'
end
9 changes: 8 additions & 1 deletion spec/postgres_utility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

describe '.db_name' do
it 'returns the db name' do
expect(PostgresUtility.db_name).to eq('pg_utility_db')
expect(PostgresUtility.db_name).to eq(ActiveRecord::Base.connection_db_config.configuration_hash[:database])
expect(PostgresUtility.db_name).to eq(ActiveRecord::Base.connection.raw_connection.conninfo_hash[:dbname])
end
end

Expand Down Expand Up @@ -49,6 +50,12 @@
end
end

describe '.table_sizes' do
it 'gives the tables and associated index sizes' do
expect(PostgresUtility.table_sizes).to be
end
end

describe '.create_database' do
it 'returns false if db already exists' do
expect(PostgresUtility.create_database).to eq(false)
Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
config.before(:suite) do
# we create a test database if it does not exist
# I do not use database users or password for the tests, using ident authentication instead

ActiveRecord::Base.establish_connection(
database_config = {
adapter: 'postgresql',
host: 'localhost',
username: 'postgres',
password: 'postgres',
port: 5432,
database: 'pg_utility_db'
)
}
database_config = YAML.load_file('config/database.yml')&.dig('test') if File.exist?('config/database.yml')
ActiveRecord::Base.establish_connection( database_config )
ActiveRecord::Base.connection.execute %{
SET client_min_messages TO warning;
DROP TABLE IF EXISTS test_models;
Expand Down