Class: DatabasePatcher::PatchEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/database_patcher/patch_entity.rb

Direct Known Subclasses

File, Folder

Defined Under Namespace

Classes: File, Folder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PatchEntity

Returns a new instance of PatchEntity.



15
16
17
# File 'lib/database_patcher/patch_entity.rb', line 15

def initialize(path)
  @path = path
end

Class Method Details

.factory(path) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/database_patcher/patch_entity.rb', line 7

def self.factory(path)
  if ::File.directory?(path)
    self::Folder.new(path)
  else
    self::File.new(path)
  end
end

Instance Method Details

#accepted_extensionsObject



89
90
91
# File 'lib/database_patcher/patch_entity.rb', line 89

def accepted_extensions
  %w(rb ru sql)
end

#down(_connection) ⇒ Object



28
29
30
# File 'lib/database_patcher/patch_entity.rb', line 28

def down(_connection)
  raise
end

#execute_file(connection, path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/database_patcher/patch_entity.rb', line 32

def execute_file(connection, path)
  case ::File.extname(path)

  when '.rb', '.ru'
    connection.instance_eval(::File.read(path))

  when '.sql'
    connection.run(::File.read(path))

  else
    raise_unknown_extension_for(path)

  end
end

#extract_comments(file_path, comment_seperator) ⇒ Object



85
86
87
# File 'lib/database_patcher/patch_entity.rb', line 85

def extract_comments(file_path, comment_seperator)
  ::File.read(file_path).scan(/#{comment_seperator}(.*)/).flatten.map(&:strip).join("\n")
end

#get_comment(file_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/database_patcher/patch_entity.rb', line 71

def get_comment(file_path)
  case ::File.extname(file_path)

  when '.rb', '.ru'
    extract_comments(file_path, '#')

  when '.sql'
    extract_comments(file_path, '--')

  else
    raise_unknown_extension_for(file_path)
  end
end

#md5(string) ⇒ Object



67
68
69
# File 'lib/database_patcher/patch_entity.rb', line 67

def md5(string)
  ::Digest::MD5.hexdigest(string)
end

#patch_recordObject



63
64
65
# File 'lib/database_patcher/patch_entity.rb', line 63

def patch_record
  uniq_indentifier.merge(comment: comment)
end

#raise_unknown_extension_for(path) ⇒ Object



93
94
95
# File 'lib/database_patcher/patch_entity.rb', line 93

def raise_unknown_extension_for(path)
  raise("unknown patch file extension: #{::File.extname(path)} (#{path})")
end

#register_this_patch(connection) ⇒ Object



47
48
49
# File 'lib/database_patcher/patch_entity.rb', line 47

def register_this_patch(connection)
  connection[:installed_patches].insert(patch_record)
end

#timestampObject



19
20
21
22
# File 'lib/database_patcher/patch_entity.rb', line 19

def timestamp
  basename = ::File.basename(@path, '.*')
  basename.scan(/^\d+/).flatten.first.to_i
end

#uniq_indentifierObject



55
56
57
58
59
60
61
# File 'lib/database_patcher/patch_entity.rb', line 55

def uniq_indentifier
  {
    timestamp: timestamp,
    md5_down: md5_down,
    md5_up: md5_up
  }
end

#unregister_this_patch(connection) ⇒ Object



51
52
53
# File 'lib/database_patcher/patch_entity.rb', line 51

def unregister_this_patch(connection)
  connection[:installed_patches].where(uniq_indentifier).delete
end

#up(_connection) ⇒ Object



24
25
26
# File 'lib/database_patcher/patch_entity.rb', line 24

def up(_connection)
  raise
end