added empty migrations template test and removed publicdir plugin
This commit is contained in:
parent
04018f931b
commit
98cc8e2aee
|
@ -1,5 +1,7 @@
|
||||||
## (WIP) v0.9.0
|
## (WIP) v0.9.0
|
||||||
|
|
||||||
|
- Added `Record.OriginalCopy()` method that returns a new `Record` copy populated with the initially loaded record data (useful if you want to compare old and new field values).
|
||||||
|
|
||||||
- Added new event hooks:
|
- Added new event hooks:
|
||||||
```go
|
```go
|
||||||
app.OnBeforeBootstrap()
|
app.OnBeforeBootstrap()
|
||||||
|
@ -16,7 +18,7 @@
|
||||||
migrationsDir := "" // default to "pb_migrations" (for js) and "migrations" (for go)
|
migrationsDir := "" // default to "pb_migrations" (for js) and "migrations" (for go)
|
||||||
|
|
||||||
// load js files if you want to allow loading external JavaScript migrations
|
// load js files if you want to allow loading external JavaScript migrations
|
||||||
jsvm.MustRegisterMigrationsLoader(app, &jsvm.MigrationsLoaderOptions{
|
jsvm.MustRegisterMigrations(app, &jsvm.MigrationsOptions{
|
||||||
Dir: migrationsDir,
|
Dir: migrationsDir,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,14 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
|
"github.com/pocketbase/pocketbase/core"
|
||||||
"github.com/pocketbase/pocketbase/plugins/jsvm"
|
"github.com/pocketbase/pocketbase/plugins/jsvm"
|
||||||
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
||||||
"github.com/pocketbase/pocketbase/plugins/publicdir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -37,7 +40,7 @@ func main() {
|
||||||
app.RootCmd.PersistentFlags().StringVar(
|
app.RootCmd.PersistentFlags().StringVar(
|
||||||
&publicDir,
|
&publicDir,
|
||||||
"publicDir",
|
"publicDir",
|
||||||
"",
|
defaultPublicDir(),
|
||||||
"the directory to serve static files",
|
"the directory to serve static files",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,11 +55,11 @@ func main() {
|
||||||
app.RootCmd.ParseFlags(os.Args[1:])
|
app.RootCmd.ParseFlags(os.Args[1:])
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// Plugins:
|
// Plugins and hooks:
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
// load js pb_migrations
|
// load js pb_migrations
|
||||||
jsvm.MustRegisterMigrationsLoader(app, &jsvm.MigrationsLoaderOptions{
|
jsvm.MustRegisterMigrations(app, &jsvm.MigrationsOptions{
|
||||||
Dir: migrationsDir,
|
Dir: migrationsDir,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -67,13 +70,22 @@ func main() {
|
||||||
Dir: migrationsDir,
|
Dir: migrationsDir,
|
||||||
})
|
})
|
||||||
|
|
||||||
// pb_public dir
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
publicdir.MustRegister(app, &publicdir.Options{
|
// serves static files from the provided public dir (if exists)
|
||||||
Dir: publicDir,
|
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS(publicDir), indexFallback))
|
||||||
IndexFallback: indexFallback,
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := app.Start(); err != nil {
|
if err := app.Start(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the default pb_public dir location is relative to the executable
|
||||||
|
func defaultPublicDir() string {
|
||||||
|
if strings.HasPrefix(os.Args[0], os.TempDir()) {
|
||||||
|
// most likely ran with go run
|
||||||
|
return "./pb_public"
|
||||||
|
}
|
||||||
|
return filepath.Join(os.Args[0], "../pb_public")
|
||||||
|
}
|
||||||
|
|
|
@ -11,46 +11,46 @@ import (
|
||||||
m "github.com/pocketbase/pocketbase/migrations"
|
m "github.com/pocketbase/pocketbase/migrations"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MigrationsLoaderOptions defines optional struct to customize the default plugin behavior.
|
// MigrationsOptions defines optional struct to customize the default migrations loader behavior.
|
||||||
type MigrationsLoaderOptions struct {
|
type MigrationsOptions struct {
|
||||||
// Dir specifies the directory with the JS migrations.
|
// Dir specifies the directory with the JS migrations.
|
||||||
//
|
//
|
||||||
// If not set it fallbacks to a relative "pb_data/../pb_migrations" directory.
|
// If not set it fallbacks to a relative "pb_data/../pb_migrations" directory.
|
||||||
Dir string
|
Dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
// migrationsLoader is the plugin definition.
|
// migrations is the migrations loader plugin definition.
|
||||||
// Usually it is instantiated via RegisterMigrationsLoader or MustRegisterMigrationsLoader.
|
// Usually it is instantiated via RegisterMigrations or MustRegisterMigrations.
|
||||||
type migrationsLoader struct {
|
type migrations struct {
|
||||||
app core.App
|
app core.App
|
||||||
options *MigrationsLoaderOptions
|
options *MigrationsOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// MustRegisterMigrationsLoader registers the plugin to the provided
|
// MustRegisterMigrations registers the migrations loader plugin to
|
||||||
// app instance and panics if it fails.
|
// the provided app instance and panics if it fails.
|
||||||
//
|
//
|
||||||
// It it calls RegisterMigrationsLoader(app, options)
|
// Internally it calls RegisterMigrations(app, options).
|
||||||
//
|
//
|
||||||
// If options is nil, by default the js files from pb_data/migrations are loaded.
|
// If options is nil, by default the js files from pb_data/migrations are loaded.
|
||||||
// Set custom options.Dir if you want to change it to some other directory.
|
// Set custom options.Dir if you want to change it to some other directory.
|
||||||
func MustRegisterMigrationsLoader(app core.App, options *MigrationsLoaderOptions) {
|
func MustRegisterMigrations(app core.App, options *MigrationsOptions) {
|
||||||
if err := RegisterMigrationsLoader(app, options); err != nil {
|
if err := RegisterMigrations(app, options); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterMigrationsLoader registers the plugin to the provided app instance.
|
// RegisterMigrations registers the plugin to the provided app instance.
|
||||||
//
|
//
|
||||||
// If options is nil, by default the js files from pb_data/migrations are loaded.
|
// If options is nil, by default the js files from pb_data/migrations are loaded.
|
||||||
// Set custom options.Dir if you want to change it to some other directory.
|
// Set custom options.Dir if you want to change it to some other directory.
|
||||||
func RegisterMigrationsLoader(app core.App, options *MigrationsLoaderOptions) error {
|
func RegisterMigrations(app core.App, options *MigrationsOptions) error {
|
||||||
l := &migrationsLoader{app: app}
|
l := &migrations{app: app}
|
||||||
|
|
||||||
if options != nil {
|
if options != nil {
|
||||||
l.options = options
|
l.options = options
|
||||||
} else {
|
} else {
|
||||||
l.options = &MigrationsLoaderOptions{}
|
l.options = &MigrationsOptions{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.options.Dir == "" {
|
if l.options.Dir == "" {
|
||||||
|
|
|
@ -667,7 +667,7 @@ func init() {
|
||||||
|
|
||||||
// save the changes and trigger automigrate
|
// save the changes and trigger automigrate
|
||||||
if err := app.Dao().SaveCollection(collection); err != nil {
|
if err := app.Dao().SaveCollection(collection); err != nil {
|
||||||
t.Fatalf("[%d] Failed to delete dummy collection, got %v", i, err)
|
t.Fatalf("[%d] Failed to save dummy collection changes, got %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := os.ReadDir(migrationsDir)
|
files, err := os.ReadDir(migrationsDir)
|
||||||
|
@ -695,3 +695,52 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAutomigrateCollectionNoChanges(t *testing.T) {
|
||||||
|
scenarios := []struct {
|
||||||
|
lang string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
migratecmd.TemplateLangJS,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
migratecmd.TemplateLangGo,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, s := range scenarios {
|
||||||
|
app, _ := tests.NewTestApp()
|
||||||
|
defer app.Cleanup()
|
||||||
|
|
||||||
|
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
|
||||||
|
|
||||||
|
migratecmd.MustRegister(app, nil, &migratecmd.Options{
|
||||||
|
TemplateLang: s.lang,
|
||||||
|
Automigrate: true,
|
||||||
|
Dir: migrationsDir,
|
||||||
|
})
|
||||||
|
|
||||||
|
// create dummy collection
|
||||||
|
collection := &models.Collection{}
|
||||||
|
collection.Name = "test123"
|
||||||
|
collection.Type = models.CollectionTypeAuth
|
||||||
|
|
||||||
|
// use different dao to avoid triggering automigrate while saving the dummy collection
|
||||||
|
if err := daos.New(app.DB()).SaveCollection(collection); err != nil {
|
||||||
|
t.Fatalf("[%d] Failed to save dummy collection, got %v", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo remove after collections cache is replaced
|
||||||
|
app.Bootstrap()
|
||||||
|
|
||||||
|
// resave without changes and trigger automigrate
|
||||||
|
if err := app.Dao().SaveCollection(collection); err != nil {
|
||||||
|
t.Fatalf("[%d] Failed to save dummy collection update, got %v", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
files, _ := os.ReadDir(migrationsDir)
|
||||||
|
if total := len(files); total != 0 {
|
||||||
|
t.Fatalf("[%d] Expected 0 files to be generated, got %d", i, total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
// Example
|
|
||||||
//
|
|
||||||
// publicdir.MustRegister(app, &publicdir.Options{
|
|
||||||
// FlagsCmd: app.RootCmd,
|
|
||||||
// IndexFallback: false,
|
|
||||||
// })
|
|
||||||
package publicdir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
|
||||||
"github.com/pocketbase/pocketbase/core"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Options struct {
|
|
||||||
Dir string
|
|
||||||
IndexFallback bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type plugin struct {
|
|
||||||
app core.App
|
|
||||||
options *Options
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustRegister(app core.App, options *Options) {
|
|
||||||
if err := Register(app, options); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Register(app core.App, options *Options) error {
|
|
||||||
p := &plugin{app: app}
|
|
||||||
|
|
||||||
if options != nil {
|
|
||||||
p.options = options
|
|
||||||
} else {
|
|
||||||
p.options = &Options{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.Dir == "" {
|
|
||||||
options.Dir = defaultPublicDir()
|
|
||||||
}
|
|
||||||
|
|
||||||
p.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
||||||
// serves static files from the provided public dir (if exists)
|
|
||||||
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS(options.Dir), options.IndexFallback))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultPublicDir() string {
|
|
||||||
if strings.HasPrefix(os.Args[0], os.TempDir()) {
|
|
||||||
// most likely ran with go run
|
|
||||||
return "./pb_public"
|
|
||||||
}
|
|
||||||
return filepath.Join(os.Args[0], "../pb_public")
|
|
||||||
}
|
|
Loading…
Reference in New Issue