onasty/cmd/seed/users.go(view raw)
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 58 |
package main
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/olexsmir/onasty/internal/hasher"
"github.com/olexsmir/onasty/internal/store/psqlutil"
)
var usersData = []struct {
id string
email string
password string
activated bool
}{
{ //nolint:exhaustruct
email: "admin@onasty.local",
password: "adminadmin",
activated: true,
},
{ //nolint:exhaustruct
email: "users@onasty.local",
activated: false,
password: "qwerty123",
},
}
func seedUsers(
ctx context.Context,
hash hasher.Hasher,
db *psqlutil.DB,
) error {
for i, user := range usersData {
passwrd, err := hash.Hash(user.password)
if err != nil {
return err
}
var id pgtype.UUID
err = db.QueryRow(ctx, `
insert into users (email, password, activated, created_at, last_login_at)
values ($1, $2, $3, $4, $5)
on conflict (email) do update
set password = excluded.password
returning id
`, user.email, passwrd, user.activated, time.Now(), time.Now()).
Scan(&id)
if err != nil {
return err
}
usersData[i].id = id.String()
}
return nil
}
|