The reparse of existing job is done by a delete and then an insert, this
introduces a new job_idx/test_idx for the same job and voids the old ones,
so users won't be able to use job_idx/test_idx to refer to job/test as their
index might change.

This cl makes reparse to keep the same job_idx and test_idx for existing
jobs.

Also fixes a bug where user created test attributes are being deleted
inadvertently during reparse.

Signed-off-by: Jiqing Tang <jiqingtang@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@3992 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/db.py b/tko/db.py
index 4b3f82a..5303ff0 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -324,16 +324,20 @@
         if match:
             afe_job_id = match.group(1)
 
-        self.insert('jobs', {'tag':tag,
-                             'label': job.label,
-                             'username': job.user,
-                             'machine_idx': job.machine_idx,
-                             'queued_time': job.queued_time,
-                             'started_time': job.started_time,
-                             'finished_time': job.finished_time,
-                             'afe_job_id': afe_job_id},
-                             commit=commit)
-        job.index = self.get_last_autonumber_value()
+        data = {'tag':tag,
+                'label': job.label,
+                'username': job.user,
+                'machine_idx': job.machine_idx,
+                'queued_time': job.queued_time,
+                'started_time': job.started_time,
+                'finished_time': job.finished_time,
+                'afe_job_id': afe_job_id}
+        is_update = hasattr(job, 'index')
+        if is_update:
+            self.update('jobs', data, {'job_idx': job.index}, commit=commit)
+        else:
+            self.insert('jobs', data, commit=commit)
+            job.index = self.get_last_autonumber_value()
         for test in job.tests:
             self.insert_test(job, test, commit=commit)
 
@@ -353,6 +357,7 @@
             where = {'test_idx': test_idx}
             self.delete('iteration_result', where)
             self.delete('iteration_attributes', where)
+            where['user_created'] = 0
             self.delete('test_attributes', where)
         else:
             self.insert('tests', data, commit=commit)