Class: Dump::Writer

Inherits:
Snapshot show all
Defined in:
lib/dump/writer.rb

Overview

Creating dump

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

Methods included from Snapshot::CleanNParse

#clean_description, #clean_str, #clean_tag, #clean_tags, #get_filter_tags

Constructor Details

This class inherits a constructor from Dump::Snapshot

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/dump/writer.rb', line 14

def config
  @config
end

#streamObject (readonly)

Returns the value of attribute stream.



14
15
16
# File 'lib/dump/writer.rb', line 14

def stream
  @stream
end

Class Method Details

.create(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dump/writer.rb', line 16

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_dumpObject



120
121
122
123
124
125
# File 'lib/dump/writer.rb', line 120

def assets_to_dump
  Rake::Task['assets'].invoke
  Dump::Env[:assets].split(Dump::Assets::SPLITTER)
rescue
  []
end

#create_file(name) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/dump/writer.rb', line 42

def create_file(name)
  Tempfile.open('dump') do |temp|
    yield(temp)
    temp.open
    stream.tar.add_file_simple(name, :mode => 0o100444, :size => temp.length) do |f|
      f.write(temp.read(4096)) until temp.eof?
    end
  end
end

#openObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dump/writer.rb', line 29

def open
  Pathname.new(path).dirname.mkpath
  gzip = Zlib::GzipWriter.open(path)
  gzip.mtime = Time.utc(2000)
  lock do
    Archive::Tar::Minitar.open(gzip, 'w') do |stream|
      @stream = stream
      @config = {:tables => {}}
      yield(self)
    end
  end
end

#write_assetsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dump/writer.rb', line 90

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_configObject



114
115
116
117
118
# File 'lib/dump/writer.rb', line 114

def write_config
  create_file('config') do |f|
    Marshal.dump(config, f)
  end
end

#write_schemaObject



52
53
54
55
56
57
58
# File 'lib/dump/writer.rb', line 52

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dump/writer.rb', line 67

def write_table(table)
  row_count = table_row_count(table)
  config[:tables][table] = row_count
  Progress.start(table, row_count + 1) do
    create_file("#{table}.dump") do |f|
      columns = table_columns(table)
      column_names = columns.map(&:name).sort
      type_cast = type_caster(columns)

      Marshal.dump(column_names, f)
      Progress.step

      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_tablesObject



60
61
62
63
64
65
# File 'lib/dump/writer.rb', line 60

def write_tables
  verify_connection
  tables_to_dump.with_progress('Tables') do |table|
    write_table(table)
  end
end