fix(be): refs for boltdb

This commit is contained in:
Denis Gukov 2022-02-03 14:19:35 +05:00
parent 05dd7c5653
commit 02a631fbd6
4 changed files with 79 additions and 23 deletions

View File

@ -511,32 +511,71 @@ func (d *BoltDb) getObjectRefs(projectID int, objectProps db.ObjectProps, object
return return
} }
//refs.Schedules, err = d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps) templates, err := d.getObjectRefsFrom(projectID, objectProps, intObjectID(objectID), db.ScheduleProps)
for _, st := range templates {
exists := false
for _, tpl := range refs.Templates {
if tpl.ID == st.ID {
exists = true
break
}
}
if exists {
continue
}
refs.Templates = append(refs.Templates, st)
}
return return
} }
func (d *BoltDb) getObjectRefsFrom(projectID int, objProps db.ObjectProps, objID objectID, referringObjectProps db.ObjectProps) (referringObjs []db.ObjectReferrer, err error) { func (d *BoltDb) getObjectRefsFrom(projectID int, objProps db.ObjectProps, objID objectID, referringObjectProps db.ObjectProps) (referringObjs []db.ObjectReferrer, err error) {
referringObjs = make([]db.ObjectReferrer, 0)
_, err = objProps.GetReferringFieldsFrom(referringObjectProps.Type) _, err = objProps.GetReferringFieldsFrom(referringObjectProps.Type)
if err != nil { if err != nil {
return return
} }
referringObjects := reflect.New(reflect.SliceOf(referringObjectProps.Type)) var referringObjects reflect.Value
err = d.getObjects(projectID, referringObjectProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool { if referringObjectProps.Type == db.ScheduleProps.Type {
return isObjectReferredBy(objProps, objID, referringObj) schedules := make([]db.Schedule, 0)
}, referringObjects.Interface()) err = d.getObjects(projectID, db.ScheduleProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
return isObjectReferredBy(objProps, objID, referringObj)
}, &schedules)
if err != nil { if err != nil {
return return
} }
for i := 0; i < referringObjects.Elem().Len(); i++ { for _, schedule := range schedules {
referringObjs = append(referringObjs, db.ObjectReferrer{ var template db.Template
ID: int(referringObjects.Elem().Index(i).FieldByName("ID").Int()), template, err = d.GetTemplate(projectID, schedule.TemplateID)
Name: referringObjects.Elem().Index(i).FieldByName("Name").String(), if err != nil {
}) return
}
referringObjs = append(referringObjs, db.ObjectReferrer{
ID: template.ID,
Name: template.Name,
})
}
} else {
referringObjects = reflect.New(reflect.SliceOf(referringObjectProps.Type))
err = d.getObjects(projectID, referringObjectProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
return isObjectReferredBy(objProps, objID, referringObj)
}, referringObjects.Interface())
if err != nil {
return
}
for i := 0; i < referringObjects.Elem().Len(); i++ {
referringObjs = append(referringObjs, db.ObjectReferrer{
ID: int(referringObjects.Elem().Index(i).FieldByName("ID").Int()),
Name: referringObjects.Elem().Index(i).FieldByName("Name").String(),
})
}
} }
return return

View File

@ -357,31 +357,52 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
repo1, err := store.CreateRepository(db.Repository{ repo1, err := store.CreateRepository(db.Repository{
Name: "repo1", Name: "repo1",
ProjectID: 1,
GitURL: "git@example.com/repo1", GitURL: "git@example.com/repo1",
GitBranch: "master", GitBranch: "master",
ProjectID: 1,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
ProjectID: 1,
Type: db.TemplateBuild, Type: db.TemplateBuild,
Name: "tpl1", Name: "tpl1",
Playbook: "build.yml", Playbook: "build.yml",
RepositoryID: repo1.ID, RepositoryID: repo1.ID,
ProjectID: 1,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
tpl2, err := store.CreateTemplate(db.Template{
Type: db.TemplateBuild,
Name: "tpl12",
Playbook: "build.yml",
ProjectID: 1,
})
if err != nil {
t.Fatal(err)
}
_, err = store.CreateSchedule(db.Schedule{
CronFormat: "* * * * *",
TemplateID: tpl2.ID,
ProjectID: 1,
RepositoryID: &repo1.ID,
})
if err != nil {
t.Fatal(err)
}
refs, err := store.GetRepositoryRefs(1, repo1.ID) refs, err := store.GetRepositoryRefs(1, repo1.ID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(refs.Templates) != 1 { if len(refs.Templates) != 2 {
t.Fatal() t.Fatal()
} }
} }

View File

@ -26,7 +26,7 @@ func TestMigration_2_8_40_Apply(t *testing.T) {
} }
err = r.Put([]byte("0000000001"), err = r.Put([]byte("0000000001"),
[]byte("{\"id\":\"1\",\"project_id\":\"1\",\"name\": \"test123\"}")) []byte("{\"id\":\"1\",\"project_id\":\"1\",\"alias\": \"test123\"}"))
return err return err
}) })
@ -54,8 +54,8 @@ func TestMigration_2_8_40_Apply(t *testing.T) {
t.Fatal("invalid name") t.Fatal("invalid name")
} }
if repo["name"] != nil { if repo["alias"] != nil {
t.Fatal("name must be deleted") t.Fatal("alias must be deleted")
} }
} }

View File

@ -369,10 +369,6 @@ func (d *SqlDb) getObjectRefs(projectID int, objectProps db.ObjectProps, objectI
return return
} }
func (d *SqlDb) getObjectRefCount(projectID int, objectProps db.ObjectProps, objectID int) (int, error) {
return 0, nil
}
func (d *SqlDb) getObjectRefsFrom( func (d *SqlDb) getObjectRefsFrom(
projectID int, projectID int,
objectProps db.ObjectProps, objectProps db.ObjectProps,