Skip to content

Commit

Permalink
fix momentum tree root bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Jun 16, 2023
1 parent 73f9f25 commit c21c78d
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 31 deletions.
34 changes: 34 additions & 0 deletions momentum-backend/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/plugins/jsvm"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
)
Expand Down Expand Up @@ -69,6 +71,9 @@ func main() {
})

app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {

testCleanUp(app.Dao().Clone())

dispatcher := momentumcore.NewDispatcher(momentumConfig, app)

// momentum core features must run before executing DB statements.
Expand All @@ -84,3 +89,32 @@ func main() {
log.Fatal(err)
}
}

func testCleanUp(dao *daos.Dao) {

keyvalues, err := dao.FindRecordsByExpr("keyValues")
depls, err := dao.FindRecordsByExpr("deployments")
stages, err := dao.FindRecordsByExpr("stages")
apps, err := dao.FindRecordsByExpr("applications")
reps, err := dao.FindRecordsByExpr("repositories")

if err != nil {
fmt.Println("TEST CLEAN FAILED:", err.Error())
}

for _, e := range keyvalues {
dao.DeleteRecord(e)
}
for _, e := range depls {
dao.DeleteRecord(e)
}
for _, e := range stages {
dao.DeleteRecord(e)
}
for _, e := range apps {
dao.DeleteRecord(e)
}
for _, e := range reps {
dao.DeleteRecord(e)
}
}
6 changes: 6 additions & 0 deletions momentum-backend/momentum-core/momentum-config/db-consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const TABLE_APPLICATIONS_FIELD_ID = "id"
const TABLE_APPLICATIONS_FIELD_NAME = "name"
const TABLE_APPLICATIONS_FIELD_STAGES = "stages"
const TABLE_APPLICATIONS_FIELD_HELMREPO = "helmRepository"
const TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY = "parentRepository"

const TABLE_DEPLOYMENTS_NAME = "deployments"
const TABLE_DEPLOYMENTS_FIELD_ID = "id"
const TABLE_DEPLOYMENTS_FIELD_NAME = "name"
const TABLE_DEPLOYMENTS_FIELD_DESCRIPTION = "description"
const TABLE_DEPLOYMENTS_FIELD_REPOSITORIES = "repositories"
const TABLE_DEPLOYMENTS_FIELD_PARENTSTAGE = "parentStage"

const TABLE_STAGES_NAME = "stages"
const TABLE_STAGES_FIELD_ID = "id"
const TABLE_STAGES_FIELD_NAME = "name"
const TABLE_STAGES_FIELD_DEPLOYMENTS = "deployments"
const TABLE_STAGES_FIELD_PARENTSTAGE = "parentStage"
const TABLE_STAGES_FIELD_PARENTAPPLICATION = "parentApplication"

const TABLE_TEMPLATES_NAME = "templates"
const TABLE_TEMPLATES_FIELD_ID = "id"
Expand All @@ -40,5 +44,7 @@ const TABLE_KEYVALUE_NAME = "keyValues"
const TABLE_KEYVALUE_FIELD_ID = "id"
const TABLE_KEYVALUE_FIELD_KEY = "key"
const TABLE_KEYVALUE_FIELD_VALUE = "value"
const TABLE_KEYVALUE_FIELD_PARENTSTAGE = "parentStage"
const TABLE_KEYVALUE_FIELD_PARENTDEPLOYMENT = "parentDeployment"

const TABLE_SECRETKEYVALUE_NAME = "secretKeyValues"
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *mod
if err != nil {
return nil, err
}
recs = append(recs, rec)

err = as.stageService.AddParentApplication(stages, rec)

recs = append(recs, rec.Id)
}
return recs, nil
}
Expand All @@ -51,11 +54,11 @@ func (as *ApplicationService) GetApplicationCollection() (*models.Collection, er
return as.dao.FindCollectionByNameOrId(consts.TABLE_APPLICATIONS_NAME)
}

func (as *ApplicationService) createWithoutEvent(name string, stageIds []string) (string, error) {
func (as *ApplicationService) createWithoutEvent(name string, stageIds []string) (*models.Record, error) {

appCollection, err := as.GetApplicationCollection()
if err != nil {
return "", err
return nil, err
}

appRecord := models.NewRecord(appCollection)
Expand All @@ -64,5 +67,5 @@ func (as *ApplicationService) createWithoutEvent(name string, stageIds []string)

err = as.dao.Clone().SaveRecord(appRecord)

return appRecord.Id, err
return appRecord, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package momentumservices

import (
"errors"
"fmt"
consts "momentum/momentum-core/momentum-config"
tree "momentum/momentum-core/momentum-tree"

Expand All @@ -29,39 +28,55 @@ func NewDeploymentService(dao *daos.Dao, keyValueService *KeyValueService) *Depl
return deplyomentService
}

func (ds *DeploymentService) SyncDeploymentsFromDisk(n *tree.Node) ([]string, error) {
func (ds *DeploymentService) SyncDeploymentsFromDisk(n *tree.Node) ([]*models.Record, error) {

deployments := n.AllDeployments()

deploymentIds := make([]string, 0)
deploymentIds := make([]*models.Record, 0)
for _, deployment := range deployments {

deploymentId, deploymentRecord, err := ds.createWithoutEvent(deployment.NormalizedPath())
deploymentRecord, err := ds.createWithoutEvent(deployment.NormalizedPath())
if err != nil {
return nil, err
}

if deployment.Kind == tree.File {
err = ds.keyValueService.SyncFile(deployment, deploymentRecord)

err := ds.keyValueService.SyncFile(deployment, deploymentRecord)
if err != nil {
return nil, err
}
}

deploymentIds = append(deploymentIds, deploymentId)
deploymentIds = append(deploymentIds, deploymentRecord)
}

return deploymentIds, nil
}

func (ds *DeploymentService) AddParentStage(stage *models.Record, deployments []*models.Record) error {

if stage.Collection().Name != consts.TABLE_STAGES_NAME {
return errors.New("stage is not record of stages collection")
}

for _, deployment := range deployments {
deployment.Set(consts.TABLE_DEPLOYMENTS_FIELD_PARENTSTAGE, stage.Id)
err := ds.saveWithoutEvent(deployment)
if err != nil {
return err
}
}

return nil
}

func (ds *DeploymentService) AddRepository(repositoryRecord *models.Record, deployments []*models.Record) error {

if repositoryRecord.Collection().Name != consts.TABLE_REPOSITORIES_NAME {
return errors.New("repositoryRecord is not record of repositories collection")
}

fmt.Println("adding repository", repositoryRecord.Id, "to deplyoments", deployments)

for _, depl := range deployments {

depl.Set(consts.TABLE_DEPLOYMENTS_FIELD_REPOSITORIES, append(depl.Get(consts.TABLE_DEPLOYMENTS_FIELD_REPOSITORIES).([]string), repositoryRecord.Id))
Expand All @@ -84,17 +99,22 @@ func (ds *DeploymentService) GetDeploymentsCollection() (*models.Collection, err
return coll, nil
}

func (ds *DeploymentService) createWithoutEvent(name string) (string, *models.Record, error) {
func (ds *DeploymentService) createWithoutEvent(name string) (*models.Record, error) {

deploymentCollection, err := ds.GetDeploymentsCollection()
if err != nil {
return "", nil, err
return nil, err
}

deploymentRecord := models.NewRecord(deploymentCollection)
deploymentRecord.Set(consts.TABLE_DEPLOYMENTS_FIELD_NAME, name)

err = ds.dao.Clone().SaveRecord(deploymentRecord)
err = ds.saveWithoutEvent(deploymentRecord)

return deploymentRecord, nil
}

func (ds *DeploymentService) saveWithoutEvent(record *models.Record) error {

return deploymentRecord.Id, deploymentRecord, nil
return ds.dao.Clone().SaveRecord(record)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ func (kvs *KeyValueService) SyncFile(n *tree.Node, parentArtifact *models.Record
return errors.New("can only sync nodes of type file")
}

kvs.syncChildren(n.Children, parentArtifact, n.NormalizedPath())

return nil
return kvs.syncChildren(n.Children, parentArtifact, n.NormalizedPath())
}

func (kvs *KeyValueService) GetKeyValueCollection() (*models.Collection, error) {
Expand Down Expand Up @@ -76,7 +74,12 @@ func (kvs *KeyValueService) syncChildren(children []*tree.Node, parentArtifact *
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_KEY, propertyPath)
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_VALUE, child.Value)

err = kvs.dao.Clone().SaveRecord(childRecord)
err = kvs.saveWithoutEvent(childRecord)
if err != nil {
break
}

err = kvs.addParentArtifact(parentArtifact, childRecord)
if err != nil {
break
}
Expand All @@ -87,7 +90,7 @@ func (kvs *KeyValueService) syncChildren(children []*tree.Node, parentArtifact *
} else {
parentArtifact.Set(consts.GENERIC_FIELD_KEYVALUES, childRecord.Id)
}
err = kvs.dao.Clone().SaveRecord(parentArtifact)
err = kvs.saveWithoutEvent(parentArtifact)
if err != nil {
break
}
Expand All @@ -96,3 +99,63 @@ func (kvs *KeyValueService) syncChildren(children []*tree.Node, parentArtifact *

return err
}

func (kvs *KeyValueService) addParentArtifact(parentArtifact *models.Record, keyValues *models.Record) error {

switch parentArtifact.Collection().Name {
case consts.TABLE_STAGES_NAME:
return kvs.addParentStage(parentArtifact, []*models.Record{keyValues})
case consts.TABLE_DEPLOYMENTS_NAME:
return kvs.addParentDeployment(parentArtifact, []*models.Record{keyValues})
default:
return errors.New("invalid parent record type")
}
}

func (kvs *KeyValueService) addParentStage(stage *models.Record, keyValues []*models.Record) error {

if stage.Collection().Name != consts.TABLE_STAGES_NAME {
return errors.New("parent stage must be record of collection stages")
}

for _, kv := range keyValues {

if kv.Collection().Name != consts.TABLE_KEYVALUE_NAME {
return errors.New("expected keyvalues record type to add parent stage")
}

kv.Set(consts.TABLE_KEYVALUE_FIELD_PARENTSTAGE, stage.Id)
err := kvs.saveWithoutEvent(kv)
if err != nil {
return err
}
}

return nil
}

func (kvs *KeyValueService) addParentDeployment(deployment *models.Record, keyValues []*models.Record) error {

if deployment.Collection().Name != consts.TABLE_DEPLOYMENTS_NAME {
return errors.New("parent deployment must be record of collection deploments")
}

for _, kv := range keyValues {

if kv.Collection().Name != consts.TABLE_KEYVALUE_NAME {
return errors.New("expected keyvalues record type to add parent deployment")
}

kv.Set(consts.TABLE_KEYVALUE_FIELD_PARENTDEPLOYMENT, deployment.Id)
err := kvs.saveWithoutEvent(kv)
if err != nil {
return err
}
}

return nil
}

func (kvs *KeyValueService) saveWithoutEvent(record *models.Record) error {
return kvs.dao.Clone().SaveRecord(record)
}
Loading

0 comments on commit c21c78d

Please sign in to comment.