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
}
//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
}
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)
if err != nil {
return
}
referringObjects := reflect.New(reflect.SliceOf(referringObjectProps.Type))
var referringObjects reflect.Value
err = d.getObjects(projectID, referringObjectProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
return isObjectReferredBy(objProps, objID, referringObj)
}, referringObjects.Interface())
if referringObjectProps.Type == db.ScheduleProps.Type {
schedules := make([]db.Schedule, 0)
err = d.getObjects(projectID, db.ScheduleProps, db.RetrieveQueryParams{}, func(referringObj interface{}) bool {
return isObjectReferredBy(objProps, objID, referringObj)
}, &schedules)
if err != nil {
return
}
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(),
})
for _, schedule := range schedules {
var template db.Template
template, err = d.GetTemplate(projectID, schedule.TemplateID)
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

View File

@ -357,31 +357,52 @@ func TestBoltDb_GetRepositoryRefs(t *testing.T) {
repo1, err := store.CreateRepository(db.Repository{
Name: "repo1",
ProjectID: 1,
GitURL: "git@example.com/repo1",
GitBranch: "master",
ProjectID: 1,
})
if err != nil {
t.Fatal(err)
}
_, err = store.CreateTemplate(db.Template{
ProjectID: 1,
Type: db.TemplateBuild,
Name: "tpl1",
Playbook: "build.yml",
RepositoryID: repo1.ID,
ProjectID: 1,
})
if err != nil {
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)
if err != nil {
t.Fatal(err)
}
if len(refs.Templates) != 1 {
if len(refs.Templates) != 2 {
t.Fatal()
}
}

View File

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

View File

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