add int id for scan overview and revoke the change in beego

This commit is contained in:
Tan Jiang 2017-07-09 12:37:08 +08:00
parent 43734bfb90
commit e1e975096c
5 changed files with 36 additions and 20 deletions

View File

@ -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 (

View File

@ -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);

View File

@ -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)
}

View File

@ -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"`

View File

@ -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