Skip to content

Commit

Permalink
handle migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomorebonato committed Nov 1, 2024
1 parent 9bf4ac2 commit f83f6bc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ WORKDIR /app
COPY --from=builder-golang /app/pocket-react /pb/pocket-react

# uncomment to copy the local pb_migrations dir into the image
COPY ./pb_migrations /pb/pb_migrations
# COPY ./pb_migrations /pb/pb_migrations

# uncomment to copy the local pb_hooks dir into the image
# COPY ./pb_hooks /pb/pb_hooks
Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"net/http"
"os"
"pocket-react/backend"
"strings"

"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
)

func main() {
Expand All @@ -24,6 +26,14 @@ func main() {
log.Fatal(err)
}

isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())

migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
// enable auto creation of migration files when making collection changes in the Admin UI
// (the isGoRun check is to enable it only during development)
Automigrate: isGoRun,
})

app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET("/health", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{
Expand Down
76 changes: 76 additions & 0 deletions migrations/1730465434_created_notes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package migrations

import (
"encoding/json"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models"
)

func init() {
m.Register(func(db dbx.Builder) error {
jsonData := `{
"id": "n1io46jj22k6dfx",
"created": "2024-11-01 12:50:34.056Z",
"updated": "2024-11-01 12:50:34.056Z",
"name": "notes",
"type": "base",
"system": false,
"schema": [
{
"system": false,
"id": "svp5iioh",
"name": "title",
"type": "text",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"system": false,
"id": "ndrlbmqo",
"name": "content",
"type": "text",
"required": false,
"presentable": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}
],
"indexes": [],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
}`

collection := &models.Collection{}
if err := json.Unmarshal([]byte(jsonData), &collection); err != nil {
return err
}

return daos.New(db).SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);

collection, err := dao.FindCollectionByNameOrId("n1io46jj22k6dfx")
if err != nil {
return err
}

return dao.DeleteCollection(collection)
})
}
50 changes: 50 additions & 0 deletions migrations/1730465480_updated_notes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package migrations

import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/tools/types"
)

func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);

collection, err := dao.FindCollectionByNameOrId("n1io46jj22k6dfx")
if err != nil {
return err
}

collection.ListRule = types.Pointer("")

collection.ViewRule = types.Pointer("")

collection.CreateRule = types.Pointer("")

collection.UpdateRule = types.Pointer("")

collection.DeleteRule = types.Pointer("")

return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);

collection, err := dao.FindCollectionByNameOrId("n1io46jj22k6dfx")
if err != nil {
return err
}

collection.ListRule = nil

collection.ViewRule = nil

collection.CreateRule = nil

collection.UpdateRule = nil

collection.DeleteRule = nil

return dao.SaveCollection(collection)
})
}
119 changes: 0 additions & 119 deletions pb_migrations/1730329446_collections_snapshot.js

This file was deleted.

0 comments on commit f83f6bc

Please sign in to comment.