Skip to content

Commit

Permalink
Switch to automigrations
Browse files Browse the repository at this point in the history
  • Loading branch information
karniv00l committed Nov 20, 2023
1 parent 56acd63 commit 362738f
Show file tree
Hide file tree
Showing 7 changed files with 928 additions and 67 deletions.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"log"
"net/http"
"os"
"strings"

_ "main/migrations"

Expand All @@ -13,6 +15,7 @@ import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
)

func main() {
Expand All @@ -22,6 +25,15 @@ func main() {

app := pocketbase.New()

// loosely check if it was executed using "go run"
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.AddRoute(echo.Route{
Method: http.MethodGet,
Expand Down
26 changes: 0 additions & 26 deletions migrations/1667688866_create_stargazers.go

This file was deleted.

101 changes: 101 additions & 0 deletions migrations/1700477408_created_iniFiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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": "9eif9v40b0uw9l8",
"created": "2023-11-20 10:50:08.919Z",
"updated": "2023-11-20 10:50:08.919Z",
"name": "iniFiles",
"type": "base",
"system": false,
"schema": [
{
"system": false,
"id": "h9yfwmvx",
"name": "signature",
"type": "text",
"required": true,
"presentable": false,
"unique": false,
"options": {
"min": 3,
"max": 255,
"pattern": ""
}
},
{
"system": false,
"id": "z3e1a5cl",
"name": "file",
"type": "file",
"required": true,
"presentable": false,
"unique": false,
"options": {
"maxSelect": 1,
"maxSize": 5242880,
"mimeTypes": [
"application/gzip",
"application/octet-stream"
],
"thumbs": [],
"protected": false
}
},
{
"system": false,
"id": "t5uayom3",
"name": "ecosystem",
"type": "select",
"required": true,
"presentable": false,
"unique": false,
"options": {
"maxSelect": 1,
"values": [
"speeduino",
"rusefi",
"fome"
]
}
}
],
"indexes": [
"CREATE INDEX ` + "`" + `officialIniFiles_created_idx` + "`" + ` ON ` + "`" + `iniFiles` + "`" + ` (` + "`" + `created` + "`" + `)",
"CREATE UNIQUE INDEX ` + "`" + `idx_unique_h9yfwmvx` + "`" + ` ON ` + "`" + `iniFiles` + "`" + ` (` + "`" + `signature` + "`" + `)"
],
"listRule": "",
"viewRule": "",
"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("9eif9v40b0uw9l8")
if err != nil {
return err
}

return dao.DeleteCollection(collection)
})
}
83 changes: 83 additions & 0 deletions migrations/1700477408_created_stargazers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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": "z8cojwcvlyxxyll",
"created": "2023-11-20 10:50:08.921Z",
"updated": "2023-11-20 10:50:08.921Z",
"name": "stargazers",
"type": "base",
"system": false,
"schema": [
{
"system": false,
"id": "him7pbq2",
"name": "user",
"type": "relation",
"required": true,
"presentable": false,
"unique": false,
"options": {
"collectionId": "_pb_users_auth_",
"cascadeDelete": false,
"minSelect": null,
"maxSelect": 1,
"displayFields": null
}
},
{
"system": false,
"id": "ny7akrmn",
"name": "tune",
"type": "relation",
"required": true,
"presentable": false,
"unique": false,
"options": {
"collectionId": "5djmpehuiigg06b",
"cascadeDelete": false,
"minSelect": null,
"maxSelect": 1,
"displayFields": null
}
}
],
"indexes": [
"CREATE INDEX ` + "`" + `_z8cojwcvlyxxyll_created_idx` + "`" + ` ON ` + "`" + `stargazers` + "`" + ` (` + "`" + `created` + "`" + `)",
"CREATE UNIQUE INDEX ` + "`" + `unique_stargazers_on_user_tune` + "`" + ` ON ` + "`" + `stargazers` + "`" + ` (` + "`" + `user` + "`" + `, ` + "`" + `tune` + "`" + `)"
],
"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("z8cojwcvlyxxyll")
if err != nil {
return err
}

return dao.DeleteCollection(collection)
})
}
Loading

0 comments on commit 362738f

Please sign in to comment.