diff --git a/Dockerfile b/Dockerfile index b898df6..d716edb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/main.go b/main.go index 90f07c5..4a954b0 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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{ diff --git a/migrations/1730465434_created_notes.go b/migrations/1730465434_created_notes.go new file mode 100644 index 0000000..ac24187 --- /dev/null +++ b/migrations/1730465434_created_notes.go @@ -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) + }) +} diff --git a/migrations/1730465480_updated_notes.go b/migrations/1730465480_updated_notes.go new file mode 100644 index 0000000..fffce9c --- /dev/null +++ b/migrations/1730465480_updated_notes.go @@ -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) + }) +} diff --git a/pb_migrations/1730329446_collections_snapshot.js b/pb_migrations/1730329446_collections_snapshot.js deleted file mode 100644 index f81592e..0000000 --- a/pb_migrations/1730329446_collections_snapshot.js +++ /dev/null @@ -1,119 +0,0 @@ -/// -migrate((db) => { - const snapshot = [ - { - "id": "_pb_users_auth_", - "created": "2024-10-24 22:52:46.666Z", - "updated": "2024-10-24 22:52:46.667Z", - "name": "users", - "type": "auth", - "system": false, - "schema": [ - { - "system": false, - "id": "users_name", - "name": "name", - "type": "text", - "required": false, - "presentable": false, - "unique": false, - "options": { - "min": null, - "max": null, - "pattern": "" - } - }, - { - "system": false, - "id": "users_avatar", - "name": "avatar", - "type": "file", - "required": false, - "presentable": false, - "unique": false, - "options": { - "mimeTypes": [ - "image/jpeg", - "image/png", - "image/svg+xml", - "image/gif", - "image/webp" - ], - "thumbs": null, - "maxSelect": 1, - "maxSize": 5242880, - "protected": false - } - } - ], - "indexes": [], - "listRule": "id = @request.auth.id", - "viewRule": "id = @request.auth.id", - "createRule": "", - "updateRule": "id = @request.auth.id", - "deleteRule": "id = @request.auth.id", - "options": { - "allowEmailAuth": true, - "allowOAuth2Auth": true, - "allowUsernameAuth": true, - "exceptEmailDomains": null, - "manageRule": null, - "minPasswordLength": 8, - "onlyEmailDomains": null, - "onlyVerified": false, - "requireEmail": false - } - }, - { - "id": "455o35ury8quzkb", - "created": "2024-10-27 10:06:25.686Z", - "updated": "2024-10-27 10:06:25.686Z", - "name": "notes", - "type": "base", - "system": false, - "schema": [ - { - "system": false, - "id": "iaqugrmp", - "name": "title", - "type": "text", - "required": false, - "presentable": false, - "unique": false, - "options": { - "min": null, - "max": null, - "pattern": "" - } - }, - { - "system": false, - "id": "c5ebm5gb", - "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": {} - } - ]; - - const collections = snapshot.map((item) => new Collection(item)); - - return Dao(db).importCollections(collections, true, null); -}, (db) => { - return null; -})