Guest User

Untitled

a guest
Apr 18th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2. require 'rubygems'
  3. require 'mysql'
  4. require 'guid'
  5.  
  6. # -- Begin Changeable vars
  7.  
  8. db_name = 'rhosync_development'
  9. db_user = 'root'
  10. db_pass = ''
  11. db_ip = 'localhost'
  12.  
  13. # The following IDs must match what's in the client adapter's config
  14. sources = Hash.new
  15. sources['SugarAccounts'] = 5066
  16. sources['SugarCalls'] = 1001
  17. sources['SugarCallsContacts'] = 48
  18. sources['SugarContacts'] = 46
  19. sources['SugarMeetings'] = 45
  20. sources['SugarMeetingsContacts'] = 2591
  21. sources['SugarOrderLines'] = 8422
  22. sources['SugarOrders'] = 47
  23. sources['SugarPrices'] = 4935
  24. sources['SugarProducts'] = 7317
  25. sources['SugarSalesHistory'] = 4945
  26.  
  27. # -- End Changeable vars
  28.  
  29. begin
  30. dbh = Mysql.real_connect(db_ip, db_user, db_pass, db_name)
  31. rescue Mysql::Error => e
  32. puts "Error code: #{e.errno}"
  33. puts "Error message: #{e.error}"
  34. puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
  35. exit
  36. end
  37.  
  38. query = 'SELECT COUNT(*) AS COUNT FROM object_values'
  39. res = dbh.query(query)
  40. count = 0
  41. res.each_hash { |row|
  42. count = row['COUNT']
  43. }
  44.  
  45. puts "Connection successful to #{db_name}@#{db_ip}. You currently have #{count} rows of object_values data. Select option from the menu:"
  46. puts "1) Create new client ID, populate client_maps and create client_info.txt/object_values.txt"
  47. puts "2) Quit"
  48.  
  49. #option = gets.chomp
  50.  
  51. case gets.chomp
  52. when "1" then begin
  53. # Get the user ID
  54. query = 'SELECT id, login FROM users ORDER BY id'
  55. res = dbh.query(query)
  56. users = Hash.new
  57. res.each_hash { |row|
  58. puts "ID: #{row['id']}, username: #{row['login']}"
  59. users[row['id']] = row['login']
  60. }
  61.  
  62. print "Select the user ID from the list above that you wish to generate client_maps for: "
  63. user_id = gets.chomp
  64. unless users.has_key?(user_id)
  65. puts "ID #{user_id} doesn't exist. Exiting..."
  66. exit
  67. end
  68. user = users[user_id]
  69. print "Generating ID for #{user} and then inserting into client_maps. Continue? "
  70. #puts "Warning: This will remove ALL client_maps for #{users[user_id]}. Continue?"
  71. exit unless gets.chomp =~ /^(y|yes)$/i
  72.  
  73. # Create new ID
  74. client_id = Guid.new
  75. db_operation = 'insert'
  76. token = Time.now.to_i
  77. dirty = 0
  78. ack_token = 1
  79.  
  80. # Iterate over object_values and insert into client_maps db and client_info/object_values files
  81. client_info = File.new('client_info.txt', 'w')
  82. object_values = File.new('object_values.txt', 'w')
  83.  
  84. client_info.puts "client_id\n"
  85. client_info.puts "#{client_id}"
  86. object_values.puts "id|source_id|attrib|object|value|attrib_type\n"
  87.  
  88. query = "SELECT o.id, s.name AS source_name, o.attrib, o.object, o.value, o.attrib_type FROM object_values o, sources s "
  89. query << "WHERE o.source_id = s.id AND o.user_id = #{user_id}"
  90. res = dbh.query(query)
  91. q = ''
  92. res.each_hash { |row|
  93. object_id = row['id']
  94. source_id = sources[row['source_name']]
  95. attrib = row['attrib']
  96. object = row['object']
  97. value = row['value']
  98. attrib_type = row['attrib_type']
  99.  
  100. q = "INSERT INTO client_maps SET client_id = '#{client_id}', object_value_id = '#{object_id}', "
  101. q << "db_operation = '#{db_operation}', token = '#{token}', dirty = '#{dirty}', ack_token = '#{ack_token}'"
  102.  
  103. begin
  104. dbh.query(q)
  105. rescue Mysql::Error => e
  106. p "Error code: #{e.errno}"
  107. p "Error message: #{e.error}"
  108. p "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
  109. puts "Query was #{q}"
  110. exit
  111. end
  112.  
  113. object_values.puts "#{object_id}|#{source_id}|#{attrib}|#{object}|#{value}|#{attrib_type}\n"
  114. }
  115. puts "Done. Copy file(s) to myapp/app/prepopulation/fixtures/ on the Mac responsible for compiling the application."
  116. end
  117. when "2" then exit
  118. end
Add Comment
Please, Sign In to add comment