Skip to content

Commit

Permalink
Merge pull request #26 from natrontech/add-parent-relations
Browse files Browse the repository at this point in the history
add parent relations
  • Loading branch information
Joel-Haeberli committed Jun 16, 2023
2 parents 73f9f25 + 7369e75 commit e403e19
Show file tree
Hide file tree
Showing 19 changed files with 572 additions and 52 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)
}
}
7 changes: 7 additions & 0 deletions momentum-backend/momentum-core/momentum-config/db-consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ const TABLE_REPOSITORIES_NAME = "repositories"
const TABLE_REPOSITORIES_FIELD_ID = "id"
const TABLE_REPOSITORIES_FIELD_NAME = "name"
const TABLE_REPOSITORIES_FIELD_URL = "url"
const TABLE_REPOSITORIES_FIELD_APPLICATIONS = "applications"

const TABLE_APPLICATIONS_NAME = "applications"
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 +45,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 @@ -8,17 +8,29 @@ import (
)

type ApplicationController struct {
appService *services.ApplicationService
appService *services.ApplicationService
repositoryService *services.RepositoryService
}

func NewApplicationController(appService *services.ApplicationService) *ApplicationController {
func NewApplicationController(appService *services.ApplicationService, repoService *services.RepositoryService) *ApplicationController {

appController := new(ApplicationController)
appController.appService = appService
appController.repositoryService = repoService

return appController
}

func (ac *ApplicationController) AddRepositoryToApplications(repoAddedEvent *RepositoryAddedEvent) error {

repositoryRecord, err := ac.repositoryService.FindForName(repoAddedEvent.RepositoryName)
if err != nil {
return err
}

return ac.appService.AddRepository(repositoryRecord, repoAddedEvent.Applications)
}

func (ac *ApplicationController) AddApplication(record *models.Record, conf *config.MomentumConfig) error {

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type RepositoryAddedEvent struct {
RepositoryName string
Applications []*models.Record
Deployments []*models.Record
}

Expand Down Expand Up @@ -60,7 +61,7 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi
return err
}

_, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
_, apps, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
if err != nil {
fmt.Println("ERROR:", err.Error())
utils.DirDelete(path)
Expand All @@ -69,6 +70,7 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi

repoAddedEvent := new(RepositoryAddedEvent)
repoAddedEvent.RepositoryName = repoName
repoAddedEvent.Applications = apps
repoAddedEvent.Deployments = deployments

rc.repositoryAddedEventChannel <- repoAddedEvent
Expand Down
10 changes: 8 additions & 2 deletions momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
repoService := services.NewRepositoryService(pb.Dao(), appService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService, repoService)
dispatcher.StagesController = controllers.NewStageController(stageService)
dispatcher.DeploymentController = controllers.NewDeploymentController(deploymentService, repoService)

Expand Down Expand Up @@ -136,7 +136,13 @@ func (d *MomentumDispatcher) setupRepositoryAddedEventChannelObserver() {

err := d.DeploymentController.AddRepositoryToDeployments(event)
if err != nil {
fmt.Println("failed adding relationship to deployments for repository after reciving RepositoryAddedEvent:", event, err, err.Error())
fmt.Println("failed adding relationship to deployments for repository after receiving RepositoryAddedEvent:", event, err, err.Error())
return err
}

err = d.ApplicationsController.AddRepositoryToApplications(event)
if err != nil {
fmt.Println("failed adding relationship to applications for repository after receiving RepositoryAddedEvent:", event, err, err.Error())
return err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package momentumservices

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

Expand All @@ -26,43 +28,74 @@ func NewApplicationService(dao *daos.Dao, stageService *StageService) *Applicati
return appService
}

func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *models.Record) ([]string, error) {
func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *models.Record) ([]*models.Record, error) {

recs := make([]string, 0)
recs := make([]*models.Record, 0)
apps := n.Apps()
for _, a := range apps {
fmt.Println(a)
}
for _, app := range apps {

stages, err := as.stageService.SyncStagesFromDisk(app)
if err != nil {
return nil, err
}

rec, err := as.createWithoutEvent(app.NormalizedPath(), stages)
stageIds := make([]string, 0)
for _, stage := range stages {
stageIds = append(stageIds, stage.Id)
}

rec, err := as.createWithoutEvent(app.NormalizedPath(), stageIds)
if err != nil {
return nil, err
}

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

recs = append(recs, rec)
}
return recs, nil
}

func (as *ApplicationService) AddRepository(repositoryRecord *models.Record, applications []*models.Record) error {

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

for _, app := range applications {

app.Set(consts.TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY, repositoryRecord.Id)
err := as.saveWithoutEvent(app)
if err != nil {
return err
}
}

return nil
}

func (as *ApplicationService) GetApplicationCollection() (*models.Collection, error) {

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)
appRecord.Set(consts.TABLE_APPLICATIONS_FIELD_NAME, name)
appRecord.Set(consts.TABLE_APPLICATIONS_FIELD_STAGES, stageIds)

err = as.dao.Clone().SaveRecord(appRecord)
return appRecord, as.saveWithoutEvent(appRecord)
}

return appRecord.Id, err
func (as *ApplicationService) saveWithoutEvent(record *models.Record) error {
return as.dao.Clone().SaveRecord(record)
}
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,43 +28,59 @@ 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))
err := ds.dao.SaveRecord(depl)
err := ds.saveWithoutEvent(depl)
if err != nil {
return err
}
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)
}
Loading

0 comments on commit e403e19

Please sign in to comment.