Class: Dump::Reader
Overview
Defined Under Namespace
Classes: Summary
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/reader.rb', line 12
def config
@config
end
|
#stream ⇒ Object
Returns the value of attribute stream.
12
13
14
|
# File 'lib/dump/reader.rb', line 12
def stream
@stream
end
|
Class Method Details
.restore(path) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/dump/reader.rb', line 14
def self.restore(path)
new(path).open do |dump|
dump.silence do
dump.read_config
dump.migrate_down
dump.read_schema
dump.read_tables
dump.read_assets
end
end
end
|
.summary(path, options = {}) ⇒ Object
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
|
# File 'lib/dump/reader.rb', line 51
def self.summary(path, options = {})
new(path).open do |dump|
dump.read_config
sum = Summary.new
tables = dump.config[:tables]
sum. 'Tables'
sum.data tables.sort.map{ |(table, rows)|
"#{table}: #{Summary.pluralize(rows, 'row')}"
}
assets = dump.config[:assets]
if assets.present?
sum. 'Assets'
sum.data assets.sort.map{ |entry|
if entry.is_a?(String)
entry
else
asset, paths = entry
if paths.is_a?(Hash)
"#{asset}: #{Summary.pluralize paths[:files], 'file'} (#{Summary.pluralize paths[:total], 'entry'} total)"
else
"#{asset}: #{Summary.pluralize paths, 'entry'}"
end
end
}
end
if options[:schema]
sum. 'Schema'
sum.data dump.schema.split("\n")
end
sum
end
end
|
Instance Method Details
#find_entry(name) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/dump/reader.rb', line 98
def find_entry(name)
stream.each do |entry|
if entry.full_name == name
return yield(entry)
end
end
end
|
#migrate_down ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/dump/reader.rb', line 127
def migrate_down
case
when Dump::Env.downcase(:migrate_down) == 'reset'
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
when !Dump::Env.no?(:migrate_down)
return unless avaliable_tables.include?('schema_migrations')
find_entry('schema_migrations.dump') do |entry|
migrated = table_rows('schema_migrations').map{ |row| row['version'] }
dump_migrations = []
Marshal.load(entry) dump_migrations << Marshal.load(entry).first until entry.eof?
migrate_down = (migrated - dump_migrations)
unless migrate_down.empty?
migrate_down.reverse.with_progress('Migrating down') do |version|
Dump::Env.with_env('VERSION' => version) do
Rake::Task['db:migrate:down'].tap do |task|
begin
task.invoke
rescue ActiveRecord::IrreversibleMigration
$stderr.puts "Irreversible migration: #{version}"
end
task.reenable
end
end
end
end
end
end
end
|
#open ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/dump/reader.rb', line 89
def open
Zlib::GzipReader.open(path) do |gzip|
Archive::Tar::Minitar.open(gzip, 'r') do |stream|
@stream = stream
yield(self)
end
end
end
|
#read_asset?(path, prefix) ⇒ Boolean
259
260
261
262
263
264
|
# File 'lib/dump/reader.rb', line 259
def read_asset?(path, prefix)
Dump::Env.filter(:restore_assets, Dump::Assets::SPLITTER).custom_pass? do |value|
File.fnmatch(File.join(prefix, value), path) ||
File.fnmatch(File.join(prefix, value, '**'), path)
end
end
|
#read_assets ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/dump/reader.rb', line 218
def read_assets
return if Dump::Env[:restore_assets] && Dump::Env[:restore_assets].empty?
return if config[:assets].blank?
assets = config[:assets]
if assets.is_a?(Hash)
assets_count = assets.values.sum{ |value| value.is_a?(Hash) ? value[:total] : value }
assets_paths = assets.keys
else
assets_count, assets_paths = nil, assets
end
if Dump::Env[:restore_assets]
assets_paths.each do |asset|
Dump::Assets.glob_asset_children(asset, '**/*').reverse.each do |child|
next unless read_asset?(child, Dump.rails_root)
case
when File.file?(child)
File.unlink(child)
when File.directory?(child)
begin
Dir.unlink(child)
rescue Errno::ENOTEMPTY
nil
end
end
end
end
else
Dump::Env.with_env(:assets => assets_paths.join(':')) do
Rake::Task['assets:delete'].invoke
end
end
read_assets_entries(assets_paths, assets_count) do |stream, root, entry, prefix|
if !Dump::Env[:restore_assets] || read_asset?(entry.full_name, prefix)
stream.(root, entry)
end
end
end
|
#read_assets_entries(_assets_paths, assets_count) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/dump/reader.rb', line 266
def read_assets_entries(_assets_paths, assets_count)
Progress.start('Assets', assets_count || 1) do
found_assets = false
find_entry('assets.tar') do |assets_tar|
def assets_tar.rewind
end
Archive::Tar::Minitar.open(assets_tar) do |inp|
inp.each do |entry|
yield inp, Dump.rails_root, entry, nil
Progress.step if assets_count
end
end
found_assets = true
end
unless found_assets
assets_root_link do |tmpdir, prefix|
stream.each do |entry|
if entry.full_name.starts_with?("#{prefix}/")
yield stream, tmpdir, entry, prefix
Progress.step if assets_count
end
end
end
end
end
end
|
#read_config ⇒ Object
123
124
125
|
# File 'lib/dump/reader.rb', line 123
def read_config
@config = Marshal.load(read_entry('config'))
end
|
#read_entry(name) ⇒ Object
107
108
109
110
111
|
# File 'lib/dump/reader.rb', line 107
def read_entry(name)
find_entry(name) do |entry|
return entry.read
end
end
|
#read_entry_to_file(name) ⇒ Object
113
114
115
116
117
118
119
120
121
|
# File 'lib/dump/reader.rb', line 113
def read_entry_to_file(name)
find_entry(name) do |entry|
Tempfile.open('dumper') do |temp|
temp.write(entry.read(4096)) until entry.eof?
temp.rewind
yield(temp)
end
end
end
|
#read_schema ⇒ Object
165
166
167
168
169
170
171
172
173
|
# File 'lib/dump/reader.rb', line 165
def read_schema
return unless restore_schema?
read_entry_to_file('schema.rb') do |f|
Dump::Env.with_env('SCHEMA' => f.path) do
Rake::Task['db:schema:load'].invoke
end
Rake::Task['db:schema:dump'].invoke
end
end
|
#read_table(table, rows_count) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/dump/reader.rb', line 189
def read_table(table, rows_count)
find_entry("#{table}.dump") do |entry|
table_sql = quote_table_name(table)
clear_table(table_sql)
columns = Marshal.load(entry)
columns_sql = columns_insert_sql(columns)
Progress.start(table, rows_count) do
until entry.eof?
rows_sql = []
1000.times do
rows_sql << values_insert_sql(Marshal.load(entry)) unless entry.eof?
end
begin
insert_into_table(table_sql, columns_sql, rows_sql)
Progress.step(rows_sql.length)
rescue
rows_sql.each do |row_sql|
insert_into_table(table_sql, columns_sql, row_sql)
Progress.step
end
end
end
end
fix_sequence!(table)
end
end
|
#read_tables ⇒ Object
179
180
181
182
183
184
185
186
187
|
# File 'lib/dump/reader.rb', line 179
def read_tables
return if Dump::Env[:restore_tables] && Dump::Env[:restore_tables].empty?
verify_connection
config[:tables].with_progress('Tables') do |table, rows|
if (restore_schema? && schema_tables.include?(table)) || Dump::Env.filter(:restore_tables).pass?(table)
read_table(table, rows)
end
end
end
|
#restore_schema? ⇒ Boolean
161
162
163
|
# File 'lib/dump/reader.rb', line 161
def restore_schema?
!Dump::Env.no?(:restore_schema)
end
|
#schema ⇒ Object
175
176
177
|
# File 'lib/dump/reader.rb', line 175
def schema
read_entry('schema.rb')
end
|