finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
194
internal/foundry/models/db/setup.go
Normal file
194
internal/foundry/models/db/setup.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Setup struct {
|
||||
ID uint
|
||||
CreatedAt time.Time
|
||||
|
||||
IsAdmin bool
|
||||
IsSetup bool
|
||||
CoreUpdate CoreUpdate
|
||||
FeaturedContent FeaturedContent
|
||||
Files Files
|
||||
Options *SetupOptions
|
||||
Release Release
|
||||
Languages []*SetupLanguage
|
||||
Modules []*Module
|
||||
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"}
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
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(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]{
|
||||
id: strconv.FormatUint(uint64(s.ID), 10),
|
||||
fieldName: "setup_id",
|
||||
}
|
||||
InsertSliceParallel(group, tx, s.Systems, relDataString)
|
||||
InsertSliceParallel(group, tx, s.Worlds, relDataString)
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Setup) Insert(db *sqlx.DB) error {
|
||||
tx := db.MustBegin()
|
||||
defer tx.Rollback()
|
||||
|
||||
const query = `
|
||||
INSERT INTO setup (is_admin, is_setup)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, created_at`
|
||||
|
||||
args := []any{s.IsAdmin, s.IsSetup}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := tx.QueryRowxContext(ctx, query, args...).Scan(&s.ID, &s.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.InsertObjects(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
type FeaturedContent struct {
|
||||
ID uint
|
||||
|
||||
Title string
|
||||
Caption string
|
||||
URL string
|
||||
Image string
|
||||
}
|
||||
|
||||
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`
|
||||
}
|
||||
|
||||
func (f *FeaturedContent) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FeaturedContent) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, f.Title, f.Caption, f.URL, f.Image}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type News struct {
|
||||
ID uint
|
||||
|
||||
Title string
|
||||
Caption string
|
||||
URL string
|
||||
Image string
|
||||
}
|
||||
|
||||
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`
|
||||
}
|
||||
|
||||
func (n *News) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&n.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *News) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&n.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func HasSetup(db *sqlx.DB) (bool, error) {
|
||||
const query = `
|
||||
SELECT EXISTS(SELECT 1 FROM setup)`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var exists bool
|
||||
err := db.GetContext(ctx, &exists, query)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
Reference in New Issue
Block a user