Don't refresh the project more then necessary

The project does not need to be retreived from the db every time
it is queried for data. However, it does need to be updated whenever
a target is initialized from the project (eg. an upsert machine
action happens). This is because upserting a target will update the
associated project. Leaving the core.Project with stale data
unless it is refreshed.
This commit is contained in:
sophia 2022-05-10 15:24:14 -05:00
parent 387470e67f
commit bb30ff94ff

View File

@ -80,7 +80,6 @@ func (p *Project) Boxes() (bc core.BoxCollection, err error) {
// Config implements core.Project
func (p *Project) Config() (*vagrant_plugin_sdk.Vagrantfile_Vagrantfile, error) {
p.refreshProject()
return p.project.Configuration, nil
}
@ -108,7 +107,6 @@ func (p *Project) DefaultProvider() (name string, err error) {
// Home implements core.Project
func (p *Project) Home() (dir path.Path, err error) {
p.refreshProject()
return path.NewPath(p.project.Path), nil
}
@ -130,7 +128,6 @@ func (p *Project) PrimaryTargetName() (name string, err error) {
// Resource implements core.Project
func (p *Project) ResourceId() (string, error) {
p.refreshProject()
return p.project.ResourceId, nil
}
@ -168,7 +165,6 @@ func (p *Project) Target(nameOrId string) (core.Target, error) {
// TargetIds implements core.Project
func (p *Project) TargetIds() ([]string, error) {
p.refreshProject()
var ids []string
for _, t := range p.project.Targets {
ids = append(ids, t.ResourceId)
@ -183,7 +179,6 @@ func (p *Project) TargetIndex() (index core.TargetIndex, err error) {
// TargetNames implements core.Project
func (p *Project) TargetNames() ([]string, error) {
p.refreshProject()
var names []string
for _, t := range p.project.Targets {
names = append(names, t.Name)
@ -203,21 +198,18 @@ func (p *Project) UI() (terminal.UI, error) {
// VagrantfileName implements core.Project
func (p *Project) VagrantfileName() (name string, err error) {
p.refreshProject()
fullPath := path.NewPath(p.project.Configuration.Path)
return fullPath.Base().String(), nil
}
// VagrantfilePath implements core.Project
func (p *Project) VagrantfilePath() (pp path.Path, err error) {
p.refreshProject()
pp = path.NewPath(p.project.Configuration.Path).Parent()
return
}
// Targets
func (p *Project) Targets() ([]core.Target, error) {
p.refreshProject()
var targets []core.Target
for _, ref := range p.project.Targets {
t, err := p.LoadTarget(WithTargetRef(ref))
@ -231,7 +223,6 @@ func (p *Project) Targets() ([]core.Target, error) {
// Custom name defined for this project
func (p *Project) Name() string {
p.refreshProject()
return p.project.Name
}
@ -298,7 +289,6 @@ func (p *Project) Client() *serverclient.VagrantClient {
// Ref returns the project ref for API calls.
func (p *Project) Ref() interface{} {
p.refreshProject()
return &vagrant_plugin_sdk.Ref_Project{
ResourceId: p.project.ResourceId,
Name: p.project.Name,
@ -508,15 +498,16 @@ func (p *Project) InitTargets() (err error) {
updated = true
}
if !updated {
return
if updated {
// If targets have been updated then refresh the project. This is required
// since upserting targets will also update the project to have a reference
// to the new targets.
err = p.refreshProject()
}
err = p.refreshProject()
return
}
// Get's the latest project from the DB and caches is
// Get's the latest project from the DB
func (p *Project) refreshProject() (err error) {
result, err := p.Client().FindProject(p.ctx,
&vagrant_server.FindProjectRequest{