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

@@ -1,5 +1,12 @@
package db
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
)
type Module struct {
ID string
@@ -11,30 +18,98 @@ type Module struct {
Bugs string
Changelog string
Version string
Socket bool
Manifest string
Download string
Manifest string
Socket bool
Protected bool
Exclusive bool
PersistentStorage bool
CoreTranslation bool
Library bool
Availability int
Locked bool
Owned bool
HasStorage bool
Active bool
Availability int
DocumentTypes DocumentTypes
Relationships Relationships
Compatibility Compatibility
Authors []Author
Media []Media
Scripts []string
Esmodules []string
Styles []Style
Languages []Language
Packs []Pack
PackFolders []Folder
Tags []string
// ModulesFlags ModulesFlags `json:"flags,omitempty"`
Authors []*Author
Media []*Media
Styles []*Style
Languages []*Language
Packs []*Pack
PackFolders []*Folder
}
func (m *Module) Query(data *InsertId[uint]) {
data.query = fmt.Sprintf(`
INSERT INTO module (%s, id, title, description, url, license, readme, bugs,
changelog, version, manifest, download, socket, protected, exclusive_, persistent_storage,
core_translation, library, locked, owned, has_storage, active, availability)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17,
$18, $19, $20, $21, $22, $23)`, data.fieldName)
}
func (m *Module) InsertObjects(tx *sqlx.Tx) error {
relId := &InsertId[string]{id: m.ID, fieldName: "module_id"}
syncDB := NewSyncDB()
defer close(syncDB.errChan)
go InsertWithCtxParallel(syncDB, tx, m.DocumentTypes, relId)
go InsertWithCtxParallel(syncDB, tx, m.Relationships, relId)
go InsertWithCtxParallel(syncDB, tx, m.Compatibility, relId)
scriptRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "scripts"}
go InsertSimpleSliceParallel(syncDB, tx, m.Scripts, scriptRelId)
esModulesRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "es_modules"}
go InsertSimpleSliceParallel(syncDB, tx, m.Esmodules, esModulesRelId)
tagsRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "tags"}
go InsertSimpleSliceParallel(syncDB, tx, m.Tags, tagsRelId)
go InsertSliceParallel(syncDB, tx, m.Authors, relId)
go InsertSliceParallel(syncDB, tx, m.Media, relId)
go InsertSliceParallel(syncDB, tx, m.Styles, relId)
go InsertSliceParallel(syncDB, tx, m.Languages, relId)
go InsertSliceParallel(syncDB, tx, m.Packs, relId)
go InsertSliceParallel(syncDB, tx, m.PackFolders, relId)
return syncDB.Wait()
}
func (m *Module) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, m.ID, m.Title, m.Description, m.URL, m.License, m.Readme, m.Bugs,
m.Changelog, m.Version, m.Manifest, m.Download, m.Socket, m.Protected, m.Exclusive, m.PersistentStorage,
m.CoreTranslation, m.Library, m.Locked, m.Owned, m.HasStorage, m.Active, m.Availability}
_, err := tx.Exec(data.query, args...)
if err != nil {
return err
}
return m.InsertObjects(tx)
}
func (m *Module) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
if data.query == "" {
return ErrNoQuery
}
args := []any{data.id, m.ID, m.Title, m.Description, m.URL, m.License, m.Readme, m.Bugs,
m.Changelog, m.Version, m.Manifest, m.Download, m.Socket, m.Protected, m.Exclusive, m.PersistentStorage,
m.CoreTranslation, m.Library, m.Locked, m.Owned, m.HasStorage, m.Active, m.Availability}
_, err := tx.ExecContext(ctx, data.query, args...)
if err != nil {
return err
}
return m.InsertObjects(tx)
}