Class: Veksel::PgCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/veksel/pg_cluster.rb

Defined Under Namespace

Classes: Database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration_hash) ⇒ PgCluster

Returns a new instance of PgCluster.



7
8
9
# File 'lib/veksel/pg_cluster.rb', line 7

def initialize(configuration_hash)
  @configuration_hash = configuration_hash
end

Instance Attribute Details

#configuration_hashObject (readonly)

Returns the value of attribute configuration_hash.



5
6
7
# File 'lib/veksel/pg_cluster.rb', line 5

def configuration_hash
  @configuration_hash
end

Instance Method Details

#create_database(dbname, template:) ⇒ Object



39
40
41
# File 'lib/veksel/pg_cluster.rb', line 39

def create_database(dbname, template:)
  system(pg_env, %[createdb --no-password --template=#{template} #{dbname}], exception: true)
end

#db_name_for_suffix(suffix) ⇒ Object



21
22
23
24
# File 'lib/veksel/pg_cluster.rb', line 21

def db_name_for_suffix(suffix)
  return main_database if suffix.empty?
  "#{forked_database_prefix}#{suffix}"
end

#drop_database(dbname, dry_run: false) ⇒ Object



53
54
55
56
57
58
# File 'lib/veksel/pg_cluster.rb', line 53

def drop_database(dbname, dry_run: false)
  if dry_run
    puts "[Veksel] Would drop database #{dbname}"
  end
  spawn(pg_env, %[dropdb --no-password #{dbname}])
end

#forked_databasesObject



15
16
17
18
19
# File 'lib/veksel/pg_cluster.rb', line 15

def forked_databases
  list_databases(prefix: forked_database_prefix).map do |name|
    Database.new(name, name.sub(forked_database_prefix, ''))
  end
end

#kill_connection(dbname) ⇒ Object



35
36
37
# File 'lib/veksel/pg_cluster.rb', line 35

def kill_connection(dbname)
  system(pg_env, %[psql #{pg_connection_args('postgres')} -c "select pg_terminate_backend(pid) from pg_stat_activity where datname = '#{dbname}' and pid <> pg_backend_pid();"], exception: true, out: '/dev/null')
end

#list_databases(prefix:) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/veksel/pg_cluster.rb', line 43

def list_databases(prefix:)
  IO.pipe(autoclose: true) do |r, w|
    sql = %[SELECT datname FROM pg_database WHERE datname LIKE '#{prefix}%'];
    psql = spawn(pg_env, %[psql -t #{pg_connection_args('postgres')} -c "#{sql}"], out: w, err: '/dev/null')
    w.close
    Process::Status.wait(psql)
    r.read.split("\n").map(&:strip)
  end
end

#main_databaseObject



11
12
13
# File 'lib/veksel/pg_cluster.rb', line 11

def main_database
  configuration_hash[:veksel_main_database] || configuration_hash[:database]
end

#target_populated?(dbname) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/veksel/pg_cluster.rb', line 26

def target_populated?(dbname)
  IO.pipe do |r, w|
    spawn(pg_env, %[psql -t #{pg_connection_args(dbname)} -c "SELECT 'ok' FROM ar_internal_metadata LIMIT 1;"], out: w, err: '/dev/null')
    pid_grep = spawn(pg_env, %[grep -qw ok], in: r, err: '/dev/null')
    w.close
    Process::Status.wait(pid_grep).success?
  end
end