Class: Martilla::Storage

Inherits:
Component show all
Defined in:
lib/martilla/storage.rb

Direct Known Subclasses

Local, S3, Scp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Component

#bash

Constructor Details

#initialize(config) ⇒ Storage

Returns a new instance of Storage.

Raises:



7
8
9
10
# File 'lib/martilla/storage.rb', line 7

def initialize(config)
  @options = config
  raise Error.new(invalid_options_msg) if @options.nil?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/martilla/storage.rb', line 5

def options
  @options
end

Class Method Details

.create(config = {}) ⇒ Object

When a new Storage is supported it needs to go here



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/martilla/storage.rb', line 50

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

Instance Method Details

#append_datetime_suffix(filename) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/martilla/storage.rb', line 32

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) ⇒ Object



45
46
47
# File 'lib/martilla/storage.rb', line 45

def config_error(config_name)
  Error.new("Storage adapter configuration requires #{config_name}. Details here: https://github.com/fdoxyz/martilla")
end

#invalid_options_msgObject



16
17
18
# File 'lib/martilla/storage.rb', line 16

def invalid_options_msg
  'Storage configuration is invalid. Details here: https://github.com/fdoxyz/martilla'
end

#output_filename(gzip) ⇒ Object



25
26
27
28
29
30
# File 'lib/martilla/storage.rb', line 25

def output_filename(gzip)
  filename = @options['filename'] || 'backup.sql'
  filename = append_datetime_suffix(filename) if suffix?
  filename = "#{filename}.gz" if gzip
  filename
end

#persist(temp_file:, gzip:) ⇒ Object

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/martilla/storage.rb', line 12

def persist(temp_file:, gzip:)
  raise NotImplementedError, 'You must implement the persist method'
end

#suffix?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/martilla/storage.rb', line 20

def suffix?
  return true if @options['suffix'].nil?
  @options['suffix']
end