-
Notifications
You must be signed in to change notification settings - Fork 14
/
dump.rb
89 lines (74 loc) · 2.15 KB
/
dump.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# encoding: UTF-8
# frozen_string_literal: true
require 'fileutils'
require 'rake'
require 'progress'
require 'dump/rails_root'
require 'dump/snapshot'
require 'dump/reader'
require 'dump/writer'
require 'dump/env'
require 'dump/railtie' if defined?(Rails::Railtie)
# Main interface
module Dump
class << self
def versions(options = {})
Snapshot.list(options).each do |dump|
if Dump::Env[:show_size] || $stdout.tty?
puts "#{dump.human_size.to_s.rjust(7)}\t#{dump}"
else
puts dump
end
begin
case options[:summary].to_s.downcase[0, 1]
when *%w[1 t y]
puts Reader.summary(dump.path)
puts
when *%w[2 s]
puts Reader.summary(dump.path, :schema => true)
puts
end
rescue => e
$stderr.puts "Error reading dump: #{e}"
$stderr.puts
end
end
end
def create(options = {})
dump = Snapshot.new(options.merge(:dir => File.join(rails_root, 'dump')))
Writer.create(dump.tmp_path)
File.rename(dump.tmp_path, dump.tgz_path)
puts File.basename(dump.tgz_path)
end
def restore(options = {})
dump = Snapshot.list(options).last
if dump
Reader.restore(dump.path)
else
$stderr.puts 'Avaliable versions:'
$stderr.puts Snapshot.list
end
end
def cleanup(options = {})
unless options[:leave].nil? || /^\d+$/ =~ options[:leave] || options[:leave].downcase == 'none'
fail ArgumentError, 'LEAVE should be number or "none"'
end
to_delete = []
all_dumps = Snapshot.list(options.merge(:all => true))
to_delete.concat(all_dumps.reject{ |dump| dump.ext == 'tgz' })
dumps = Snapshot.list(options)
leave = (options[:leave] || 5).to_i
to_delete.concat(dumps[0, dumps.length - leave]) if dumps.length > leave
to_delete.each do |dump|
dump.lock do
begin
dump.path.unlink
puts "Deleted #{dump.path}"
rescue => e
$stderr.puts "Can not delete #{dump.path} — #{e}"
end
end
end
end
end
end