mirror of
				https://github.com/ClusterCockpit/cc-backend
				synced 2025-11-04 01:25:06 +01:00 
			
		
		
		
	Cleanup transaction api
This commit is contained in:
		internal
@@ -16,6 +16,11 @@ import (
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/schema"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	addTagQuery = "INSERT INTO tag (tag_name, tag_type) VALUES (?, ?)"
 | 
			
		||||
	setTagQuery = "INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Delete the tables "job", "tag" and "jobtag" from the database and
 | 
			
		||||
// repopulate them using the jobs found in `archive`.
 | 
			
		||||
func InitDB() error {
 | 
			
		||||
@@ -27,7 +32,7 @@ func InitDB() error {
 | 
			
		||||
	starttime := time.Now()
 | 
			
		||||
	log.Print("Building job table...")
 | 
			
		||||
 | 
			
		||||
	t, err := r.TransactionInit(repository.NamedJobInsert)
 | 
			
		||||
	t, err := r.TransactionInit()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Warn("Error while initializing SQL transactions")
 | 
			
		||||
		return err
 | 
			
		||||
@@ -105,7 +110,8 @@ func InitDB() error {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		id, err := r.TransactionAdd(t, job)
 | 
			
		||||
		id, err := r.TransactionAddNamed(t,
 | 
			
		||||
			repository.NamedJobInsert, job)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Errorf("repository initDB(): %v", err)
 | 
			
		||||
			errorOccured++
 | 
			
		||||
@@ -116,7 +122,9 @@ func InitDB() error {
 | 
			
		||||
			tagstr := tag.Name + ":" + tag.Type
 | 
			
		||||
			tagId, ok := tags[tagstr]
 | 
			
		||||
			if !ok {
 | 
			
		||||
				tagId, err = r.TransactionAddTag(t, tag)
 | 
			
		||||
				tagId, err = r.TransactionAdd(t,
 | 
			
		||||
					addTagQuery,
 | 
			
		||||
					tag.Name, tag.Type)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Errorf("Error adding tag: %v", err)
 | 
			
		||||
					errorOccured++
 | 
			
		||||
@@ -125,7 +133,9 @@ func InitDB() error {
 | 
			
		||||
				tags[tagstr] = tagId
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			r.TransactionSetTag(t, id, tagId)
 | 
			
		||||
			r.TransactionAdd(t,
 | 
			
		||||
				setTagQuery,
 | 
			
		||||
				id, tagId)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err == nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,6 @@ package repository
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/log"
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/schema"
 | 
			
		||||
	"github.com/jmoiron/sqlx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -15,7 +14,7 @@ type Transaction struct {
 | 
			
		||||
	stmt *sqlx.NamedStmt
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *JobRepository) TransactionInit(sqlStmt string) (*Transaction, error) {
 | 
			
		||||
func (r *JobRepository) TransactionInit() (*Transaction, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	t := new(Transaction)
 | 
			
		||||
 | 
			
		||||
@@ -24,13 +23,6 @@ func (r *JobRepository) TransactionInit(sqlStmt string) (*Transaction, error) {
 | 
			
		||||
		log.Warn("Error while bundling transactions")
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t.stmt, err = t.tx.PrepareNamed(sqlStmt)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Warn("Error while preparing SQL statement in transaction")
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return t, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -49,7 +41,6 @@ func (r *JobRepository) TransactionCommit(t *Transaction) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t.stmt = t.tx.NamedStmt(t.stmt)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -62,10 +53,14 @@ func (r *JobRepository) TransactionEnd(t *Transaction) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *JobRepository) TransactionAdd(t *Transaction, obj interface{}) (int64, error) {
 | 
			
		||||
	res, err := t.stmt.Exec(obj)
 | 
			
		||||
func (r *JobRepository) TransactionAddNamed(
 | 
			
		||||
	t *Transaction,
 | 
			
		||||
	query string,
 | 
			
		||||
	args ...interface{},
 | 
			
		||||
) (int64, error) {
 | 
			
		||||
	res, err := t.tx.NamedExec(query, args)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Errorf("repository initDB(): %v", err)
 | 
			
		||||
		log.Errorf("Named Exec failed: %v", err)
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -78,26 +73,14 @@ func (r *JobRepository) TransactionAdd(t *Transaction, obj interface{}) (int64,
 | 
			
		||||
	return id, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *JobRepository) TransactionAddTag(t *Transaction, tag *schema.Tag) (int64, error) {
 | 
			
		||||
	res, err := t.tx.Exec(`INSERT INTO tag (tag_name, tag_type) VALUES (?, ?)`, tag.Name, tag.Type)
 | 
			
		||||
func (r *JobRepository) TransactionAdd(t *Transaction, query string, args ...interface{}) (int64, error) {
 | 
			
		||||
	res := t.tx.MustExec(query, args)
 | 
			
		||||
 | 
			
		||||
	id, err := res.LastInsertId()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Errorf("Error while inserting tag into tag table: %v (Type %v)", tag.Name, tag.Type)
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	tagId, err := res.LastInsertId()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Warn("Error while getting last insert ID")
 | 
			
		||||
		log.Errorf("repository initDB(): %v", err)
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return tagId, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *JobRepository) TransactionSetTag(t *Transaction, jobId int64, tagId int64) error {
 | 
			
		||||
	if _, err := t.tx.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, jobId, tagId); err != nil {
 | 
			
		||||
		log.Errorf("Error while inserting jobtag into jobtag table: %v (TagID %v)", jobId, tagId)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
	return id, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user