fixed autogenerated go collection update migration template
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
@@ -59,11 +60,11 @@ func (p *plugin) automigrateOnCollectionChange(e *core.CollectionRequestEvent) e
|
||||
var action string
|
||||
switch {
|
||||
case new == nil:
|
||||
action = "deleted_" + old.Name
|
||||
action = "deleted_" + normalizeCollectionName(old.Name)
|
||||
case old == nil:
|
||||
action = "created_" + new.Name
|
||||
action = "created_" + normalizeCollectionName(new.Name)
|
||||
default:
|
||||
action = "updated_" + old.Name
|
||||
action = "updated_" + normalizeCollectionName(old.Name)
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("%d_%s.%s", time.Now().Unix(), action, p.config.TemplateLang)
|
||||
@@ -93,3 +94,13 @@ func (p *plugin) automigrateOnCollectionChange(e *core.CollectionRequestEvent) e
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func normalizeCollectionName(name string) string {
|
||||
// adds an extra "_" suffix to the name in case the collection ends
|
||||
// with "test" to prevent accidentally resulting in "_test.go"/"_test.js" files
|
||||
if strings.HasSuffix(strings.ToLower(name), "test") {
|
||||
name += "_"
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
@@ -1036,7 +1036,7 @@ func init() {
|
||||
collection.Fields.RemoveById("f3_id")
|
||||
|
||||
// add field
|
||||
if err := json.Unmarshal([]byte(` + "`" + `[{
|
||||
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
|
||||
"autogeneratePattern": "",
|
||||
"hidden": false,
|
||||
"id": "f4_id",
|
||||
@@ -1049,12 +1049,12 @@ func init() {
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "text"
|
||||
}]` + "`" + `), &collection.Fields); err != nil {
|
||||
}` + "`" + `)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// update field
|
||||
if err := json.Unmarshal([]byte(` + "`" + `[{
|
||||
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
|
||||
"hidden": false,
|
||||
"id": "f2_id",
|
||||
"max": null,
|
||||
@@ -1065,7 +1065,7 @@ func init() {
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "number"
|
||||
}]` + "`" + `), &collection.Fields); err != nil {
|
||||
}` + "`" + `)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1099,7 +1099,7 @@ func init() {
|
||||
}
|
||||
|
||||
// add field
|
||||
if err := json.Unmarshal([]byte(` + "`" + `[{
|
||||
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
|
||||
"hidden": false,
|
||||
"id": "f3_id",
|
||||
"name": "f3_name",
|
||||
@@ -1107,7 +1107,7 @@ func init() {
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "bool"
|
||||
}]` + "`" + `), &collection.Fields); err != nil {
|
||||
}` + "`" + `)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1115,7 +1115,7 @@ func init() {
|
||||
collection.Fields.RemoveById("f4_id")
|
||||
|
||||
// update field
|
||||
if err := json.Unmarshal([]byte(` + "`" + `[{
|
||||
if err := collection.Fields.AddMarshaledJSON([]byte(` + "`" + `{
|
||||
"hidden": false,
|
||||
"id": "f2_id",
|
||||
"max": null,
|
||||
@@ -1126,7 +1126,7 @@ func init() {
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "number"
|
||||
}]` + "`" + `), &collection.Fields); err != nil {
|
||||
}` + "`" + `)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -546,7 +546,7 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
|
||||
upParts = append(upParts, fmt.Sprintf("%s.Fields.RemoveById(%q)\n", varName, oldField.GetId()))
|
||||
|
||||
downParts = append(downParts, "// add field")
|
||||
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`[%s]`), &%s.Fields)", escapeBacktick(string(rawOldField)), varName)))
|
||||
downParts = append(downParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawOldField)))))
|
||||
}
|
||||
|
||||
// created fields
|
||||
@@ -561,7 +561,7 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
|
||||
}
|
||||
|
||||
upParts = append(upParts, "// add field")
|
||||
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`[%s]`), &%s.Fields)", escapeBacktick(string(rawNewField)), varName)))
|
||||
upParts = append(upParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawNewField)))))
|
||||
|
||||
downParts = append(downParts, "// remove field")
|
||||
downParts = append(downParts, fmt.Sprintf("%s.Fields.RemoveById(%q)\n", varName, newField.GetId()))
|
||||
@@ -591,10 +591,10 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
|
||||
}
|
||||
|
||||
upParts = append(upParts, "// update field")
|
||||
upParts = append(upParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`[%s]`), &%s.Fields)", escapeBacktick(string(rawNewField)), varName)))
|
||||
upParts = append(upParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawNewField)))))
|
||||
|
||||
downParts = append(downParts, "// update field")
|
||||
downParts = append(downParts, goErrIf(fmt.Sprintf("json.Unmarshal([]byte(`[%s]`), &%s.Fields)", escapeBacktick(string(rawOldField)), varName)))
|
||||
downParts = append(downParts, goErrIf(fmt.Sprintf("%s.Fields.AddMarshaledJSON([]byte(`%s`))", varName, escapeBacktick(string(rawOldField)))))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user