1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-25 23:52:32 +02:00

fixed autogenerated go collection update migration template

This commit is contained in:
Gani Georgiev
2024-10-09 17:11:14 +03:00
parent 307ce1aa4e
commit 54344f2b9d
7 changed files with 252 additions and 58 deletions

View File

@@ -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
}