Class: Pgmove::Db

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/pgmove/db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#system!

Methods included from Logger

#logger, stderr, #stderr

Constructor Details

#initialize(name:, user:, pass:, host:, port:, use_tmp: false) ⇒ Db

Returns a new instance of Db.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pgmove/db.rb', line 10

def initialize(name:, user:, pass:, host:, port:, use_tmp: false)
  @user = user
  @pass = pass
  @host = host
  @port = port

  if use_tmp
    @name = "#{name}_bucardo_tmp"
    @final_name = name
  else
    @name = name
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/pgmove/db.rb', line 8

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/pgmove/db.rb', line 8

def name
  @name
end

#passObject (readonly)

Returns the value of attribute pass.



8
9
10
# File 'lib/pgmove/db.rb', line 8

def pass
  @pass
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/pgmove/db.rb', line 8

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/pgmove/db.rb', line 8

def user
  @user
end

Instance Method Details

#bucardo_conn_strObject



47
48
49
# File 'lib/pgmove/db.rb', line 47

def bucardo_conn_str
  "dbhost=#{@host} dbport=#{@port} dbname=#{@name} dbuser=#{@user} password=#{@pass}"
end

#compare(other_db) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/pgmove/db.rb', line 75

def compare(other_db)
  row_counts = row_counts()
  other_row_counts = other_db.row_counts
  row_counts.each do |k, v|
    printf "%-60s %15d %15d\n", k, row_counts[k], other_row_counts[k]
  end
end

#conn_str(db: nil) ⇒ Object



42
43
44
45
# File 'lib/pgmove/db.rb', line 42

def conn_str(db: nil)
  db ||= @name
  "host=#{@host} port=#{@port} dbname=#{db} user=#{@user} password=#{@pass}"
end

#finalizeObject



33
34
35
# File 'lib/pgmove/db.rb', line 33

def finalize
  psql "ALTER DATABASE #{@name} RENAME to #{@final_name}", db: "postgres"
end

#load_schema(path) ⇒ Object



29
30
31
# File 'lib/pgmove/db.rb', line 29

def load_schema(path)
  psql_raw "-f #{path}"
end

#pg_connObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pgmove/db.rb', line 83

def pg_conn
  conn = PG::Connection.open(
    :dbname => @name, 
    :user => @user, 
    :password => @pass, 
    :host => @host, 
    :port => @port
  )
  if block_given?
    yield conn
  else
    conn
  end
ensure
  if block_given?
    conn.close if conn
  end
end

#resetObject



24
25
26
27
# File 'lib/pgmove/db.rb', line 24

def reset
  psql "DROP DATABASE IF EXISTS #{@name}", db: "postgres"
  psql "CREATE DATABASE #{@name}", db: "postgres"
end

#row_count(table, conn:) ⇒ Object



68
69
70
71
72
73
# File 'lib/pgmove/db.rb', line 68

def row_count(table, conn:)
  sql = "select count(*) from #{table}"
  conn.exec(sql)[0]["count"].to_i
rescue
  -1
end

#schemasObject



62
63
64
65
66
# File 'lib/pgmove/db.rb', line 62

def schemas
  sql = "SELECT n.nspname FROM pg_catalog.pg_namespace n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' AND n.nspname <> 'bucardo'"
  rows = psql_query sql
  rows.map { |r| r[0] }
end

#superuser?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/pgmove/db.rb', line 37

def superuser?
  rows = psql_query "select usesuper from pg_user where usename = CURRENT_USER"
  rows[0][0] == 't'
end

#tablesObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/pgmove/db.rb', line 51

def tables
  tables = []
  schemas.each do |s|
    sql = "SELECT table_name FROM information_schema.tables \
         WHERE table_schema='#{s}' AND table_type='BASE TABLE'"
     rows = psql_query sql
     rows.each { |r| tables << "#{s}.#{r[0]}" }
  end
  tables.sort
end