Don't depend on last inserted id to get the id for the new software (#4298)

* Don't depend on last inserted id to get the id for the new software

* Correct typo
This commit is contained in:
Tomas Touceda 2022-02-18 16:30:24 -03:00 committed by GitHub
parent c641e39ef4
commit 6d582cffa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -0,0 +1 @@
* Don't depend on lastInsertedId to gather the id of the new software

View File

@ -186,7 +186,7 @@ func getOrGenerateSoftwareIdDB(ctx context.Context, tx sqlx.ExtContext, s fleet.
return uint(existingId[0]), nil
}
result, err := tx.ExecContext(ctx,
_, err := tx.ExecContext(ctx,
"INSERT INTO software "+
"(name, version, source, `release`, vendor, arch, bundle_identifier) "+
"VALUES (?, ?, ?, ?, ?, ?, ?) "+
@ -196,11 +196,9 @@ func getOrGenerateSoftwareIdDB(ctx context.Context, tx sqlx.ExtContext, s fleet.
if err != nil {
return 0, ctxerr.Wrap(ctx, err, "insert software")
}
id, err := result.LastInsertId()
if err != nil {
return 0, ctxerr.Wrap(ctx, err, "last id from software")
}
return uint(id), nil
// LastInsertId sometimes returns 0 as it's dependent on connections and how mysql is configured
// doing the select recursively is a bit slower, but most times, we won't end up in this situation
return getOrGenerateSoftwareIdDB(ctx, tx, s)
}
func insertNewInstalledHostSoftwareDB(