finish setup insert method, refactor sql migration file

This commit is contained in:
lbenedar
2026-04-17 17:33:21 +03:00
parent 1b7c7e9ae3
commit 21abe68858
47 changed files with 2054 additions and 1056 deletions

View File

@@ -2,7 +2,7 @@ package db
import (
"context"
"sync"
"strconv"
"time"
"github.com/jmoiron/sqlx"
@@ -14,19 +14,45 @@ type Setup struct {
IsAdmin bool
IsSetup bool
CoreUpdate CoreUpdate //+
FeaturedContent FeaturedContent //+
Files Files //+
Options *SetupOptions //+
Release Release //+
Languages []SetupLanguage //+
CoreUpdate CoreUpdate
FeaturedContent FeaturedContent
Files Files
Options *SetupOptions
Release Release
Languages []*SetupLanguage
Modules []*Module
News []News //+
News []*News
PackageWarnings []*PackageWarning
Systems []*System
Worlds []*World
}
func (s *Setup) InsertObjects(tx *sqlx.Tx) error {
relData := &InsertId[uint]{id: s.ID, fieldName: "setup_id"}
syncDB := NewSyncDB()
defer close(syncDB.errChan)
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)
InsertSliceParallel(syncDB, tx, s.Languages, relData)
InsertSliceParallel(syncDB, tx, s.Modules, relData)
InsertSliceParallel(syncDB, tx, s.News, relData)
InsertSliceParallel(syncDB, tx, s.PackageWarnings, relData)
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)
return syncDB.Wait()
}
func (s *Setup) Insert(db *sqlx.DB) error {
tx := db.MustBegin()
defer tx.Rollback()
@@ -45,23 +71,8 @@ func (s *Setup) Insert(db *sqlx.DB) error {
if err != nil {
return err
}
id := InsertId[uint]{id: s.ID, fieldName: "setup_id"}
dataSync := SyncDbOperations{
wg: sync.WaitGroup{},
errChan: make(chan error),
}
defer close(dataSync.errChan)
go InsertSliceParallel(&dataSync, tx, s.News, &id)
go InsertSliceParallel(&dataSync, tx, s.Languages, &id)
InsertWithCtx(tx, &s.FeaturedContent, &id)
InsertWithCtx(tx, &s.Files, &id)
InsertWithCtx(tx, &s.Release, &id)
InsertWithCtx(tx, &s.CoreUpdate, &id)
err = dataSync.Wait()
err = s.InsertObjects(tx)
if err != nil {
return err
}
@@ -78,15 +89,21 @@ type FeaturedContent struct {
Image string
}
func (f *FeaturedContent) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
const query = `
func (f *FeaturedContent) Query(data *InsertId[uint]) {
data.query = `
INSERT INTO featured_content (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
}
args := []any{setup.id, f.Title, f.Caption, f.URL, f.Image}
func (f *FeaturedContent) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
err := tx.QueryRowx(query, args...).Scan(&f.ID)
args := []any{data.id, f.Title, f.Caption, f.URL, f.Image}
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
@@ -94,15 +111,14 @@ func (f *FeaturedContent) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
return nil
}
func (f *FeaturedContent) InsertCtx(ctx context.Context, tx *sqlx.Tx, setup *InsertId[uint]) error {
const query = `
INSERT INTO featured_content (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
func (f *FeaturedContent) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{setup.id, f.Title, f.Caption, f.URL, f.Image}
args := []any{data.id, f.Title, f.Caption, f.URL, f.Image}
err := tx.QueryRowxContext(ctx, query, args...).Scan(&f.ID)
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
if err != nil {
return err
}
@@ -119,15 +135,21 @@ type News struct {
Image string
}
func (n News) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
const insertNews = `
func (n *News) Query(data *InsertId[uint]) {
data.query = `
INSERT INTO news (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
}
args := []any{setup.id, n.Title, n.Caption, n.URL, n.Image}
func (n *News) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
err := tx.QueryRow(insertNews, args...).Scan(&n.ID)
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
err := tx.QueryRow(data.query, args...).Scan(&n.ID)
if err != nil {
return err
}
@@ -135,15 +157,14 @@ func (n News) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
return nil
}
func (n News) InsertCtx(ctx context.Context, tx *sqlx.Tx, setup *InsertId[uint]) error {
const insertNews = `
INSERT INTO news (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
func (n *News) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{setup.id, n.Title, n.Caption, n.URL, n.Image}
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
err := tx.QueryRowContext(ctx, insertNews, args...).Scan(&n.ID)
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&n.ID)
if err != nil {
return err
}