database: add missing transaction commits and close opened statement before inserting feature versions.

This commit is contained in:
Quentin Machu 2016-01-15 15:22:52 -05:00 committed by Jimmy Zelinskie
parent c5d1a8e5f7
commit 74fc5b3e66

View File

@ -84,6 +84,7 @@ func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion)
} }
if newOrExisting == "exi" { if newOrExisting == "exi" {
// That featureVersion already exists, return its id. // That featureVersion already exists, return its id.
tx.Commit()
return featureVersion.ID, nil return featureVersion.ID, nil
} }
@ -91,7 +92,7 @@ func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion)
// Vulnerability_Affects_FeatureVersion. // Vulnerability_Affects_FeatureVersion.
err = linkFeatureVersionToVulnerabilities(tx, featureVersion) err = linkFeatureVersionToVulnerabilities(tx, featureVersion)
if err != nil { if err != nil {
// tx.Rollback() is done in linkFeatureVersionToVulnerabilities. tx.Rollback()
return 0, err return 0, err
} }
@ -124,40 +125,49 @@ func (pgSQL *pgSQL) insertFeatureVersions(featureVersions []database.FeatureVers
return IDs, nil return IDs, nil
} }
type vulnerabilityAffectsFeatureVersion struct {
vulnerabilityID int
fixedInID int
fixedInVersion types.Version
}
func linkFeatureVersionToVulnerabilities(tx *sql.Tx, featureVersion database.FeatureVersion) error { func linkFeatureVersionToVulnerabilities(tx *sql.Tx, featureVersion database.FeatureVersion) error {
// Select every vulnerability and the fixed version that affect this Feature. // Select every vulnerability and the fixed version that affect this Feature.
// TODO(Quentin-M): LIMIT // TODO(Quentin-M): LIMIT
rows, err := tx.Query(getQuery("s_vulnerability_fixedin_feature"), featureVersion.Feature.ID) rows, err := tx.Query(getQuery("s_vulnerability_fixedin_feature"), featureVersion.Feature.ID)
if err != nil { if err != nil {
tx.Rollback()
return handleError("s_vulnerability_fixedin_feature", err) return handleError("s_vulnerability_fixedin_feature", err)
} }
defer rows.Close() defer rows.Close()
var fixedInID, vulnerabilityID int var affects []vulnerabilityAffectsFeatureVersion
var fixedInVersion types.Version
for rows.Next() { for rows.Next() {
err := rows.Scan(&fixedInID, &vulnerabilityID, &fixedInVersion) var affect vulnerabilityAffectsFeatureVersion
err := rows.Scan(&affect.fixedInID, &affect.vulnerabilityID, &affect.fixedInVersion)
if err != nil { if err != nil {
tx.Rollback()
return handleError("s_vulnerability_fixedin_feature.Scan()", err) return handleError("s_vulnerability_fixedin_feature.Scan()", err)
} }
if featureVersion.Version.Compare(fixedInVersion) < 0 { if featureVersion.Version.Compare(affect.fixedInVersion) < 0 {
// The version of the FeatureVersion we are inserting is lower than the fixed version on this // The version of the FeatureVersion we are inserting is lower than the fixed version on this
// Vulnerability, thus, this FeatureVersion is affected by it. // Vulnerability, thus, this FeatureVersion is affected by it.
_, err := tx.Exec(getQuery("i_vulnerability_affects_featureversion"), vulnerabilityID, affects = append(affects, affect)
featureVersion.ID, fixedInID)
if err != nil {
tx.Rollback()
return handleError("i_vulnerability_affects_featureversion", err)
}
} }
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
tx.Rollback()
return handleError("s_vulnerability_fixedin_feature.Rows()", err) return handleError("s_vulnerability_fixedin_feature.Rows()", err)
} }
rows.Close()
// Insert into Vulnerability_Affects_FeatureVersion.
for _, affect := range affects {
_, err := tx.Exec(getQuery("i_vulnerability_affects_featureversion"), affect.vulnerabilityID,
featureVersion.ID, affect.fixedInID)
if err != nil {
return handleError("i_vulnerability_affects_featureversion", err)
}
}
return nil return nil
} }