class Tools::Database

Attributes

configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/tools/database.rb, line 4
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

dump(debug: false) click to toggle source

Backup the database and save it on the backup folder set in the configuration. If you need to make the command more verbose, pass `debug: true` in the arguments of the function.

Return the full path of the backup file created in the disk.

# File lib/tools/database.rb, line 14
def dump(debug: false)
  file_path = File.join(backup_folder, "#{file_name}#{file_suffix}.sql")

  cmd = "PGPASSWORD='#{password}' pg_dump -F p -v -O -U '#{user}' -h '#{host}' -d '#{database}' -f '#{file_path}' -p '#{port}' "
  debug ? system(cmd) : system(cmd, err: File::NULL)

  file_path
end
list_files() click to toggle source

List all backup files from the local backup folder.

Return a list of strings containing only the file names.

# File lib/tools/database.rb, line 49
def list_files
  Dir.glob("#{backup_folder}/*.sql")
    .reject { |f| File.directory?(f) }
    .map { |f| Pathname.new(f).basename }
end
reset() click to toggle source

Drop the database and recreate it.

This is done by invoking two Active Record's rake tasks:

  • rake db:drop

  • rake db:create

# File lib/tools/database.rb, line 29
def reset
  system('bundle exec rake db:drop db:create')
end
restore(file_name, debug: false) click to toggle source

Restore the database from a file in the file system.

If you need to make the command more verbose, pass `debug: true` in the arguments of the function.

# File lib/tools/database.rb, line 37
def restore(file_name, debug: false)
  file_path = File.join(backup_folder, file_name)
  output_redirection = debug ? '': ' > /dev/null'
  cmd = "PGPASSWORD='#{password}' psql -U '#{user}' -h '#{host}' -d '#{database}' -f '#{file_path}' -p '#{port}' #{output_redirection}"
  system(cmd)

  file_path
end

Private Instance Methods

backup_folder() click to toggle source
# File lib/tools/database.rb, line 88
def backup_folder
  @backup_folder ||= begin
    File.join(Rails.root, configuration.backup_folder).tap do |folder|
      FileUtils.mkdir_p(folder)
    end
  end
end
database() click to toggle source
# File lib/tools/database.rb, line 67
def database
  @database ||= ::ActiveRecord::Base.connection_config[:database]
end
file_name() click to toggle source
# File lib/tools/database.rb, line 79
def file_name
  @file_name ||= Time.current.strftime('%Y%m%d%H%M%S')
end
file_suffix() click to toggle source
# File lib/tools/database.rb, line 83
def file_suffix
  return if configuration.file_suffix.empty?
  @file_suffix ||= "_#{configuration.file_suffix}"
end
host() click to toggle source
# File lib/tools/database.rb, line 59
def host
  @host ||= ::ActiveRecord::Base.connection_config[:host]
end
password() click to toggle source
# File lib/tools/database.rb, line 75
def password
  @password ||= ::ActiveRecord::Base.connection_config[:password]
end
port() click to toggle source
# File lib/tools/database.rb, line 63
def port
  @port ||= ::ActiveRecord::Base.connection_config[:port]
end
user() click to toggle source
# File lib/tools/database.rb, line 71
def user
  ::ActiveRecord::Base.connection_config[:username]
end