pronounsfu/scripts/seeddb/main.go

148 lines
3.6 KiB
Go
Raw Normal View History

package seeddb
2022-11-19 18:11:01 -08:00
import (
2023-09-10 07:49:16 -07:00
"log"
2022-11-19 18:11:01 -08:00
"os"
2023-06-03 07:18:47 -07:00
"codeberg.org/pronounscc/pronouns.cc/backend/db"
2023-04-03 19:11:03 -07:00
"github.com/jackc/pgx/v5/pgxpool"
2022-11-19 18:11:01 -08:00
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
2023-09-10 07:49:16 -07:00
"gopkg.in/yaml.v3"
2022-11-19 18:11:01 -08:00
)
2023-09-10 07:49:16 -07:00
type Seed struct {
Users []SeedUser `yaml:"users"`
}
type SeedUser struct {
Username string `yaml:"username"`
DisplayName *string `yaml:"displayName"`
Bio *string `yaml:"bio"`
Links []string `yaml:"links"`
Names []db.FieldEntry `yaml:"names"`
Pronouns []db.PronounEntry `yaml:"pronouns"`
Fields []db.Field `yaml:"fields"`
Members []SeedMember `yaml:"members"`
}
type SeedMember struct {
Name string `yaml:"name"`
DisplayName *string `yaml:"displayName"`
Bio string `yaml:"bio"`
Links []string `yaml:"links"`
Names []db.FieldEntry `yaml:"names"`
Pronouns []db.PronounEntry `yaml:"pronouns"`
Fields []db.Field `yaml:"fields"`
Members []SeedMember `yaml:"members"`
}
var Command = &cli.Command{
Name: "seed",
Usage: "Seed the database with test data",
Action: run,
}
func run(c *cli.Context) error {
2022-11-19 18:11:01 -08:00
err := godotenv.Load()
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error loading .env file:", err)
return err
2022-11-19 18:11:01 -08:00
}
ctx := c.Context
2022-11-19 18:11:01 -08:00
2023-04-03 19:11:03 -07:00
pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
2022-11-19 18:11:01 -08:00
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error opening database:", err)
return err
2022-11-19 18:11:01 -08:00
}
defer pool.Close()
2023-09-10 07:49:16 -07:00
log.Println("opened database")
2022-11-19 18:11:01 -08:00
pg := &db.DB{Pool: pool}
2023-09-10 07:49:16 -07:00
// read seed file
seedFile, err := os.ReadFile("seed.yaml")
2022-11-19 18:11:01 -08:00
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error opening seed.yaml:", err)
return err
2022-11-19 18:11:01 -08:00
}
2023-09-10 07:49:16 -07:00
var seed Seed
err = yaml.Unmarshal(seedFile, &seed)
2022-11-19 18:11:01 -08:00
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error reading seed.yaml:", err)
return err
2022-11-19 18:11:01 -08:00
}
2023-09-10 07:49:16 -07:00
tx, err := pg.Begin(ctx)
2022-11-19 18:11:01 -08:00
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error beginning transaction:", err)
return err
2022-11-19 18:11:01 -08:00
}
2023-09-10 07:49:16 -07:00
defer tx.Rollback(ctx)
for i, su := range seed.Users {
u, err := pg.CreateUser(ctx, tx, su.Username)
if err != nil {
log.Printf("error creating user #%v/%s: %v", i+1, su.Username, err)
return err
}
_, err = pg.UpdateUser(ctx, tx, u.ID, su.DisplayName, su.Bio, nil, nil, &su.Links, nil, nil, nil)
if err != nil {
log.Printf("updating user %s: %v", su.Username, err)
return err
}
err = pg.SetUserNamesPronouns(ctx, tx, u.ID, db.NotNull(su.Names), db.NotNull(su.Pronouns))
if err != nil {
log.Printf("setting names/pronouns for user %s: %v", su.Username, err)
return err
}
err = pg.SetUserFields(ctx, tx, u.ID, db.NotNull(su.Fields))
if err != nil {
log.Printf("setting fields for user %s: %v", su.Username, err)
return err
}
log.Printf("creating members for user %s", su.Username)
for _, sm := range su.Members {
m, err := pg.CreateMember(ctx, tx, u.ID, sm.Name, sm.DisplayName, sm.Bio, db.NotNull(sm.Links))
if err != nil {
log.Printf("creating member %s: %v", sm.Name, err)
return err
}
err = pg.SetMemberNamesPronouns(ctx, tx, m.ID, db.NotNull(sm.Names), db.NotNull(sm.Pronouns))
if err != nil {
log.Printf("setting names/pronouns for member %s: %v", sm.Name, err)
return err
}
err = pg.SetMemberFields(ctx, tx, m.ID, db.NotNull(sm.Fields))
if err != nil {
log.Printf("setting fields for member %s: %v", sm.Name, err)
return err
}
log.Printf("created member %s", sm.Name)
}
log.Printf("created user %s", su.Username)
2022-11-19 18:11:01 -08:00
}
err = tx.Commit(ctx)
if err != nil {
2023-09-10 07:49:16 -07:00
log.Println("error committing transaction:", err)
return err
2022-11-19 18:11:01 -08:00
}
2023-09-10 07:49:16 -07:00
log.Printf("seeded database with %d users", len(seed.Users))
return nil
2022-11-19 18:11:01 -08:00
}