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



66
67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Method Details

#append_datetime_suffix(filename) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



61
62
63
# 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!Object

Raises:

  • (NotImplementedError)


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

def enforce_retention!
  raise NotImplementedError, 'You must implement the enforce_retention! method'
end

#invalid_options_msgObject



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

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

#options_filenameObject



29
30
31
# File 'lib/martilla/storage.rb', line 29

def options_filename
  @options['filename'] || 'backup.sql'
end

#output_filename(gzip) ⇒ Object



33
34
35
36
37
38
# 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:) ⇒ 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

#retention_limitObject



53
54
55
# File 'lib/martilla/storage.rb', line 53

def retention_limit
  @options['retention'].to_i
end

#suffix?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/martilla/storage.rb', line 24

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

#timestamp_regexObject



57
58
59
# File 'lib/martilla/storage.rb', line 57

def timestamp_regex
  /\d{4}-\d{2}-\d{2}T\d{6}/
end