#!/usr/local/bin/ruby
require 'rubygems'
require 'mysql'
require 'guid'
# -- Begin Changeable vars
db_name = 'rhosync_development'
db_user = 'root'
db_pass = ''
db_ip = 'localhost'
# The following IDs must match what's in the client adapter's config
sources = Hash.new
sources['SugarAccounts'] = 5066
sources['SugarCalls'] = 1001
sources['SugarCallsContacts'] = 48
sources['SugarContacts'] = 46
sources['SugarMeetings'] = 45
sources['SugarMeetingsContacts'] = 2591
sources['SugarOrderLines'] = 8422
sources['SugarOrders'] = 47
sources['SugarPrices'] = 4935
sources['SugarProducts'] = 7317
sources['SugarSalesHistory'] = 4945
# -- End Changeable vars
begin
dbh = Mysql.real_connect(db_ip, db_user, db_pass, db_name)
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
exit
end
query = 'SELECT COUNT(*) AS COUNT FROM object_values'
res = dbh.query(query)
count = 0
res.each_hash { |row|
count = row['COUNT']
}
puts "Connection successful to #{db_name}@#{db_ip}. You currently have #{count} rows of object_values data. Select option from the menu:"
puts "1) Create new client ID, populate client_maps and create client_info.txt/object_values.txt"
puts "2) Quit"
#option = gets.chomp
case gets.chomp
when "1" then begin
# Get the user ID
query = 'SELECT id, login FROM users ORDER BY id'
res = dbh.query(query)
users = Hash.new
res.each_hash { |row|
puts "ID: #{row['id']}, username: #{row['login']}"
users[row['id']] = row['login']
}
print "Select the user ID from the list above that you wish to generate client_maps for: "
user_id = gets.chomp
unless users.has_key?(user_id)
puts "ID #{user_id} doesn't exist. Exiting..."
exit
end
user = users[user_id]
print "Generating ID for #{user} and then inserting into client_maps. Continue? "
#puts "Warning: This will remove ALL client_maps for #{users[user_id]}. Continue?"
exit unless gets.chomp =~ /^(y|yes)$/i
# Create new ID
client_id = Guid.new
db_operation = 'insert'
token = Time.now.to_i
dirty = 0
ack_token = 1
# Iterate over object_values and insert into client_maps db and client_info/object_values files
client_info = File.new('client_info.txt', 'w')
object_values = File.new('object_values.txt', 'w')
client_info.puts "client_id\n"
client_info.puts "#{client_id}"
object_values.puts "id|source_id|attrib|object|value|attrib_type\n"
query = "SELECT o.id, s.name AS source_name, o.attrib, o.object, o.value, o.attrib_type FROM object_values o, sources s "
query << "WHERE o.source_id = s.id AND o.user_id = #{user_id}"
res = dbh.query(query)
q = ''
res.each_hash { |row|
object_id = row['id']
source_id = sources[row['source_name']]
attrib = row['attrib']
object = row['object']
value = row['value']
attrib_type = row['attrib_type']
q = "INSERT INTO client_maps SET client_id = '#{client_id}', object_value_id = '#{object_id}', "
q << "db_operation = '#{db_operation}', token = '#{token}', dirty = '#{dirty}', ack_token = '#{ack_token}'"
begin
dbh.query(q)
rescue Mysql::Error => e
p "Error code: #{e.errno}"
p "Error message: #{e.error}"
p "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
puts "Query was #{q}"
exit
end
object_values.puts "#{object_id}|#{source_id}|#{attrib}|#{object}|#{value}|#{attrib_type}\n"
}
puts "Done. Copy file(s) to myapp/app/prepopulation/fixtures/ on the Mac responsible for compiling the application."
end
when "2" then exit
end