Class: Forklift::Connection::Mysql

Inherits:
Base::Connection show all
Defined in:
lib/forklift/transports/mysql.rb

Instance Method Summary collapse

Methods inherited from Base::Connection

#client, #exec, #exec!, #exec_ruby, #pipe

Constructor Details

#initialize(config, forklift) ⇒ Mysql

Returns a new instance of Mysql.



8
9
10
11
# File 'lib/forklift/transports/mysql.rb', line 8

def initialize(config, forklift)
  @config = config
  @forklift = forklift
end

Instance Method Details

#columns(table, database = current_database, return_types = false) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/forklift/transports/mysql.rb', line 220

def columns(table, database=current_database, return_types=false)
  cols = []
  types = []
  read("describe `#{database}`.`#{table}`").each do |row|
    cols  << row[:Field]
    types << row[:Type]
  end
  return cols if return_types == false
  return cols, types
end

#configObject



21
22
23
# File 'lib/forklift/transports/mysql.rb', line 21

def config
  @config
end

#connectObject



13
14
15
# File 'lib/forklift/transports/mysql.rb', line 13

def connect
  @client = Mysql2::Client.new(@config)
end

#count(table, database = current_database) ⇒ Object



204
205
206
# File 'lib/forklift/transports/mysql.rb', line 204

def count(table, database=current_database)
  read("select count(1) as \"count\" from `#{database}`.`#{table}`")[0][:count]
end

#current_databaseObject



200
201
202
# File 'lib/forklift/transports/mysql.rb', line 200

def current_database
  q("select database() as 'db'").first['db']
end

#default_matcherObject



29
30
31
# File 'lib/forklift/transports/mysql.rb', line 29

def default_matcher
  'updated_at'
end

#disconnectObject



17
18
19
# File 'lib/forklift/transports/mysql.rb', line 17

def disconnect
  @client.close
end

#drop!(table, database = current_database) ⇒ Object



33
34
35
# File 'lib/forklift/transports/mysql.rb', line 33

def drop!(table, database=current_database)
  q("DROP table `#{database}`.`#{table}`");
end

#dump(file, options = []) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/forklift/transports/mysql.rb', line 231

def dump(file, options=[])
  # example options: 
  # options.push '--max_allowed_packet=512M'
  # options.push '--set-gtid-purged=OFF'
  cmd = "mysqldump"
  cmd << " -u#{config[:username]}" unless config[:username].nil?
  cmd << " -p#{config[:password]}" unless config[:password].nil?
  options.each do |o|
    cmd << " #{o} "
  end
  cmd << " #{config[:database]}"
  cmd << " | gzip > #{file}"
  forklift.logger.log "Dumping #{config['database']} to #{file}"
  forklift.logger.debug cmd

  stdin, stdout, stderr = Open3.popen3(cmd)
  stdout = stdout.readlines
  stderr = stderr.readlines
  if stderr.length > 0
    raise "  > Dump error: #{stderr.join(" ")}"
  else
    forklift.logger.log "  > Dump complete"
  end
end

#exec_script(path) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/forklift/transports/mysql.rb', line 256

def exec_script(path)
  body = File.read(path)
  lines = body.split(';')
  lines.each do |line|
    line.strip!
    q(line) if line.length > 0
  end
end

#forkliftObject



25
26
27
# File 'lib/forklift/transports/mysql.rb', line 25

def forklift
  @forklift
end

#lazy_table_create(table, data, database = current_database, primary_key = 'id', matcher = default_matcher) ⇒ Object



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/forklift/transports/mysql.rb', line 128

def lazy_table_create(table, data, database=current_database, primary_key='id', matcher=default_matcher)
  keys = {}
  data.each do |item|
    item.each do |k,v|
      keys[k.to_s] = sql_type(v) if (keys[k.to_s].nil? || keys[k.to_s] == sql_type(nil))
    end
  end
  keys[primary_key] = 'bigint(20)' unless keys.has_key?(primary_key)

  col_defn = keys.map do |col, type|
    if col == primary_key
      "`#{col}` #{type} NOT NULL AUTO_INCREMENT"
    else
      "`#{col}` #{type} DEFAULT NULL"
    end
  end
  col_defn << "PRIMARY KEY (`#{primary_key}`)"
  col_defn << "KEY `#{matcher}` (`#{matcher}`)" if keys.include?(matcher.to_sym)

  command = <<-EOS
  CREATE TABLE `#{database}`.`#{table}` (
    #{col_defn.join(', ')}
  )
  EOS

  q(command)
  forklift.logger.log "lazy-created table `#{database}`.`#{table}`"
end

#max_timestamp(table, matcher = default_matcher, database = current_database) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/forklift/transports/mysql.rb', line 183

def max_timestamp(table, matcher=default_matcher, database=current_database)
  last_copied_row = read("select max(`#{matcher}`) as \"#{matcher}\" from `#{database}`.`#{table}`")[0]
  if ( last_copied_row.nil? || last_copied_row[matcher.to_sym].nil? )
    latest_timestamp = '1970-01-01 00:00'
  else
    return last_copied_row[matcher.to_sym].to_s(:db)
  end
end

#q(query, options = {}) ⇒ Object



265
266
267
268
# File 'lib/forklift/transports/mysql.rb', line 265

def q(query, options={})
  forklift.logger.debug "    SQL: #{query}"
  return client.query(query, options)
end

#read(query, database = current_database, looping = true, limit = forklift.config[:batch_size], offset = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/forklift/transports/mysql.rb', line 41

def read(query, database=current_database, looping=true, limit=forklift.config[:batch_size], offset=0)
  loop_count = 0
  # TODO: Detect limit/offset already present in query
  q("USE `#{database}`")

  while ( looping == true || loop_count == 0 )
    data = []
    prepared_query = query
    if prepared_query.downcase.include?("select") && !prepared_query.downcase.include?("limit")
      prepared_query = "#{prepared_query} LIMIT #{offset}, #{limit}"
    end
    response = q(prepared_query, symbolize_keys: true)
    response.each do |row|
      data << row
    end

    if block_given?
      yield data
    else
      return data
    end

    offset = offset + limit
    looping = false if data.length == 0
    loop_count = loop_count + 1
  end
end

#read_since(table, since, matcher = default_matcher, database = current_database) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/forklift/transports/mysql.rb', line 172

def read_since(table, since, matcher=default_matcher, database=current_database)
  query = "select * from `#{database}`.`#{table}` where `#{matcher}` >= '#{since}' order by `#{matcher}` asc"
  self.read(query, database){|data|
    if block_given?
      yield data
    else
      return data
    end
  }
end

#rename(table, new_table, database = current_database, new_database = current_database) ⇒ Object



37
38
39
# File 'lib/forklift/transports/mysql.rb', line 37

def rename(table, new_table, database=current_database, new_database=current_database)
  q("RENAME TABLE `#{database}`.`#{table}` TO `#{new_database}`.`#{new_table}`")
end

#sql_type(v) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/forklift/transports/mysql.rb', line 157

def sql_type(v)
  return "bigint(20)"   if v.class == Fixnum
  return "float"        if v.class == Float
  return "float"        if v.class == BigDecimal
  return "date"         if v.class == Date
  return "datetime"     if v.class == Time
  return "datetime"     if v.class == DateTime
  return "varchar(255)" if v.class == Symbol
  return "tinyint(1)"   if v.class == TrueClass
  return "tinyint(1)"   if v.class == FalseClass
  return "text"         if v.class == String
  return "varchar(0)"   if v.class == NilClass
  return "text"         # catchall
end

#tablesObject



192
193
194
195
196
197
198
# File 'lib/forklift/transports/mysql.rb', line 192

def tables
  t = []
  client.query("show tables").each do |row|
    t << row.values[0]
  end
  t
end

#truncate(table, database = current_database) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/forklift/transports/mysql.rb', line 212

def truncate(table, database=current_database)
  begin
    self.truncate!(table, database=current_database)
  rescue Exception => e
    forklift.logger.debug e
  end
end

#truncate!(table, database = current_database) ⇒ Object



208
209
210
# File 'lib/forklift/transports/mysql.rb', line 208

def truncate!(table, database=current_database)
  q("truncate table `#{database}`.`#{table}`")
end

#write(data, table, to_update = true, database = current_database, primary_key = 'id', lazy = true, crash_on_extral_col = false) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/forklift/transports/mysql.rb', line 69

def write(data, table, to_update=true, database=current_database, primary_key='id', lazy=true, crash_on_extral_col=false)
  data.map{|l| l.symbolize_keys! }

  if tables.include? table
    ensure_row_types(data, table, database)
  elsif(lazy == true && data.length > 0)
    lazy_table_create(table, data, database, primary_key)
  end

  if data.length > 0
    columns = columns(table, database)
    data.each do |d|

      if crash_on_extral_col == false
        d.each do |k,v|
          unless columns.include?(k.to_s)
            q("ALTER TABLE `#{database}`.`#{table}` ADD `#{k}` #{sql_type(v)}  NULL  DEFAULT NULL;")
            columns = columns(table, database)
          end
        end
      end
    end

    insert_q = "INSERT INTO `#{database}`.`#{table}` (#{safe_columns(columns)}) VALUES "
    delete_q = "DELETE FROM `#{database}`.`#{table}` WHERE `#{primary_key}` IN "
    delete_keys = []
    data.each do |d|
      if(to_update == true && !d[primary_key.to_sym].nil?)
        delete_keys << d[primary_key.to_sym]
      end
      insert_q << safe_values(columns, d)
      insert_q << ","
    end

    if delete_keys.length > 0
      delete_q << "(#{delete_keys.join(',')})"
      q(delete_q)
    end
    insert_q = insert_q[0...-1]

    begin
      q(insert_q)
    rescue Mysql2::Error => ex
      # UTF8 Safety.  Open a PR if you don't want UTF8 data...
      # https://github.com/taskrabbit/demoji
      raise ex unless ex.message.match /Incorrect string value:/
      safer_insert_q = ""
      for i in (0...insert_q.length)
        char = insert_q[i]
        char = '???' if char.ord > forklift.config[:char_bytecode_max]
        safer_insert_q << char
      end
      q(safer_insert_q)
    end

    forklift.logger.log "wrote #{data.length} rows to `#{database}`.`#{table}`"
  end
end