Class: Postgressor::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/postgressor/cli.rb

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



98
99
100
# File 'lib/postgressor/cli.rb', line 98

def __print_version
  puts VERSION
end

#createdbObject



40
41
42
43
44
45
46
47
# File 'lib/postgressor/cli.rb', line 40

def createdb
  preload!

  command = ["createdb", @conf[:db]] + @pg_cli_args
  if execute command, with_env: true
    say "Created database #{@conf[:db]}", :green
  end
end

#createuserObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/postgressor/cli.rb', line 12

def createuser
  preload!

  # Use psql `CREATE USER` instead of `createuser` CLI command, to automatically provide user password:
  is_superuser = "SUPERUSER" if options[:superuser]
  psql_command = "CREATE USER #{@conf[:user]} WITH CREATEDB LOGIN #{is_superuser} PASSWORD '#{@conf[:password]}';"

  command = %W(sudo -i -u postgres psql -c #{psql_command})
  if execute command
    say "Created user #{@conf[:user]}", :green
  end
end

#dropdbObject



51
52
53
54
55
56
57
58
# File 'lib/postgressor/cli.rb', line 51

def dropdb
  preload!

  command = ["dropdb", @conf[:db]] + @pg_cli_args
  if execute command, with_env: true
    say "Dropped database #{@conf[:db]}", :green
  end
end

#dropuserObject



27
28
29
30
31
32
33
34
# File 'lib/postgressor/cli.rb', line 27

def dropuser
  preload!

  command = %W(sudo -i -u postgres dropuser #{@conf[:user]})
  if execute command
    say "Dropped user #{@conf[:user]}", :green
  end
end

#dumpdb(dump_file_path = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/postgressor/cli.rb', line 64

def dumpdb(dump_file_path = nil)
  preload!

  unless dump_file_path
    dump_file_path = "#{@conf[:db]}.dump"
  end

  command = %W(pg_dump #{@conf[:db]} -Fc --no-acl --no-owner -f #{dump_file_path}) + @pg_cli_args

  if execute command, with_env: true
    say "Dumped database #{@conf[:db]} to #{dump_file_path} file", :green
  end
end

#restoredb(dump_file_path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/postgressor/cli.rb', line 81

def restoredb(dump_file_path)
  preload!

  set_user_to_superuser if options[:switch_to_superuser]

  command = %W(pg_restore #{dump_file_path} -d #{@conf[:db]} --no-acl --no-owner --verbose) + @pg_cli_args
  if execute command, with_env: true
    say "Restored database #{@conf[:db]} from #{dump_file_path} file", :green
  end

  set_user_to_nosuperuser if options[:switch_to_superuser]
end