From 6d582cffa0e7a457a30e9234bbead3221b8a7831 Mon Sep 17 00:00:00 2001 From: Tomas Touceda Date: Fri, 18 Feb 2022 16:30:24 -0300 Subject: [PATCH] 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 --- changes/issue-4082-host-software-0 | 1 + server/datastore/mysql/software.go | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 changes/issue-4082-host-software-0 diff --git a/changes/issue-4082-host-software-0 b/changes/issue-4082-host-software-0 new file mode 100644 index 0000000000..d7b3fddf4c --- /dev/null +++ b/changes/issue-4082-host-software-0 @@ -0,0 +1 @@ +* Don't depend on lastInsertedId to gather the id of the new software diff --git a/server/datastore/mysql/software.go b/server/datastore/mysql/software.go index 9e33a83c32..72d06bfcc2 100644 --- a/server/datastore/mysql/software.go +++ b/server/datastore/mysql/software.go @@ -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(