From 20d68b4dbba1f6a6768b6714bf747c9a4ff2b8d0 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 13:46:34 +1100 Subject: [PATCH 1/6] Add a table_sizes method --- lib/postgres_utility.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/postgres_utility.rb b/lib/postgres_utility.rb index 54ff60b..a728176 100644 --- a/lib/postgres_utility.rb +++ b/lib/postgres_utility.rb @@ -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 From efdaaf0eb4ab7d5c757b206b5be768eba2a74bec Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 14:52:24 +1100 Subject: [PATCH 2/6] Add a test --- spec/postgres_utility_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/postgres_utility_spec.rb b/spec/postgres_utility_spec.rb index d2d0e80..bc819d7 100644 --- a/spec/postgres_utility_spec.rb +++ b/spec/postgres_utility_spec.rb @@ -49,6 +49,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) From 2112f71c1dd3af191b850452f1afa7c7810ec94e Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 15:02:01 +1100 Subject: [PATCH 3/6] Let dev / test use a local database configuration --- bin/console | 11 +++++++++++ spec/spec_helper.rb | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/console b/bin/console index 313c4c9..b6492d5 100755 --- a/bin/console +++ b/bin/console @@ -12,4 +12,15 @@ require 'postgres_utility' # Pry.start require 'irb' + +database_config = YAML.load_file('config/database.yml')&.dig('development') || { + adapter: 'postgresql', + host: 'localhost', + username: 'postgres', + password: 'postgres', + port: 5432, + database: 'pg_utility_db' + } +ActiveRecord::Base.establish_connection( database_config ) + IRB.start(__FILE__) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c22e2f2..7ac3fe4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,15 +19,15 @@ 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 = YAML.load_file('config/database.yml')&.dig('test') || { adapter: 'postgresql', host: 'localhost', username: 'postgres', password: 'postgres', port: 5432, database: 'pg_utility_db' - ) + } + ActiveRecord::Base.establish_connection( database_config ) ActiveRecord::Base.connection.execute %{ SET client_min_messages TO warning; DROP TABLE IF EXISTS test_models; From 6f11d825d91043546f16d41d9ddd2072e6909afd Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 16:18:07 +1100 Subject: [PATCH 4/6] Update gems and dependencies --- Gemfile.lock | 96 ++++++++++++++++++++-------------------- postgres_utility.gemspec | 10 ++--- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a29aeec..0b85cb6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/postgres_utility.gemspec b/postgres_utility.gemspec index 2e724f1..85c992e 100644 --- a/postgres_utility.gemspec +++ b/postgres_utility.gemspec @@ -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 From 671bb100fc239330af22fddccd0155cd49c0e38a Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 16:18:40 +1100 Subject: [PATCH 5/6] Update to use new ActiveRecord configuration class --- lib/postgres_utility.rb | 2 +- spec/postgres_utility_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/postgres_utility.rb b/lib/postgres_utility.rb index a728176..f5c5041 100644 --- a/lib/postgres_utility.rb +++ b/lib/postgres_utility.rb @@ -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 diff --git a/spec/postgres_utility_spec.rb b/spec/postgres_utility_spec.rb index bc819d7..8ef590c 100644 --- a/spec/postgres_utility_spec.rb +++ b/spec/postgres_utility_spec.rb @@ -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 From d543e4410682d8e0678d1ae6a4663304c94a1a1b Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 4 Mar 2023 16:22:25 +1100 Subject: [PATCH 6/6] Better config loading --- bin/console | 18 ++++++++++-------- spec/spec_helper.rb | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/bin/console b/bin/console index b6492d5..039fda8 100755 --- a/bin/console +++ b/bin/console @@ -13,14 +13,16 @@ require 'postgres_utility' require 'irb' -database_config = YAML.load_file('config/database.yml')&.dig('development') || { - adapter: 'postgresql', - host: 'localhost', - username: 'postgres', - password: 'postgres', - port: 5432, - database: 'pg_utility_db' - } +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__) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7ac3fe4..dd7d70f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,7 +19,7 @@ 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 - database_config = YAML.load_file('config/database.yml')&.dig('test') || { + database_config = { adapter: 'postgresql', host: 'localhost', username: 'postgres', @@ -27,6 +27,7 @@ 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;