From e1e975096cf0849bb0e72f0bc180aad672320a76 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Sun, 9 Jul 2017 12:37:08 +0800 Subject: [PATCH] add int id for scan overview and revoke the change in beego --- make/common/db/registry.sql | 4 +- make/common/db/registry_sqlite.sql | 6 ++- src/common/dao/scan_job.go | 40 ++++++++++++------- src/common/models/scan_job.go | 3 +- .../github.com/astaxie/beego/orm/orm.go | 3 +- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/make/common/db/registry.sql b/make/common/db/registry.sql index 259f3b5df..d3412223a 100644 --- a/make/common/db/registry.sql +++ b/make/common/db/registry.sql @@ -181,6 +181,7 @@ create table img_scan_job ( ); create table img_scan_overview ( + id int NOT NULL AUTO_INCREMENT, image_digest varchar(128) NOT NULL, scan_job_id int NOT NULL, /* 0 indicates none, the higher the number, the more severe the status */ @@ -191,7 +192,8 @@ create table img_scan_overview ( details_key varchar(128), creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, - PRIMARY KEY(image_digest) + PRIMARY KEY(id), + UNIQUE(image_digest) ); create table clair_vuln_timestamp ( diff --git a/make/common/db/registry_sqlite.sql b/make/common/db/registry_sqlite.sql index be18f8d69..257176788 100644 --- a/make/common/db/registry_sqlite.sql +++ b/make/common/db/registry_sqlite.sql @@ -172,7 +172,8 @@ create table img_scan_job ( ); create table img_scan_overview ( - image_digest varchar(128) PRIMARY KEY, + id INTEGER PRIMARY KEY, + image_digest varchar(128), scan_job_id int NOT NULL, /* 0 indicates none, the higher the number, the more severe the status */ severity int NOT NULL default 0, @@ -181,7 +182,8 @@ create table img_scan_overview ( /* primary key for querying details, in clair it should be the name of the "top layer" */ details_key varchar(128), creation_time timestamp default CURRENT_TIMESTAMP, - update_time timestamp default CURRENT_TIMESTAMP + update_time timestamp default CURRENT_TIMESTAMP, + UNIQUE(image_digest) ); CREATE INDEX policy ON replication_job (policy_id); diff --git a/src/common/dao/scan_job.go b/src/common/dao/scan_job.go index 216203313..099964953 100644 --- a/src/common/dao/scan_job.go +++ b/src/common/dao/scan_job.go @@ -95,6 +95,7 @@ func SetScanJobForImg(digest string, jobID int64) error { } if !created { rec.JobID = jobID + rec.UpdateTime = time.Now() n, err := o.Update(rec, "JobID", "UpdateTime") if n == 0 { return fmt.Errorf("Failed to set scan job for image with digest: %s, error: %v", digest, err) @@ -105,17 +106,18 @@ func SetScanJobForImg(digest string, jobID int64) error { // GetImgScanOverview returns the ImgScanOverview based on the digest. func GetImgScanOverview(digest string) (*models.ImgScanOverview, error) { - o := GetOrmer() - rec := &models.ImgScanOverview{ - Digest: digest, - } - err := o.Read(rec) - if err != nil && err != orm.ErrNoRows { + res := []*models.ImgScanOverview{} + _, err := scanOverviewQs().Filter("image_digest", digest).All(&res) + if err != nil { return nil, err } - if err == orm.ErrNoRows { + if len(res) == 0 { return nil, nil } + if len(res) > 1 { + return nil, fmt.Errorf("Found multiple scan_overview entries for digest: %s", digest) + } + rec := res[0] if len(rec.CompOverviewStr) > 0 { co := &models.ComponentsOverview{} if err := json.Unmarshal([]byte(rec.CompOverviewStr), co); err != nil { @@ -129,17 +131,22 @@ func GetImgScanOverview(digest string) (*models.ImgScanOverview, error) { // UpdateImgScanOverview updates the serverity and components status of a record in img_scan_overview func UpdateImgScanOverview(digest, detailsKey string, sev models.Severity, compOverview *models.ComponentsOverview) error { o := GetOrmer() + rec, err := GetImgScanOverview(digest) + if err != nil { + return fmt.Errorf("Failed to getting scan_overview record for update: %v", err) + } + if rec == nil { + return fmt.Errorf("No scan_overview record for digest: %s", digest) + } b, err := json.Marshal(compOverview) if err != nil { return err } - rec := &models.ImgScanOverview{ - Digest: digest, - Sev: int(sev), - CompOverviewStr: string(b), - DetailsKey: detailsKey, - UpdateTime: time.Now(), - } + rec.Sev = int(sev) + rec.CompOverviewStr = string(b) + rec.DetailsKey = detailsKey + rec.UpdateTime = time.Now() + n, err := o.Update(rec, "Sev", "CompOverviewStr", "DetailsKey", "UpdateTime") if n == 0 || err != nil { return fmt.Errorf("Failed to update scan overview record with digest: %s, error: %v", digest, err) @@ -154,3 +161,8 @@ func ListImgScanOverviews() ([]*models.ImgScanOverview, error) { _, err := o.QueryTable(models.ScanOverviewTable).All(&res) return res, err } + +func scanOverviewQs() orm.QuerySeter { + o := GetOrmer() + return o.QueryTable(models.ScanOverviewTable) +} diff --git a/src/common/models/scan_job.go b/src/common/models/scan_job.go index 51fffab28..0dc41a571 100644 --- a/src/common/models/scan_job.go +++ b/src/common/models/scan_job.go @@ -53,7 +53,8 @@ func (s *ScanJob) TableName() string { //ImgScanOverview mapped to a record of image scan overview. type ImgScanOverview struct { - Digest string `orm:"pk;column(image_digest)" json:"image_digest"` + ID int64 `orm:"pk;auto;column(id)" json:"-"` + Digest string `orm:"column(image_digest)" json:"image_digest"` Status string `orm:"-" json:"scan_status"` JobID int64 `orm:"column(scan_job_id)" json:"job_id"` Sev int `orm:"column(severity)" json:"severity"` diff --git a/src/vendor/github.com/astaxie/beego/orm/orm.go b/src/vendor/github.com/astaxie/beego/orm/orm.go index e389a9930..0ffb6b869 100644 --- a/src/vendor/github.com/astaxie/beego/orm/orm.go +++ b/src/vendor/github.com/astaxie/beego/orm/orm.go @@ -137,11 +137,10 @@ func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, i if err == ErrNoRows { // Create id, err := o.Insert(md) - fmt.Printf("id when create: %d", id) return (err == nil), id, err } - return false, 0, err + return false, ind.FieldByIndex(mi.fields.pk.fieldIndex).Int(), err } // insert model data to database