class Martilla::Storage

Attributes

options[R]

Public Class Methods

create(config = {}) click to toggle source

When a new Storage is supported it needs to go here

# File lib/martilla/storage.rb, line 66
def self.create(config = {})
  case config['type'].downcase
  when 'local'
    Local.new(config['options'])
  when 's3'
    S3.new(config['options'])
  when 'scp'
    Scp.new(config['options'])
  else
    raise Error.new("Invalid Storage type: #{config['type']}")
  end
end
new(config) click to toggle source
# File lib/martilla/storage.rb, line 7
def initialize(config)
  @options = config
  raise Error.new(invalid_options_msg) if @options.nil?
end

Public Instance Methods

append_datetime_suffix(filename) click to toggle source
# File lib/martilla/storage.rb, line 40
def append_datetime_suffix(filename)
  dirname = File.dirname(filename)
  basename = File.basename(filename, '.*')

  # 'dir_with_name' is the original filename WITHOUT the extension
  dir_with_name = "#{dirname}/#{basename}"
  dir_with_name = basename if dirname == '.'

  extension = filename.gsub(dir_with_name, '')
  timestamp = Time.now.strftime("%Y-%m-%dT%H%M%S")
  "#{dirname}/#{basename}_#{timestamp}#{extension}"
end
config_error(config_name) click to toggle source
# File lib/martilla/storage.rb, line 61
def config_error(config_name)
  Error.new("Storage adapter configuration requires #{config_name}. Details here: https://github.com/fdoxyz/martilla")
end
enforce_retention!() click to toggle source
# File lib/martilla/storage.rb, line 16
def enforce_retention!
  raise NotImplementedError, 'You must implement the enforce_retention! method'
end
invalid_options_msg() click to toggle source
# File lib/martilla/storage.rb, line 20
def invalid_options_msg
  'Storage configuration is invalid. Details here: https://github.com/fdoxyz/martilla'
end
options_filename() click to toggle source
# File lib/martilla/storage.rb, line 29
def options_filename
  @options['filename'] || 'backup.sql'
end
output_filename(gzip) click to toggle source
# File lib/martilla/storage.rb, line 33
def output_filename(gzip)
  filename = options_filename
  filename = append_datetime_suffix(filename) if suffix?
  filename = "#{filename}.gz" if gzip
  filename
end
persist(temp_file:, gzip:) click to toggle source
# File lib/martilla/storage.rb, line 12
def persist(temp_file:, gzip:)
  raise NotImplementedError, 'You must implement the persist method'
end
retention_limit() click to toggle source
# File lib/martilla/storage.rb, line 53
def retention_limit
  @options['retention'].to_i
end
suffix?() click to toggle source
# File lib/martilla/storage.rb, line 24
def suffix?
  return true if @options['suffix'].nil?
  @options['suffix']
end
timestamp_regex() click to toggle source
# File lib/martilla/storage.rb, line 57
def timestamp_regex
  /\d{4}-\d{2}-\d{2}T\d{6}/
end