Skip to content

Commit

Permalink
tweaked automigrate to check for git status and extracted the base fl…
Browse files Browse the repository at this point in the history
…ags from the plugins
  • Loading branch information
ganigeorgiev committed Nov 26, 2022
1 parent 8c9b657 commit 675d459
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 367 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
# goreleaser builds folder
/.builds/

# examples app directories
pb_data
pb_public

# tests coverage
coverage.out

Expand Down
241 changes: 0 additions & 241 deletions cmd/migrate.go

This file was deleted.

21 changes: 19 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (

"github.com/fatih/color"
"github.com/labstack/echo/v5/middleware"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/migrations/logs"
"github.com/pocketbase/pocketbase/tools/migrate"
"github.com/spf13/cobra"
"golang.org/x/crypto/acme"
Expand All @@ -38,7 +41,7 @@ func NewServeCommand(app core.App, showStartBanner bool) *cobra.Command {
// (or if this is the first time the init migration was executed)
if err := app.RefreshSettings(); err != nil {
color.Yellow("=====================================")
color.Yellow("WARNING - Settings load error! \n%v", err)
color.Yellow("WARNING: Settings load error! \n%v", err)
color.Yellow("Fallback to the application defaults.")
color.Yellow("=====================================")
}
Expand Down Expand Up @@ -137,8 +140,22 @@ func NewServeCommand(app core.App, showStartBanner bool) *cobra.Command {
return command
}

type migrationsConnection struct {
DB *dbx.DB
MigrationsList migrate.MigrationsList
}

func runMigrations(app core.App) error {
connections := migrationsConnectionsMap(app)
connections := []migrationsConnection{
{
DB: app.DB(),
MigrationsList: migrations.AppMigrations,
},
{
DB: app.LogsDB(),
MigrationsList: logs.LogsMigrations,
},
}

for _, c := range connections {
runner, err := migrate.NewRunner(c.DB, c.MigrationsList)
Expand Down
3 changes: 3 additions & 0 deletions examples/base/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pb_data/
pb_public/
pb_migrations/
56 changes: 52 additions & 4 deletions examples/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"log"
"os"
"os/exec"

"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/plugins/jsvm"
Expand All @@ -12,19 +14,65 @@ import (
func main() {
app := pocketbase.New()

// ---------------------------------------------------------------
// Optional plugin flags:
// ---------------------------------------------------------------

var migrationsDir string
app.RootCmd.PersistentFlags().StringVar(
&migrationsDir,
"migrationsDir",
"",
"the directory with the user defined migrations",
)

var automigrate bool
_, gitErr := exec.LookPath("git")
app.RootCmd.PersistentFlags().BoolVar(
&automigrate,
"automigrate",
gitErr == nil,
"enable/disable auto migrations",
)

var publicDir string
app.RootCmd.PersistentFlags().StringVar(
&publicDir,
"publicDir",
"",
"the directory to serve static files",
)

var indexFallback bool
app.RootCmd.PersistentFlags().BoolVar(
&indexFallback,
"indexFallback",
true,
"fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA)",
)

app.RootCmd.ParseFlags(os.Args[1:])

// ---------------------------------------------------------------
// Plugins:
// ---------------------------------------------------------------

// load js pb_migrations
jsvm.MustRegisterMigrationsLoader(app, nil)
jsvm.MustRegisterMigrationsLoader(app, &jsvm.MigrationsLoaderOptions{
Dir: migrationsDir,
})

// migrate command (with js templates)
migratecmd.MustRegister(app, app.RootCmd, &migratecmd.Options{
TemplateLang: migratecmd.TemplateLangJS,
AutoMigrate: true,
Automigrate: automigrate,
Dir: migrationsDir,
})

// pb_public dir
publicdir.MustRegister(app, &publicdir.Options{
FlagsCmd: app.RootCmd,
IndexFallback: true,
Dir: publicDir,
IndexFallback: indexFallback,
})

if err := app.Start(); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions plugins/jsvm/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

// MigrationsLoaderOptions defines optional struct to customize the default plugin behavior.
type MigrationsLoaderOptions struct {
// Dir is the app migrations directory from where the js files will be loaded
// (default to pb_data/migrations)
// Dir specifies the directory with the JS migrations.
//
// If not set it fallbacks to a relative "pb_data/../pb_migrations" directory.
Dir string
}

Expand Down
Loading

0 comments on commit 675d459

Please sign in to comment.