fix(backup): export/import survay_variables

This commit is contained in:
Denis Gukov 2024-10-20 21:06:51 +00:00
parent 2a8f7cf884
commit a28cfd2908
3 changed files with 26 additions and 12 deletions

View File

@ -37,17 +37,17 @@ const (
)
type SurveyVarEnumValue struct {
Name string `json:"name"`
Value string `json:"value"`
Name string `json:"name" backup:"name"`
Value string `json:"value" backup:"value"`
}
type SurveyVar struct {
Name string `json:"name"`
Title string `json:"title"`
Required bool `json:"required"`
Type SurveyVarType `json:"type"`
Description string `json:"description"`
Values []SurveyVarEnumValue `json:"values"`
Name string `json:"name" backup:"name"`
Title string `json:"title" backup:"title"`
Required bool `json:"required" backup:"required"`
Type SurveyVarType `json:"type" backup:"type"`
Description string `json:"description" backup:"description"`
Values []SurveyVarEnumValue `json:"values" backup:"values"`
}
type TemplateFilter struct {
@ -94,8 +94,8 @@ type Template struct {
// SurveyVarsJSON used internally for read from database.
// It is not used for store survey vars to database.
// Do not use it in your code. Use SurveyVars instead.
SurveyVarsJSON *string `db:"survey_vars" json:"-"`
SurveyVars []SurveyVar `db:"-" json:"survey_vars" backup:"-"`
SurveyVarsJSON *string `db:"survey_vars" json:"-" backup:"-"`
SurveyVars []SurveyVar `db:"-" json:"survey_vars" backup:"survey_vars"`
SuppressSuccessAlerts bool `db:"suppress_success_alerts" json:"suppress_success_alerts"`

View File

@ -1,7 +1,9 @@
package bolt
import (
"encoding/json"
"errors"
"github.com/ansible-semaphore/semaphore/db"
"go.etcd.io/bbolt"
)
@ -67,7 +69,15 @@ func (d *BoltDb) GetTemplates(projectID int, filter db.TemplateFilter, params db
templatesMap := make(map[int]*db.Template)
for i := 0; i < len(templates); i++ {
templates[i].Vaults, err = d.GetTemplateVaults(projectID, templates[i].ID)
if templates[i].SurveyVarsJSON != nil {
err = json.Unmarshal([]byte(*templates[i].SurveyVarsJSON), &templates[i].SurveyVars)
}
if err != nil {
return
}
templatesMap[templates[i].ID] = &templates[i]
}

View File

@ -2,6 +2,7 @@ package sql
import (
"database/sql"
"encoding/json"
"github.com/Masterminds/squirrel"
"github.com/ansible-semaphore/semaphore/db"
@ -228,7 +229,10 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
}
}
template.Vaults, err = d.GetTemplateVaults(projectID, template.ID)
if tpl.SurveyVarsJSON != nil {
err = json.Unmarshal([]byte(*tpl.SurveyVarsJSON), &tpl.SurveyVars)
}
if err != nil {
return
}