Class: Myreplicator::MysqlExporter
- Inherits:
-
Object
- Object
- Myreplicator::MysqlExporter
- Defined in:
- lib/exporter/mysql_exporter.rb
Class Method Summary collapse
- .compare_datatypes(index, vertica_schema, mysql_schema) ⇒ Object
- .compare_schemas(vertica_schema, mysql_schema) ⇒ Object
- .get_mysql_schema_rows(mysql_schema) ⇒ Object
- .schema_changed?(options) ⇒ Boolean
Instance Method Summary collapse
-
#check_result(result, size) ⇒ Object
Checks the returned resut from SSH CMD Size specifies if there should be any returned results or not.
-
#execute_export(cmd, metadata) ⇒ Object
Executes export command via ssh on the source DB Updates/interacts with the metadata object.
-
#export_table(export_obj) ⇒ Object
Gets an Export object and dumps the data Initially using mysqldump Incrementally using mysql -e afterwards.
- #exporting_state_trans ⇒ Object
-
#filepath ⇒ Object
File path on remote server.
-
#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.
-
#initial_export(metadata) ⇒ Object
Exports Table using mysqldump.
- #initial_mysqldump_cmd ⇒ Object
-
#initialize(*args) ⇒ MysqlExporter
constructor
A new instance of MysqlExporter.
- #load_to ⇒ Object
- #on_export_success(metadata) ⇒ Object
- #on_failure_state_trans(metadata, state) ⇒ Object
-
#prepare(metadata) ⇒ Object
Setups SSH connection to remote host.
- #update_export(*args) ⇒ Object
-
#zipfile(metadata) ⇒ Object
zips the file on the source DB server.
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 = args. end |
Class Method Details
.compare_datatypes(index, vertica_schema, mysql_schema) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/exporter/mysql_exporter.rb', line 190 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/exporter/mysql_exporter.rb', line 164 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
204 205 206 207 208 209 210 |
# File 'lib/exporter/mysql_exporter.rb', line 204 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/exporter/mysql_exporter.rb', line 212 def self.schema_changed? #Kernel.p "===== schema_changed? =====" #puts options mysql_schema = Loader.mysql_table_definition() vertica_schema = VerticaLoader.destination_table_vertica() # 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
239 240 241 242 243 |
# File 'lib/exporter/mysql_exporter.rb', line 239 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
249 250 251 252 253 254 255 256 257 |
# File 'lib/exporter/mysql_exporter.rb', line 249 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") || 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_trans ⇒ Object
292 293 294 295 296 |
# File 'lib/exporter/mysql_exporter.rb', line 292 def exporting_state_trans update_export(:state => "exporting", :export_started_at => Time.now, :exporter_pid => Process.pid) end |
#filepath ⇒ Object
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 156 157 158 159 160 161 162 |
# 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 if (@export_obj.export_type == "all" && @export_obj.export_to == "vertica") .export_type = "incremental" end = { :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"], :export_id => @export_obj.id } schema_status = Myreplicator::MysqlExporter.schema_changed?() Kernel.p "===== schema_status =====" Kernel.p schema_status if schema_status[:changed] # && new? .export_type = "initial" else [:incremental_col] = @export_obj.incremental_column [:incremental_col_type] = @export_obj.incremental_column_type [:export_type] = @export_obj.export_type [:incremental_val] = [@export_obj.destination_max_incremental_value, @export_obj.max_incremental_value].min #options[:incremental_val] = @export_obj.max_incremental_value end #Kernel.p "===== incremental_export_into_outfile OPTIONS =====" #Kernel.p options cmd = SqlCommands.mysql_export_outfile() #Kernel.p "===== incremental_export_into_outfile CMD =====" #puts cmd 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_cmd ⇒ Object
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 puts cmd return cmd end |
#load_to ⇒ Object
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
298 299 300 301 302 303 304 305 306 |
# File 'lib/exporter/mysql_exporter.rb', line 298 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
278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/exporter/mysql_exporter.rb', line 278 def on_failure_state_trans , state .on_failure do |m| update_export(:state => state, :export_finished_at => Time.now, :error => .error) begin Myreplicator::Loader.cleanup rescue Exception => e puts e. end 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 = args. @export_obj.update_attributes! end |
#zipfile(metadata) ⇒ Object
zips the file on the source DB server
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/exporter/mysql_exporter.rb', line 262 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 |