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



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

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

#compare(other_db) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/pgmove/db.rb', line 98

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



65
66
67
68
# File 'lib/pgmove/db.rb', line 65

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

#disableObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pgmove/db.rb', line 33

def disable
  new_name = "#{@name}_pgmove_disabled"
  disable_sql = "update pg_database set datallowconn = false where datname = '#{@name}'"
  stop_activity_sql = <<~SQL
    select pg_terminate_backend(pid) from pg_stat_activity
    where datname = '#{@name}'
    AND COALESCE(application_name, '') NOT LIKE 'bucardo%' 
    AND COALESCE(application_name, '') NOT LIKE 'pgmove'
  SQL
  rename_sql = "ALTER DATABASE #{@name} RENAME to #{new_name}"
  dbconn = pg_conn
  pg_conn("postgres") do |conn|
    logger.bullet "sql: #{disable_sql}"
    conn.exec disable_sql
    logger.bullet "sql: #{stop_activity_sql}"
    conn.exec stop_activity_sql
    yield dbconn
    dbconn.close
    logger.bullet "sql: #{rename_sql}"
    conn.exec rename_sql
  end
end

#finalizeObject



56
57
58
# File 'lib/pgmove/db.rb', line 56

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_conn(db = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pgmove/db.rb', line 106

def pg_conn(db = nil)
  db ||= @name
  conn = PG::Connection.open(
    dbname: db,
    user: @user,
    password: @pass,
    host: @host,
    port: @port,
    application_name: "pgmove"
  )
  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



91
92
93
94
95
96
# File 'lib/pgmove/db.rb', line 91

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

#schemasObject



85
86
87
88
89
# File 'lib/pgmove/db.rb', line 85

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)


60
61
62
63
# File 'lib/pgmove/db.rb', line 60

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

#tablesObject



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

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