mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 04:44:37 +03:00
Data layer restructuring (#997)
* Move query builders to sqlite package * Add transaction system * Wrap model resolvers in transaction * Add error return value for StringSliceToIntSlice * Update/refactor mutation resolvers * Convert query builders * Remove unused join types * Add stash id unit tests * Use WAL journal mode
This commit is contained in:
195
pkg/sqlite/transaction.go
Normal file
195
pkg/sqlite/transaction.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stashapp/stash/pkg/database"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
)
|
||||
|
||||
type dbi interface {
|
||||
Get(dest interface{}, query string, args ...interface{}) error
|
||||
Select(dest interface{}, query string, args ...interface{}) error
|
||||
Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
|
||||
NamedExec(query string, arg interface{}) (sql.Result, error)
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
}
|
||||
|
||||
type transaction struct {
|
||||
Ctx context.Context
|
||||
tx *sqlx.Tx
|
||||
}
|
||||
|
||||
func (t *transaction) Begin() error {
|
||||
if t.tx != nil {
|
||||
return errors.New("transaction already begun")
|
||||
}
|
||||
|
||||
var err error
|
||||
t.tx, err = database.DB.BeginTxx(t.Ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error starting transaction: %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *transaction) Rollback() error {
|
||||
if t.tx == nil {
|
||||
return errors.New("not in transaction")
|
||||
}
|
||||
|
||||
err := t.tx.Rollback()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error rolling back transaction: %s", err.Error())
|
||||
}
|
||||
t.tx = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *transaction) Commit() error {
|
||||
if t.tx == nil {
|
||||
return errors.New("not in transaction")
|
||||
}
|
||||
|
||||
err := t.tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error committing transaction: %s", err.Error())
|
||||
}
|
||||
t.tx = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *transaction) Repository() models.Repository {
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *transaction) ensureTx() {
|
||||
if t.tx == nil {
|
||||
panic("tx is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func (t *transaction) Gallery() models.GalleryReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewGalleryReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Image() models.ImageReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewImageReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Movie() models.MovieReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewMovieReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Performer() models.PerformerReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewPerformerReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) SceneMarker() models.SceneMarkerReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewSceneMarkerReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Scene() models.SceneReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewSceneReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) ScrapedItem() models.ScrapedItemReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewScrapedItemReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Studio() models.StudioReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewStudioReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
func (t *transaction) Tag() models.TagReaderWriter {
|
||||
t.ensureTx()
|
||||
return NewTagReaderWriter(t.tx)
|
||||
}
|
||||
|
||||
type ReadTransaction struct{}
|
||||
|
||||
func (t *ReadTransaction) Begin() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Rollback() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Commit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Repository() models.ReaderRepository {
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Gallery() models.GalleryReader {
|
||||
return NewGalleryReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Image() models.ImageReader {
|
||||
return NewImageReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Movie() models.MovieReader {
|
||||
return NewMovieReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Performer() models.PerformerReader {
|
||||
return NewPerformerReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) SceneMarker() models.SceneMarkerReader {
|
||||
return NewSceneMarkerReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Scene() models.SceneReader {
|
||||
return NewSceneReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) ScrapedItem() models.ScrapedItemReader {
|
||||
return NewScrapedItemReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Studio() models.StudioReader {
|
||||
return NewStudioReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
func (t *ReadTransaction) Tag() models.TagReader {
|
||||
return NewTagReaderWriter(database.DB)
|
||||
}
|
||||
|
||||
type TransactionManager struct {
|
||||
// only allow one write transaction at a time
|
||||
c chan struct{}
|
||||
}
|
||||
|
||||
func NewTransactionManager() *TransactionManager {
|
||||
return &TransactionManager{
|
||||
c: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TransactionManager) WithTxn(ctx context.Context, fn func(r models.Repository) error) error {
|
||||
return models.WithTxn(&transaction{Ctx: ctx}, fn)
|
||||
}
|
||||
|
||||
func (t *TransactionManager) WithReadTxn(ctx context.Context, fn func(r models.ReaderRepository) error) error {
|
||||
return models.WithROTxn(&ReadTransaction{}, fn)
|
||||
}
|
||||
Reference in New Issue
Block a user