Class: Dump::Writer
Overview
Instance Attribute Summary collapse
Attributes inherited from Snapshot
#path
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Snapshot
#==, #description, #ext, #human_size, #initialize, #inspect, list, #lock, #name, #parts, #silence, #size, #tags, #tgz_path, #time, #tmp_path
#clean_description, #clean_str, #clean_tag, #clean_tags, #get_filter_tags
Constructor Details
This class inherits a constructor from Dump::Snapshot
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
12
13
14
|
# File 'lib/dump/writer.rb', line 12
def config
@config
end
|
#stream ⇒ Object
Returns the value of attribute stream.
12
13
14
|
# File 'lib/dump/writer.rb', line 12
def stream
@stream
end
|
Class Method Details
.create(path) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/dump/writer.rb', line 14
def self.create(path)
new(path).open do |dump|
dump.silence do
dump.write_schema
dump.write_tables
dump.write_assets
dump.write_config
end
end
end
|
Instance Method Details
#assets_to_dump ⇒ Object
129
130
131
132
133
134
|
# File 'lib/dump/writer.rb', line 129
def assets_to_dump
Rake::Task['assets'].invoke
Dump::Env[:assets].split(Dump::Assets::SPLITTER)
rescue
[]
end
|
#create_file(name) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/dump/writer.rb', line 41
def create_file(name)
Tempfile.open('dump') do |temp|
yield(temp)
temp.open
stream.tar.add_file_simple(name, :mode => 0100444, :size => temp.length) do |f|
f.write(temp.read(4096)) until temp.eof?
end
end
end
|
#open ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/dump/writer.rb', line 27
def open
Pathname.new(path).dirname.mkpath
Zlib::GzipWriter.open(path) do |gzip|
gzip.mtime = Time.utc(2000)
lock do
Archive::Tar::Minitar.open(gzip, 'w') do |stream|
@stream = stream
@config = {:tables => {}}
yield(self)
end
end
end
end
|
#write_assets ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/dump/writer.rb', line 99
def write_assets
assets = assets_to_dump
return if assets.blank?
config[:assets] = {}
Dir.chdir(Dump.rails_root) do
assets = Dir[*assets].uniq
assets.with_progress('Assets') do |asset|
paths = Dir[File.join(asset, '**/*')]
files = paths.select{ |path| File.file?(path) }
config[:assets][asset] = {:total => paths.length, :files => files.length}
assets_root_link do |_tmpdir, prefix|
paths.with_progress(asset) do |entry|
begin
Archive::Tar::Minitar.pack_file(File.join(prefix, entry), stream)
rescue => e
$stderr.puts "Skipped asset due to error #{e}"
end
end
end
end
end
end
|
#write_config ⇒ Object
123
124
125
126
127
|
# File 'lib/dump/writer.rb', line 123
def write_config
create_file('config') do |f|
Marshal.dump(config, f)
end
end
|
#write_schema ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/dump/writer.rb', line 51
def write_schema
create_file('schema.rb') do |f|
Dump::Env.with_env('SCHEMA' => f.path) do
Rake::Task['db:schema:dump'].invoke
end
end
end
|
#write_table(table) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/dump/writer.rb', line 66
def write_table(table)
row_count = table_row_count(table)
config[:tables][table] = row_count
Progress.start(table, 1 + row_count) do
create_file("#{table}.dump") do |f|
columns = table_columns(table)
column_names = columns.map(&:name).sort
columns_by_name = columns.index_by(&:name)
Marshal.dump(column_names, f)
Progress.step
type_cast = if columns.first.respond_to?(:type_cast_from_database)
proc do |column_name, value|
columns_by_name[column_name].type_cast_from_database(value)
end
else
proc do |column_name, value|
columns_by_name[column_name].type_cast(value)
end
end
each_table_row(table, row_count) do |row|
values = column_names.map do |column_name|
type_cast.call(column_name, row[column_name])
end
Marshal.dump(values, f)
Progress.step
end
end
end
end
|
#write_tables ⇒ Object
59
60
61
62
63
64
|
# File 'lib/dump/writer.rb', line 59
def write_tables
verify_connection
tables_to_dump.with_progress('Tables') do |table|
write_table(table)
end
end
|