Class: Pgmove::Bucardo

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

Constant Summary collapse

SCHEMA_PATH =
"tmp/schema.sql"
PGPASS_PATH =
"#{Dir.home}/.pgpass"
RELGROUP =
"pgmove"
SYNC_NAME =
"pgmove"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#system!

Methods included from Logger

#logger, stderr, #stderr

Constructor Details

#initialize(src_db, dest_db) ⇒ Bucardo

Returns a new instance of Bucardo.



15
16
17
18
# File 'lib/pgmove/bucardo.rb', line 15

def initialize(src_db, dest_db)
  @src_db = src_db
  @dest_db = dest_db
end

Instance Attribute Details

#dest_dbObject (readonly)

Returns the value of attribute dest_db.



13
14
15
# File 'lib/pgmove/bucardo.rb', line 13

def dest_db
  @dest_db
end

#src_dbObject (readonly)

Returns the value of attribute src_db.



13
14
15
# File 'lib/pgmove/bucardo.rb', line 13

def src_db
  @src_db
end

Instance Method Details

#bucardo(command, db: nil, env: {}) ⇒ Object



30
31
32
33
# File 'lib/pgmove/bucardo.rb', line 30

def bucardo(command, db: nil, env: {})
  db ||= "bucardo"
  system! "bucardo -U #{@src_db.user} -P #{@src_db.pass} -h #{@src_db.host} -p #{@src_db.port} -d #{db} #{command}", env: env
end

#compareObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pgmove/bucardo.rb', line 59

def compare
  begin
    sconn = @src_db.pg_conn
    dconn = @dest_db.pg_conn
    format = "%-75s | %15s | %15s | %5s\n"
    printf format, "table", "source", "dest", "diff"
    puts "-" * (75 + 15 + 15 + 5 + 9)
    @src_db.tables.each do |t|
      src_count = @src_db.row_count(t, conn: sconn)
      dest_count = @dest_db.row_count(t, conn: dconn)
      marker = src_count == dest_count ? '' : "*"
      printf format, t, src_count, dest_count, marker
    end
  ensure
    sconn.close if sconn
    dconn.close if dconn
  end
end

#finalizeObject



78
79
80
81
# File 'lib/pgmove/bucardo.rb', line 78

def finalize
  stop
  @dest_db.finalize
end

#resetObject



35
36
37
38
39
40
41
42
# File 'lib/pgmove/bucardo.rb', line 35

def reset
  stop
  reset_pgpass
  system! %(psql "#{@src_db.conn_str}" -c 'DROP schema if exists bucardo cascade')
  system! %(psql "#{@src_db.conn_str}" -c 'DROP database IF EXISTS bucardo')
  system! %(psql "#{@src_db.conn_str}" -c 'drop role IF EXISTS bucardo')
  @dest_db.reset
end

#setupObject



20
21
22
23
24
25
26
27
28
# File 'lib/pgmove/bucardo.rb', line 20

def setup
  create_dirs
  reset
  install
  copy_schema
  add_db
  add_tables
  add_sync
end

#start_syncObject



44
45
46
# File 'lib/pgmove/bucardo.rb', line 44

def start_sync
  bucardo "start --log-destination log"
end

#statusObject



48
49
50
# File 'lib/pgmove/bucardo.rb', line 48

def status
  bucardo "status pgmove"
end

#stopObject



52
53
54
55
56
57
# File 'lib/pgmove/bucardo.rb', line 52

def stop
  if Dir.glob("tmp/*.pid").size > 0
    bucardo "stop"
    sleep 5
  end
end