Class: Mimas::Plugins::PostgreSQL::Commands::CreateDB

Inherits:
CLI::Commands::BaseCommand
  • Object
show all
Defined in:
lib/mimas/plugins/postgresql/commands/create_db.rb

Instance Method Summary collapse

Instance Method Details

#call(server_name:, db_name:, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mimas/plugins/postgresql/commands/create_db.rb', line 14

def call(server_name:, db_name:, **options)
  server = Config.current.servers[server_name.to_sym]
  sudo_user = options[:user]

  ssh(server, user: sudo_user) do |session|
    unless options[:owner]
      password = SecureRandom.alphanumeric(64)
      session.run "sudo -u postgres psql -c \"CREATE ROLE #{db_name} WITH LOGIN PASSWORD \'#{password}\';\"", capture: true

      say ""
      say "Created a new role #{db_name.dup.bold.magenta} with password: #{password.bold.white}"
      say "Ensure you save this password as it cannot be displayed again."
    end

    session.run "sudo -u postgres psql -c \"CREATE DATABASE #{db_name} OWNER #{db_name};\"", capture: true

    say ""
    say "Created database named #{db_name.dup.bold.magenta}"
  end
end