Class: Myreplicator::MysqlExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/exporter/mysql_exporter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MysqlExporter

Returns a new instance of MysqlExporter.



4
5
6
# File 'lib/exporter/mysql_exporter.rb', line 4

def initialize *args
  options = args.extract_options!
end

Class Method Details

.compare_datatypes(index, vertica_schema, mysql_schema) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/exporter/mysql_exporter.rb', line 183

def self.compare_datatypes index, vertica_schema, mysql_schema
  type = Myreplicator::VerticaTypes.convert mysql_schema[index]["data_type"], mysql_schema[index]["column_type"]
  if vertica_schema.rows[index][:data_type].downcase != type.downcase
    if !(vertica_schema.rows[index][:data_type].include?("timestamp")) && 
      !(vertica_schema.rows[index][:data_type].include?("decimal")) && 
      !(vertica_schema.rows[index][:data_type].include?("numeric")) &&
      !(vertica_schema.rows[index][:data_type].include?("binary"))
      return true
    end
    return false
  end
  return false
end

.compare_schemas(vertica_schema, mysql_schema) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/exporter/mysql_exporter.rb', line 157

def self.compare_schemas vertica_schema, mysql_schema
  #Kernel.p vertica_schema
  #Kernel.p mysql_schema
  if vertica_schema.size != mysql_schema.size
    return true
  else
    index = 0
    while index < vertica_schema.size
      # check for column name
      if vertica_schema.rows[index][:column_name] != mysql_schema[index]["column_name"]
        puts "diff"
        return true
      end
  
      # check for column's data type
      if compare_datatypes index, vertica_schema, mysql_schema
        puts "diff #{index}"
        return true
      end
      # and others ?? (PRIMARY, DEFAULT NULL, etc.)
      index += 1
    end
  end
  return false      
end

.get_mysql_schema_rows(mysql_schema) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/exporter/mysql_exporter.rb', line 197

def self.get_mysql_schema_rows mysql_schema 
  mysql_schema_simple_form = []
  mysql_schema.each(:as => :hash) do |row|
    mysql_schema_simple_form << row
  end
  return mysql_schema_simple_form
end

.schema_changed?(options) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/exporter/mysql_exporter.rb', line 205

def self.schema_changed? options
  #Kernel.p "===== schema_changed? ====="
  #puts options
  mysql_schema = Loader.mysql_table_definition(options)
  vertica_schema = VerticaLoader.destination_table_vertica(options)

  # empty result set from vertica means table does not exist 
  unless vertica_schema.size > 0 
    return {:changed => true, :mysql_schema => mysql_schema, :new => true}
  end
  # compare two schemas
  
  
  mysql_schema_2 = get_mysql_schema_rows mysql_schema
  if compare_schemas(vertica_schema, mysql_schema_2)
    result =  {:changed => true, :mysql_schema => mysql_schema, :vertica_schema => vertica_schema,:new => false}
  else
    result =  {:changed => false, :mysql_schema => mysql_schema}
  end
  #Kernel.p result
  return result
end

Instance Method Details

#check_result(result, size) ⇒ Object

Checks the returned resut from SSH CMD Size specifies if there should be any returned results or not



232
233
234
235
236
# File 'lib/exporter/mysql_exporter.rb', line 232

def check_result result, size
  unless result.nil?
    raise Exceptions::ExportError.new("Export Error\n#{result}") if result.length > 0
  end     
end

#execute_export(cmd, metadata) ⇒ Object

Executes export command via ssh on the source DB Updates/interacts with the metadata object



242
243
244
245
246
247
248
249
250
# File 'lib/exporter/mysql_exporter.rb', line 242

def execute_export cmd, 
  .store!
  result = ""

  # Execute Export command on the source DB server
  result = .ssh.exec!(cmd)
  
  return result
end

#export_table(export_obj) ⇒ Object

Gets an Export object and dumps the data Initially using mysqldump Incrementally using mysql -e afterwards



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
# File 'lib/exporter/mysql_exporter.rb', line 13

def export_table export_obj
  @export_obj = export_obj

  ExportMetadata.record(:table => @export_obj.table_name,
                        :database => @export_obj.source_schema,
                        :export_to => load_to,
                        :export_id => @export_obj.id,
                        :filepath => filepath,
                        :store_in => @export_obj.s3_path,
                        :incremental_col => @export_obj.incremental_column) do ||

    prepare 

    if @export_obj.export_type? == :new && load_to == "mysql"
      on_failure_state_trans(, "new") # If failed, go back to new
      on_export_success()
      initial_export 
    elsif @export_obj.export_type? == :incremental || load_to == "vertica"
      on_failure_state_trans(, "failed") # Set state trans on failure
      on_export_success()
      incremental_export_into_outfile 
    end
    
  end # metadata
end

#exporting_state_transObject



280
281
282
283
284
# File 'lib/exporter/mysql_exporter.rb', line 280

def exporting_state_trans
  update_export(:state => "exporting", 
                :export_started_at => Time.now, 
                :exporter_pid => Process.pid)
end

#filepathObject

File path on remote server



63
64
65
# File 'lib/exporter/mysql_exporter.rb', line 63

def filepath
  File.join(Myreplicator.configs[@export_obj.source_schema]["ssh_tmp_dir"], @export_obj.filename)
end

#incremental_export_into_outfile(metadata) ⇒ Object

Exports table incrementally, similar to incremental_export method Dumps file in tmp directory specified in myreplicator.yml Note that directory needs 777 permissions for mysql to be able to export the file Uses \0 as the delimiter and new line for lines



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'lib/exporter/mysql_exporter.rb', line 110

def incremental_export_into_outfile 
  unless @export_obj.is_running?
    
    if @export_obj.export_type == "incremental"
      max_value = @export_obj.max_value
      .export_type = "incremental"        
      @export_obj.update_max_val if @export_obj.max_incremental_value.blank?   
    end

    options = {
      :db => @export_obj.source_schema,
      :source_schema => @export_obj.source_schema,
      :table => @export_obj.table_name,
      :filepath => filepath,
      :destination_schema => @export_obj.destination_schema,
      :enclosed_by => Myreplicator.configs[@export_obj.source_schema]["enclosed_by"]}

    schema_status = Myreplicator::MysqlExporter.schema_changed?(options)
    Kernel.p "===== schema_status ====="
    Kernel.p schema_status
    if schema_status[:changed] # && new?
      .export_type = "initial"
    else
      options[:incremental_col] = @export_obj.incremental_column
      options[:incremental_col_type] = @export_obj.incremental_column_type
      #options[:incremental_val] = @export_obj.max_incremental_value
      options[:export_type] = @export_obj.export_type
      options[:incremental_val] = [@export_obj.destination_max_incremental_value, @export_obj.max_incremental_value].min
    end

    #Kernel.p "===== incremental_export_into_outfile OPTIONS ====="
    #Kernel.p options
    cmd = SqlCommands.mysql_export_outfile(options)      
    exporting_state_trans
    puts "Exporting..."
    result = execute_export(cmd, )
    check_result(result, 0)

    if @export_obj.export_type == "incremental"
      .incremental_val = max_value # store max val in metadata
      @export_obj.update_max_val(max_value) # update max value if export was successful
    end
  end

  return false
end

#initial_export(metadata) ⇒ Object

Exports Table using mysqldump. This method is invoked only once. Dumps with create options, no need to create table manaully



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/exporter/mysql_exporter.rb', line 72

def initial_export 
  .export_type = "initial"
  max_value = @export_obj.max_value if @export_obj.incremental_export?
  cmd = initial_mysqldump_cmd
  exporting_state_trans # mark exporting

  puts "Exporting..."
  result = execute_export(cmd, )
  check_result(result, 0)

  @export_obj.update_max_val(max_value) if @export_obj.incremental_export?
end

#initial_mysqldump_cmdObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/exporter/mysql_exporter.rb', line 85

def initial_mysqldump_cmd
  flags = ["create-options", "single-transaction"]       
  cmd = ""
  # Mysql - Mysql Export
  if @export_obj.export_to == "destination_db"
    cmd = SqlCommands.mysqldump(:db => @export_obj.source_schema,
                                :flags => flags,
                                :filepath => filepath,
                                :table_name => @export_obj.table_name)     
  else # Other destinations
    cmd = SqlCommands.mysql_export_outfile(:db => @export_obj.source_schema,
                                           :filepath => filepath,
                                           :table => @export_obj.table_name)
  end
  
  return cmd
end

#load_toObject



39
40
41
42
43
44
45
# File 'lib/exporter/mysql_exporter.rb', line 39

def load_to
  if @export_obj.export_to == "vertica"
    return "vertica"
  else
    return "mysql"
  end
end

#on_export_success(metadata) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/exporter/mysql_exporter.rb', line 286

def on_export_success 
  .on_success do |m|
    zipfile()
    update_export(:state => "export_completed", 
                  :export_finished_at => Time.now, 
                  :error => .error)
    .state = "export_completed"
  end
end

#on_failure_state_trans(metadata, state) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/exporter/mysql_exporter.rb', line 271

def on_failure_state_trans , state
  .on_failure do |m|
    update_export(:state => state, 
                  :export_finished_at => Time.now, 
                  :error => .error)
    raise Exceptions::ExportError.new(.error)
  end
end

#prepare(metadata) ⇒ Object

Setups SSH connection to remote host



50
51
52
53
# File 'lib/exporter/mysql_exporter.rb', line 50

def prepare 
  ssh = @export_obj.ssh_to_source
  .ssh = ssh
end

#update_export(*args) ⇒ Object



55
56
57
58
# File 'lib/exporter/mysql_exporter.rb', line 55

def update_export *args
  options = args.extract_options!
  @export_obj.update_attributes! options
end

#zipfile(metadata) ⇒ Object

zips the file on the source DB server



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/exporter/mysql_exporter.rb', line 255

def zipfile 
  cmd = "cd #{Myreplicator.configs[@export_obj.source_schema]["ssh_tmp_dir"]}; gzip #{@export_obj.filename}"

  puts cmd

  zip_result = .ssh.exec!(cmd)

  unless zip_result.nil?        
    raise Exceptions::ExportError.new("Export Error\n#{zip_result}") if zip_result.length > 0
  end

  .zipped = true

  return zip_result
end