Class: Seira::Db

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/seira/db.rb,
lib/seira/db/create.rb

Defined Under Namespace

Classes: Create

Constant Summary collapse

VALID_ACTIONS =
%w[help create delete list restart connect ps kill analyze create-readonly-user psql table-sizes index-sizes vacuum unused-indexes unused-indices user-connections info].freeze
SUMMARY =
"Manage your Cloud SQL Postgres databases.".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#gcloud, gcloud, kubectl, #kubectl

Constructor Details

#initialize(app:, action:, args:, context:) ⇒ Db

Returns a new instance of Db.



15
16
17
18
19
20
# File 'lib/seira/db.rb', line 15

def initialize(app:, action:, args:, context:)
  @app = app
  @action = action
  @args = args
  @context = context
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



13
14
15
# File 'lib/seira/db.rb', line 13

def action
  @action
end

#appObject (readonly)

Returns the value of attribute app.



13
14
15
# File 'lib/seira/db.rb', line 13

def app
  @app
end

#argsObject (readonly)

Returns the value of attribute args.



13
14
15
# File 'lib/seira/db.rb', line 13

def args
  @args
end

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/seira/db.rb', line 13

def context
  @context
end

Instance Method Details

#primary_instanceObject

NOTE: Relies on the pgbouncer instance being named based on the db name, as is done in create command



64
65
66
67
68
69
70
71
72
73
# File 'lib/seira/db.rb', line 64

def primary_instance
  database_url = Helpers.get_secret(context: context, key: 'DATABASE_URL')
  return nil unless database_url

  primary_uri = URI.parse(database_url)
  host = primary_uri.host

  # Convert handshake-onyx-burmese-pgbouncer-service to handshake-onyx-burmese
  host.gsub('-pgbouncer-service', '')
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/seira/db.rb', line 22

def run
  case action
  when 'help'
    run_help
  when 'create'
    run_create
  when 'delete'
    run_delete
  when 'list'
    run_list
  when 'restart'
    run_restart
  when 'connect'
    run_connect
  when 'ps'
    run_ps
  when 'kill'
    run_kill
  when 'analyze'
    run_analyze
  when 'create-readonly-user'
    run_create_readonly_user
  when 'psql'
    run_psql
  when 'table-sizes'
    run_table_sizes
  when 'index-sizes'
    run_index_sizes
  when 'vacuum'
    run_vacuum
  when 'unused-indexes', 'unused-indices'
    run_unused_indexes
  when 'user-connections'
    run_user_connections
  when 'info'
    run_info
  else
    fail "Unknown command encountered"
  end
end