rhomobile (owner)

Revisions

gist: 213494 Download_button fork
public
Description:
Imports a text file into a rhodes sqlite3 database
Public Clone URL: git://gist.github.com/213494.git
Embed All Files: show embed
generate.rb #
1
2
3
4
5
6
7
8
9
10
11
12
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
38
39
40
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
68
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
#!/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