package infrastructure import ( "context" "fmt" "github.com/google/uuid" "github.com/jackc/pgx/v4" "go.uber.org/zap" "serverctl/pkg/application/users" "serverctl/pkg/db" ) func AddSeedData(database *db.Client, logger *zap.Logger) { conn := database.GetConn(context.Background()) defer conn.Release() var numRows int err := conn.QueryRow(context.Background(), "select count(id) from sctl_user").Scan(&numRows) if err != nil { panic(err) } if numRows == 0 { addTestData(database, logger) } } func addTestData(database *db.Client, logger *zap.Logger) { ctx := context.Background() for jobs := 0; jobs < 10; jobs++ { go func(batchNr int) { conn := database.GetConn(ctx) defer conn.Release() batch := &pgx.Batch{} numInserts := 5_000 for i := 0; i < numInserts; i++ { var ( user *users.CreateUser err error ) user, err = users.NewCreateUser(fmt.Sprintf("%s@test.com", uuid.New().String()), "password", users.NewPlainTextPasswordHasher()) if err != nil { panic(err) } batch.Queue("INSERT INTO sctl_user(email, password_hash) values ($1, $2)", user.Email, user.PasswordHash) } res := conn.SendBatch(ctx, batch) for i := 0; i < numInserts; i++ { _, err := res.Exec() if err != nil { return } } logger.Debug("sent batch", zap.Int("batchId", batchNr)) }(jobs) } }