finish setup data insertion, add world insertion
This commit is contained in:
@@ -2,10 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Setup struct {
|
||||
@@ -28,29 +31,32 @@ type Setup struct {
|
||||
}
|
||||
|
||||
func (s *Setup) InsertObjects(tx *sqlx.Tx) error {
|
||||
relData := &InsertId[uint]{id: s.ID, fieldName: "setup_id"}
|
||||
syncDB := NewSyncDB()
|
||||
defer close(syncDB.errChan)
|
||||
relData := InsertId[uint]{id: s.ID, fieldName: "setup_id"}
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
InsertWithCtxParallel(syncDB, tx, &s.CoreUpdate, relData)
|
||||
InsertWithCtxParallel(syncDB, tx, &s.FeaturedContent, relData)
|
||||
InsertWithCtxParallel(syncDB, tx, &s.Files, relData)
|
||||
InsertWithCtxParallel(syncDB, tx, s.Options, relData)
|
||||
InsertWithCtxParallel(syncDB, tx, &s.Release, relData)
|
||||
InsertWithCtxParallel(group, ctx, tx, &s.CoreUpdate, relData)
|
||||
InsertWithCtxParallel(group, ctx, tx, &s.FeaturedContent, relData)
|
||||
InsertWithCtxParallel(group, ctx, tx, &s.Files, relData)
|
||||
InsertWithCtxParallel(group, ctx, tx, s.Options, relData)
|
||||
InsertWithCtxParallel(group, ctx, tx, &s.Release, relData)
|
||||
|
||||
InsertSliceParallel(syncDB, tx, s.Languages, relData)
|
||||
InsertSliceParallel(syncDB, tx, s.Modules, relData)
|
||||
InsertSliceParallel(syncDB, tx, s.News, relData)
|
||||
InsertSliceParallel(syncDB, tx, s.PackageWarnings, relData)
|
||||
InsertSliceParallel(group, tx, s.Languages, relData)
|
||||
InsertSliceParallel(group, tx, s.Modules, relData)
|
||||
InsertSliceParallel(group, tx, s.News, relData)
|
||||
InsertSliceParallel(group, tx, s.PackageWarnings, relData)
|
||||
|
||||
relDataString := &InsertId[string]{
|
||||
relDataString := InsertId[string]{
|
||||
id: strconv.FormatUint(uint64(s.ID), 10),
|
||||
fieldName: "setup_id",
|
||||
}
|
||||
InsertSliceParallel(syncDB, tx, s.Systems, relDataString)
|
||||
InsertSliceParallel(syncDB, tx, s.Worlds, relDataString)
|
||||
InsertSliceParallel(group, tx, s.Systems, relDataString)
|
||||
InsertSliceParallel(group, tx, s.Worlds, relDataString)
|
||||
|
||||
return syncDB.Wait()
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Setup) Insert(db *sqlx.DB) error {
|
||||
@@ -67,7 +73,7 @@ func (s *Setup) Insert(db *sqlx.DB) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := tx.QueryRowContext(ctx, query, args...).Scan(&s.ID, &s.CreatedAt)
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&s.ID, &s.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -149,7 +155,7 @@ func (n *News) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
|
||||
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
||||
|
||||
err := tx.QueryRow(data.query, args...).Scan(&n.ID)
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&n.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -164,7 +170,7 @@ func (n *News) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint])
|
||||
|
||||
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
||||
|
||||
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&n.ID)
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&n.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user