Description: Bug when loading wrong history records
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 opennebula (5.4.0-1) unstable; urgency=low
 .
   * Imported from http://packages.qa.debian.org/o/opennebula.html
Author: OpenNebula Team <contact@opennebula.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: http://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- opennebula-5.4.0.orig/src/vm/VirtualMachine.cc
+++ opennebula-5.4.0/src/vm/VirtualMachine.cc
@@ -517,6 +517,8 @@ int VirtualMachine::select(SqlDB * db)
             History * hp;
 
             hp = new History(oid, i);
+            history_records[i] = hp;
+
             rc = hp->select(db);
 
             if ( rc != 0)
@@ -524,8 +526,6 @@ int VirtualMachine::select(SqlDB * db)
                 goto error_previous_history;
             }
 
-            history_records[i] = hp;
-
             if ( i == last_seq )
             {
                 history = hp;
