lib/forklift/transports/mysql.rb in forklift_etl-1.2.1 vs lib/forklift/transports/mysql.rb in forklift_etl-1.2.2
- old
+ new
@@ -97,11 +97,11 @@
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]
@@ -127,26 +127,30 @@
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] = sql_type(v) if ( keys[k].nil? && v.class != NilClass )
+ 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)
- data.first.each do |k,v|
- keys[k] = sql_type(v) if ( keys[k].nil? )
+ 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 = "CREATE TABLE `#{database}`.`#{table}` ( "
- command << " `#{primary_key}` bigint(20) NOT NULL AUTO_INCREMENT, " if ( data.first[primary_key.to_sym].nil? )
- keys.each do |col, type|
- command << " `#{col}` #{type} DEFAULT NULL, "
- end
- command << " PRIMARY KEY (`#{primary_key}`) "
- command << " , KEY `#{matcher}` (`#{matcher}`) " if keys.include?(matcher.to_sym)
- command << " ) "
+ command = <<-EOS
+ CREATE TABLE `#{database}`.`#{table}` (
+ #{col_defn.join(', ')}
+ )
+ EOS
q(command)
forklift.logger.log "lazy-created table `#{database}`.`#{table}`"
end
@@ -236,11 +240,11 @@
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(" ")}"
@@ -283,10 +287,10 @@
forklift.logger.log alter_sql
q(alter_sql)
end
end
- end
+ end
end
def safe_columns(cols)
a = []
cols.each do |c|