-
Notifications
You must be signed in to change notification settings - Fork 31
/
1668292997-users.go
57 lines (49 loc) · 1.12 KB
/
1668292997-users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package migrations
import (
"database/sql"
"errors"
"log"
"strings"
"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 {
dao := daos.New(db)
collection, err := dao.FindCollectionByNameOrId("users")
if err != nil {
return err
}
_, err = dao.FindFirstRecordByData(collection.Name, "email", UserEmailPassword)
exists := true
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
exists = false
} else {
return err
}
}
if exists {
return nil
}
log.Println("inserting normal user: ", UserEmailPassword)
r := models.NewRecord(collection)
if err := r.SetEmail(UserEmailPassword); err != nil {
return err
}
if err := r.SetUsername(strings.Split(UserEmailPassword, "@")[0]); err != nil {
return err
}
if err := r.SetVerified(true); err != nil {
return err
}
if err := r.SetPassword(UserEmailPassword); err != nil {
return err
}
return dao.SaveRecord(r)
}, func(db dbx.Builder) error {
return nil
})
}