Skip to content

Commit 0c05858

Browse files
author
Keerthan Mala
committed
fix the query for the latest versions and create cluster checkins table
1 parent 4736fb5 commit 0c05858

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

pkg/data/clusters_checkins_table.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const (
1919

2020
// ClustersCheckinsTable type that expresses the `clusters_checkins` postgres table schema
2121
type clustersCheckinsTable struct {
22-
CheckinID string `gorm:"primary_key;type:bigserial;column_name:checkins_id"`
23-
ClusterID string `gorm:"type:uuid;column_name:cluster_id;index"`
24-
CreatedAt string `gorm:"type:timestamp;column_name:created_at;index"`
25-
Data types.JSONText `gorm:"type:json;column_name:data"`
22+
CheckinsID string `gorm:"primary_key;type:bigserial;column_name:checkins_id"`
23+
ClusterID string `gorm:"type:uuid;column_name:cluster_id;index"`
24+
CreatedAt string `gorm:"type:timestamp;column_name:created_at;index"`
25+
Data types.JSONText `gorm:"type:json;column_name:data"`
2626
}
2727

2828
func newClustersCheckinsTable(checkinID, clusterID string, createdAt time.Time, clusterData []byte) clustersCheckinsTable {
2929
return clustersCheckinsTable{
30-
CheckinID: checkinID,
31-
ClusterID: clusterID,
32-
CreatedAt: Timestamp{Time: createdAt}.String(),
33-
Data: types.JSONText(clusterData),
30+
CheckinsID: checkinID,
31+
ClusterID: clusterID,
32+
CreatedAt: Timestamp{Time: createdAt}.String(),
33+
Data: types.JSONText(clusterData),
3434
}
3535
}
3636

pkg/data/version.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,12 @@ func GetLatestVersions(db *gorm.DB, ct []ComponentAndTrain) ([]*models.Component
9191
listedTrains[c.Train] = struct{}{}
9292
}
9393
}
94-
95-
rows, err := db.
96-
Table("versions").
97-
Select("*, MAX(release_timestamp)").
98-
Where("component_name IN (?) AND train IN (?)", componentsList, trainsList).
99-
Group("component_name, train").
94+
rows, err := db.Raw("select * from versions as ver where ver.component_name IN (?) AND ver.train IN (?) AND release_timestamp = (select MAX(release_timestamp) from versions as ver1 where ver1.component_name = ver.component_name AND ver1.train = ver.train)", componentsList, trainsList).
10095
Rows()
10196
if err != nil {
10297
return nil, err
10398
}
99+
defer rows.Close()
104100
rowsResult := []versionsTable{}
105101
for rows.Next() {
106102
var row versionsTable
@@ -114,7 +110,6 @@ func GetLatestVersions(db *gorm.DB, ct []ComponentAndTrain) ([]*models.Component
114110
&row.Version,
115111
&row.ReleaseTimestamp,
116112
&row.Data,
117-
&sql.NullString{},
118113
); err != nil {
119114
return nil, err
120115
}

pkg/data/version_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestGetLatestVersions(t *testing.T) {
114114

115115
//specify a different version for each component version in this name/train
116116
cv.Version.Version = fmt.Sprintf("version%d-%d", idx, n)
117-
cvReleaseTime := time.Now().Add(time.Duration(idx*(n+1)) * time.Hour)
117+
cvReleaseTime := time.Now().Add(time.Duration((idx+1)*(n+1)) * time.Hour)
118118
cv.Version.Released = cvReleaseTime.Format(released)
119119

120120
// record the latest release time for each component

pkg/handlers/get_latest_versions.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handlers
22

33
import (
4+
"log"
45
"net/http"
56

67
"github.com/deis/workflow-manager-api/pkg/data"
@@ -55,6 +56,7 @@ func GetLatestVersions(params operations.GetComponentsByLatestReleaseParams, db
5556

5657
componentVersions, err := data.GetLatestVersions(db, componentAndTrainSlice)
5758
if err != nil {
59+
log.Printf("data.GetLatestVersions error (%s)", err)
5860
return operations.NewGetComponentsByLatestReleaseDefault(http.StatusInternalServerError).WithPayload(&models.Error{Code: http.StatusInternalServerError, Message: "database error"})
5961
}
6062
ret := operations.GetComponentsByLatestReleaseOKBodyBody{Data: componentVersions}

pkg/swagger/restapi/configure_workflow_manager.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func configureFlags(api *operations.WorkflowManagerAPI) {
4545
func configureAPI(api *operations.WorkflowManagerAPI) http.Handler {
4646

4747
rdsDB := getDb(api)
48+
rdsDB.LogMode(true)
4849
// configure the api here
4950
api.ServeError = errors.ServeError
5051

0 commit comments

Comments
 (0)