Class: Chronicler::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicler/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Repository

Returns a new instance of Repository.



5
6
7
# File 'lib/chronicler/repository.rb', line 5

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/chronicler/repository.rb', line 3

def name
  @name
end

Instance Method Details

#branchObject



13
14
15
# File 'lib/chronicler/repository.rb', line 13

def branch
  Git.branch(path)
end

#branchesObject



17
18
19
# File 'lib/chronicler/repository.rb', line 17

def branches
  Git.branches(path)
end

#changesObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chronicler/repository.rb', line 97

def changes
  selected_databases = databases
  stored = (config[:"repository.checksums"] || {}).select{|table, checksum| selected_databases.include?(table.gsub(/-.*/, ""))}
  current = checksums

  added = current.reject{|table, checksum| stored.keys.include?(table)}
  modified = current.select{|table, checksum| stored.keys.include?(table) && (stored[table] != checksum)}
  deleted = stored.reject{|table, checksum| current.keys.include?(table)}

  Hash.new do |hash, key|
    hash[key] = {}
  end.tap do |changes|
    changes[:added] = added if added.any?
    changes[:modified] = modified if modified.any?
    changes[:deleted] = deleted if deleted.any?
  end
end

#checkout(branch) ⇒ Object



62
63
64
65
# File 'lib/chronicler/repository.rb', line 62

def checkout(branch)
  git "checkout #{branch}"
  load
end

#checksumsObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chronicler/repository.rb', line 115

def checksums
  checksums = databases.collect do |database|
    `mysql -u root #{database} -e "SHOW TABLES" -sN`.split("\n").collect do |table|
      `mysql -u root #{database} -e "CHECKSUM TABLE #{table}"`.split("\n").last
    end
  end.flatten.collect do |checksum|
    table, size = checksum.split("\t")
    [table.gsub(".", "-"), size.to_i]
  end
  Hash[checksums]
end

#commit(message, tag = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/chronicler/repository.rb', line 45

def commit(message, tag = nil)
  dump
  config[:"repository.origin"] = Git.head(path) if config[:"repository.origin"].nil?
  config[:"repository.checksums"] = checksums
  git "add ."
  git "commit -m #{message.inspect}"
  git "tag -f #{tag}" if tag
end

#databasesObject



93
94
95
# File 'lib/chronicler/repository.rb', line 93

def databases
  config[:"repository.databases"] || []
end

#dirty?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/chronicler/repository.rb', line 89

def dirty?
  changes.any?
end

#git(command, output = :silence) ⇒ Object



71
72
73
# File 'lib/chronicler/repository.rb', line 71

def git(command, output = :silence)
  run "git #{command}", output
end

#initObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chronicler/repository.rb', line 21

def init
  return if File.exists?(path)

  FileUtils.mkdir(path)
  File.open(File.join(path, ".gitignore"), "w+") do |file|
    file.puts ".DS_Store"
  end

  git "init"
  git "add .gitignore"
  git "commit -m 'Initial commit'"

  path
end

#loadObject



54
55
56
57
58
59
60
# File 'lib/chronicler/repository.rb', line 54

def load
  databases.each do |database|
    Dir[File.join(path, database, "*.sql.gz")].each do |gzip|
      run "gunzip < #{gzip} | mysql -u root #{database}"
    end
  end
end

#new(branch) ⇒ Object



36
37
38
39
# File 'lib/chronicler/repository.rb', line 36

def new(branch)
  config[:"repository.origin"] = Git.head(path)
  git "checkout -b #{branch}"
end

#new?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/chronicler/repository.rb', line 85

def new?
  [nil, Git.head(path)].include?(config[:"repository.origin"])
end

#pathObject



9
10
11
# File 'lib/chronicler/repository.rb', line 9

def path
  File.join(Chronicler.store, name)
end

#reset(commit) ⇒ Object



67
68
69
# File 'lib/chronicler/repository.rb', line 67

def reset(commit)
  git "reset --hard #{commit}"
end

#run(command, output = :silence) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/chronicler/repository.rb', line 75

def run(command, output = :silence)
  command = "cd #{path} && #{command}"
  command << " > /dev/null" if output == :silence
  if output == :system
    system command
  else
    `#{command}`.strip
  end
end

#select(*databases) ⇒ Object



41
42
43
# File 'lib/chronicler/repository.rb', line 41

def select(*databases)
  config[:"repository.databases"] = [databases].flatten.sort
end