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



138
139
140
141
142
143
# File 'lib/dump/writer.rb', line 138

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

#create_file(name) ⇒ Object



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

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
41
# File 'lib/dump/writer.rb', line 29

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_assetsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dump/writer.rb', line 108

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



132
133
134
135
136
# File 'lib/dump/writer.rb', line 132

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

#write_schemaObject



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

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



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
98
99
100
101
102
103
104
105
106
# File 'lib/dump/writer.rb', line 68

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 = case
      when columns.first.respond_to?(:type_cast_from_database)
        proc do |column_name, value|
          columns_by_name[column_name].type_cast_from_database(value)
        end
      when columns.first.respond_to?(:type_cast)
        proc do |column_name, value|
          columns_by_name[column_name].type_cast(value)
        end
      when connection.respond_to?(:lookup_cast_type_from_column)
        proc do |column_name, value|
          connection.lookup_cast_type_from_column(columns_by_name[column_name]).deserialize(value)
        end
      else
        fail 'Failed to determine the way to convert values from database input to the appropriate ruby type'
      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_tablesObject



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

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