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



91
92
93
# File 'lib/database_patcher/patch_entity.rb', line 91

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



87
88
89
# File 'lib/database_patcher/patch_entity.rb', line 87

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



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

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



69
70
71
# File 'lib/database_patcher/patch_entity.rb', line 69

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

#patch_recordObject



65
66
67
# File 'lib/database_patcher/patch_entity.rb', line 65

def patch_record
  uniq_indentifier.merge(comment: comment)
end

#raise_unknown_extension_for(path) ⇒ Object



95
96
97
# File 'lib/database_patcher/patch_entity.rb', line 95

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

#register_this_patch(connection) ⇒ Object



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

def register_this_patch(connection)
  connection[:installed_patches].insert(patch_record)
  $stdout.puts("Patch applied: #{uniq_indentifier[:timestamp]}")
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



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

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

#unregister_this_patch(connection) ⇒ Object



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

def unregister_this_patch(connection)
  connection[:installed_patches].where(uniq_indentifier).delete
  $stdout.puts("Patch removed: #{uniq_indentifier[:timestamp]}")
end

#up(_connection) ⇒ Object



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

def up(_connection)
  raise
end