The way that parse looks for the index of the field it just added
is broken.  Instead, use LAST_INSERT_ID() to figure it out.

For example, let's say you have 2 'tests' with the subdir set to None/NULL
(which is true for special 'tests' like boot):  the first entry would be
added and then find_test is used to find the first entry with a certain
job index and a subdir.  Then the second boot occurs and the same thing
happens.  The problem is that you then get the test index of the first boot
since they both had the same job index and subdir.  Now, one could correctly
argue that we should have DB constraints to make these things impossible, but
this type of search is always going to be error prone.  The right solution is
to use LAST_INSERT_ID() which grabs the last autonumber ID put into the DB
for that _connection_.  Since parse is single threaded, we know that the
number returned will be correct.


Signed-off-by: Jeremy Orlow <jorlow@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@1237 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/db.py b/tko/db.py
index 2cf13ad..22d05b8 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -70,6 +70,11 @@
 		self.con.commit()
 
 
+	def get_last_autonumber_value(self):
+		self.cur.execute('SELECT LAST_INSERT_ID()', [])
+		return self.cur.fetchall()[0][0]
+
+
 	def select(self, fields, table, where, wherein={}, distinct = False,
 							group_by = None):
 		"""\
@@ -215,7 +220,7 @@
                                      'username': job.user,
 		                     'machine_idx':job.machine_idx},
                                      commit=commit)
-		job.index = self.find_job(tag)
+		job.index = self.get_last_autonumber_value()
 		for test in job.tests:
 			self.insert_test(job, test, commit=commit)
 
@@ -227,7 +232,7 @@
 			'status':self.status_idx[test.status],
 			'reason':test.reason, 'machine_idx':job.machine_idx }
 		self.insert('tests', data, commit=commit)
-		test_idx = self.find_test(job.index, test.subdir)
+		test_idx = self.get_last_autonumber_value()
 		data = { 'test_idx':test_idx }
 
 		for i in test.iterations:
@@ -264,7 +269,7 @@
 		              'machine_group' : group ,
 			      'owner' : job.machine_owner },
 		            commit=commit)
-		return self.lookup_machine(hostname)
+		return self.get_last_autonumber_value()
 
 
 	def lookup_machine(self, hostname):
@@ -295,7 +300,7 @@
 		             'printable':kernel.base},
 		            commit=commit)
 		# WARNING - incorrectly shoving base into printable here.
-		kver = self.lookup_kernel(kernel)
+		kver = self.get_last_autonumber_value()
 		for patch in kernel.patches:
 			self.insert_patch(kver, patch, commit=commit)
 		return kver