Module: Cawcaw::Command::Postgresql::Table

Includes:
Database::Table
Defined in:
lib/cawcaw/command/postgresql/table.rb

Class Method Summary collapse

Methods included from Database::Table

included

Methods included from Database::Table::ClassMethods

#count_table_sizes, #get_full_table_paths, #initialize_params, #run

Class Method Details

.count_table_sizes(full_table_paths, params) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cawcaw/command/postgresql/table.rb', line 35

def self.count_table_sizes(full_table_paths, params)
  ActiveRecord::Base.establish_connection(params)
  model_class = ActiveRecord::Base
  if false && params[:stats_flag]
    count_sql = <<-EOF
select nspname || '.' || relname as full_table_path, reltuples as record_size, relpages * 8 as byte_size
from pg_class
inner join pg_namespace
on pg_class.relnamespace = pg_namespace.oid
where relkind='r'
  and (
    #{
      full_table_paths.map{|full_table_path|
"(nspname || '.' || relname) = #{model_class.sanitize(full_table_path)}"
      }.join(" or ")
    }
  )
    EOF
  else
    count_sql = <<-EOF
#{
  full_table_paths.map{|full_table_path|
    "(select #{model_class.sanitize(full_table_path)} as full_table_path, count(*) as record_size from #{full_table_path})"
  }.join(" union ")
}
    EOF
  end
  results = ActiveRecord::Base.connection.execute count_sql
  return results
end

.get_full_table_paths(table_paths, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cawcaw/command/postgresql/table.rb', line 20

def self.get_full_table_paths(table_paths, params)
  full_table_paths = {}
  table_paths.each do |table_path|
    table_path_pair = table_path.split(/\./)
    if /[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)?/ !~ table_path
      Cawcaw.logger.error("#{params[:adapter_name]} table name '#{table_path}' is invalid")
      return nil
    end
    table_name = table_path_pair.pop
    schema_name = table_path_pair.pop || "public"
    full_table_paths[table_path] = "#{schema_name}.#{table_name}"
  end
  return full_table_paths
end

.initialize_params(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cawcaw/command/postgresql/table.rb', line 7

def self.initialize_params(params)
  params[:adapter] ||= "postgresql"
  params[:host] ||= "localhost"
  params[:port] ||= 5432
  params[:adapter_name] ||= params[:adapter]
  
  params[:graph_title] ||= "#{params[:adapter_name]} records"
  params[:graph_args] ||= "--base 1000"
  params[:graph_vlabel] ||= "records"
  params[:graph_category] ||= params[:adapter_name]
  params[:graph_info] ||= "#{params[:adapter_name]} record size"
end