Adding files during migration #3234
-
I would like to add files from a seed migration file, but i'm not sure if it's possible, since i can't access the main "app", or "fs". Is there a alternative solution i could use? package migrations
import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/forms"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db)
collection, err := dao.FindCollectionByNameOrId("test_collection")
if err != nil {
return err
}
record := models.NewRecord(collection)
// Can't access app
form := forms.NewRecordUpsert(app, record)
form.LoadData(map[string]any{
"title": "Hello world!",
"slug": "hello-world",
"description": "Lorem ipsum...",
"rank": 123,
})
// Can't access fs
img, _ := filesystem.NewFileFromPath("./assets/test.jpg")
form.AddFiles("thumbnail", img)
return form.Submit()
}, func(db dbx.Builder) error {
return nil
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The As a workaround you can try to create a custom console command instead of a migration - https://pocketbase.io/docs/go-console-commands/. |
Beta Was this translation helpful? Give feedback.
-
Just hit this today. Would also like the ability to add files during migration natively without CLI commands. Ideally: |
Beta Was this translation helpful? Give feedback.
The
app
instance is not accessible in the Go migrations at the moment (this was suggested before but for now I'm reluctant to allow it because users could easily misuse it especially when working withapp.Dao()
since it won't be part of the migration transaction).As a workaround you can try to create a custom console command instead of a migration - https://pocketbase.io/docs/go-console-commands/.