// UpsertVersion adds or updates a single version record in the database
UpsertVersion adds or updates a single version record in the database
// UpsertVersion adds or updates a single version record in the database
func UpsertVersion(db *gorm.DB, componentVersion models.ComponentVersion) (models.ComponentVersion, error) {
	releaseTimestamp, err := newTimestampFromStr(componentVersion.Version.Released)
	if err != nil {
		return models.ComponentVersion{}, err
	}
	js, err := json.Marshal(componentVersion.Version.Data)
	if err != nil {
		return models.ComponentVersion{}, err
	}
	queryVsn := versionsTable{
		ComponentName: componentVersion.Component.Name,
		Train:         componentVersion.Version.Train,
		Version:       componentVersion.Version.Version,
	}
	newVsn := versionsTable{
		ComponentName:    componentVersion.Component.Name,
		Train:            componentVersion.Version.Train,
		Version:          componentVersion.Version.Version,
		ReleaseTimestamp: releaseTimestamp,
		Data:             string(js),
	}
	tx := db.Begin()
	cvPtr, err := upsertVersion(tx, queryVsn, newVsn)
	if err != nil {
		rollbackDB := tx.Rollback()
		if rollbackDB.Error != nil {
			return models.ComponentVersion{}, txErr{op: "rollback", orig: err, err: rollbackDB.Error}
		}
		return models.ComponentVersion{}, err
	}
	commitDB := tx.Commit()
	if commitDB.Error != nil {
		log.Println("6")
		return models.ComponentVersion{}, txErr{op: "commit", orig: nil, err: commitDB.Error}
	}
	return *cvPtr, nil
}
func UpsertVersion ( db * gorm . DB , componentVersion models . ComponentVersion ) ( models . ComponentVersion , error ) { releaseTimestamp , err := newTimestampFromStr ( componentVersion . Version . Released ) if err != nil { return models . ComponentVersion { } , err } js , err := json . Marshal ( componentVersion . Version . Data ) if err != nil { return models . ComponentVersion { } , err } queryVsn := versionsTable { ComponentName : componentVersion . Component . Name , Train : componentVersion . Version . Train , Version : componentVersion . Version . Version , } newVsn := versionsTable { ComponentName : componentVersion . Component . Name , Train : componentVersion . Version . Train , Version : componentVersion . Version . Version , ReleaseTimestamp : releaseTimestamp , Data : string ( js ) , } tx := db . Begin ( ) cvPtr , err := upsertVersion ( tx , queryVsn , newVsn ) if err != nil { rollbackDB := tx . Rollback ( ) if rollbackDB . Error != nil { return models . ComponentVersion { } , txErr { op : " " , orig : err , err : rollbackDB . Error } } return models . ComponentVersion { } , err } commitDB := tx . Commit ( ) if commitDB . Error != nil { log . Println ( " " ) return models . ComponentVersion { } , txErr { op : " " , orig : nil , err : commitDB . Error } } return * cvPtr , nil }
UpsertVersion adds or updates a single version record in the database
UpsertVersion adds or updates a single version record in the database
// the query used to find the original version // the new version