2023-03-15 10:04:48 +01:00
|
|
|
package seeddb
|
2022-11-20 03:11:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-06-03 16:18:47 +02:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
2023-04-04 04:11:03 +02:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2022-11-20 03:11:01 +01:00
|
|
|
"github.com/joho/godotenv"
|
2023-03-15 10:04:48 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-11-20 03:11:01 +01:00
|
|
|
)
|
|
|
|
|
2023-03-15 10:04:48 +01:00
|
|
|
var Command = &cli.Command{
|
|
|
|
Name: "seed",
|
|
|
|
Usage: "Seed the database with test data",
|
|
|
|
Action: run,
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c *cli.Context) error {
|
2022-11-20 03:11:01 +01:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error loading .env file:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:04:48 +01:00
|
|
|
ctx := c.Context
|
2022-11-20 03:11:01 +01:00
|
|
|
|
2023-04-04 04:11:03 +02:00
|
|
|
pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
|
2022-11-20 03:11:01 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error opening database:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
defer pool.Close()
|
|
|
|
|
|
|
|
fmt.Println("opened database")
|
|
|
|
|
|
|
|
pg := &db.DB{Pool: pool}
|
|
|
|
|
|
|
|
tx, err := pg.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error beginning transaction:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := pg.CreateUser(ctx, tx, "test")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error creating user:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:24:38 +02:00
|
|
|
_, err = pg.UpdateUser(ctx, tx, u.ID, ptr("testing"), ptr("This is a bio!"), nil, ptr(false), &[]string{"https://pronouns.cc"}, nil, nil, nil)
|
2022-11-20 03:11:01 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error setting user info:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 02:07:46 +01:00
|
|
|
err = pg.SetUserNamesPronouns(ctx, tx, u.ID, []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "testing 1", Status: "favourite"},
|
|
|
|
{Value: "testing 2", Status: "okay"},
|
2023-03-08 02:07:46 +01:00
|
|
|
}, []db.PronounEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Pronouns: "it/it/its/its/itself", DisplayText: ptr("it/its"), Status: "favourite"},
|
|
|
|
{Pronouns: "they/them/their/theirs/themself", Status: "okay"},
|
2022-11-20 03:11:01 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error setting pronouns:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pg.SetUserFields(ctx, tx, u.ID, []db.Field{
|
|
|
|
{
|
2023-03-08 02:07:46 +01:00
|
|
|
Name: "Field 1",
|
|
|
|
Entries: []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "Favourite 1", Status: "favourite"},
|
|
|
|
{Value: "Okay 1", Status: "okay"},
|
|
|
|
{Value: "Jokingly 1", Status: "jokingly"},
|
|
|
|
{Value: "Friends only 1", Status: "friends_only"},
|
|
|
|
{Value: "Avoid 1", Status: "avoid"},
|
2023-03-08 02:07:46 +01:00
|
|
|
},
|
2022-11-20 03:11:01 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-08 02:07:46 +01:00
|
|
|
Name: "Field 2",
|
|
|
|
Entries: []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "Favourite 2", Status: "favourite"},
|
|
|
|
{Value: "Okay 2", Status: "okay"},
|
|
|
|
{Value: "Jokingly 2", Status: "jokingly"},
|
|
|
|
{Value: "Friends only 2", Status: "friends_only"},
|
|
|
|
{Value: "Avoid 2", Status: "avoid"},
|
2023-03-08 02:07:46 +01:00
|
|
|
},
|
2022-11-20 03:11:01 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-08 02:07:46 +01:00
|
|
|
Name: "Field 3",
|
|
|
|
Entries: []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "Favourite 3", Status: "favourite"},
|
|
|
|
{Value: "Okay 3", Status: "okay"},
|
|
|
|
{Value: "Jokingly 3", Status: "jokingly"},
|
|
|
|
{Value: "Friends only 3", Status: "friends_only"},
|
|
|
|
{Value: "Avoid 3", Status: "avoid"},
|
2023-03-08 02:07:46 +01:00
|
|
|
},
|
2022-11-20 03:11:01 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-08 02:07:46 +01:00
|
|
|
Name: "Field 4",
|
|
|
|
Entries: []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "Favourite 4", Status: "favourite"},
|
|
|
|
{Value: "Okay 4", Status: "okay"},
|
|
|
|
{Value: "Jokingly 4", Status: "jokingly"},
|
|
|
|
{Value: "Friends only 4", Status: "friends_only"},
|
|
|
|
{Value: "Avoid 4", Status: "avoid"},
|
2023-03-08 02:07:46 +01:00
|
|
|
},
|
2022-11-20 03:11:01 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-08 02:07:46 +01:00
|
|
|
Name: "Field 5",
|
|
|
|
Entries: []db.FieldEntry{
|
2023-04-21 16:35:13 +02:00
|
|
|
{Value: "Favourite 5", Status: "favourite"},
|
|
|
|
{Value: "Okay 5", Status: "okay"},
|
|
|
|
{Value: "Jokingly 5", Status: "jokingly"},
|
|
|
|
{Value: "Friends only 5", Status: "friends_only"},
|
|
|
|
{Value: "Avoid 5", Status: "avoid"},
|
2023-03-08 02:07:46 +01:00
|
|
|
},
|
2022-11-20 03:11:01 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error setting fields:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error committing transaction:", err)
|
2023-03-15 10:04:48 +01:00
|
|
|
return err
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Created testing user with ID", u.ID, "and name", u.Username)
|
2023-03-15 10:04:48 +01:00
|
|
|
return nil
|
2022-11-20 03:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ptr[T any](v T) *T {
|
|
|
|
return &v
|
|
|
|
}
|