Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
aleda145 committed Aug 4, 2022
1 parent 6b72576 commit c0bca0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/pocketbase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"net/http"

_ "github.com/aleda145/pocketbase-example/pocketbase/migrations"

"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
Expand Down
50 changes: 50 additions & 0 deletions backend/pocketbase/migrations/1659606226_user_nums.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/models"
"github.com/pocketbase/pocketbase/models/schema"
)

func init() {
userRequiredRule := "@request.user.id = user"
m.Register(func(db dbx.Builder) error {
collection := &models.Collection{
Name: "user_nums",
CreateRule: &userRequiredRule,
ListRule: &userRequiredRule,
ViewRule: &userRequiredRule,
UpdateRule: &userRequiredRule,
DeleteRule: &userRequiredRule,
Schema: schema.NewSchema(
&schema.SchemaField{
Name: "user",
Type: schema.FieldTypeUser,
Required: true,
Options: &schema.UserOptions{
MaxSelect: 1,
CascadeDelete: true,
},
},
&schema.SchemaField{
Name: "num",
Type: schema.FieldTypeNumber,
Options: &schema.NumberOptions{},
},
// ... etc.
),
}
return daos.New(db).SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db)

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

return daos.New(db).DeleteCollection(collection)
})
}

0 comments on commit c0bca0b

Please sign in to comment.