github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/mysql/migrations/batch.go (about)

     1  package migrations
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  )
     8  
     9  type templatedStatement func(tx *tables) string
    10  
    11  type statementBatch struct {
    12  	statements []templatedStatement
    13  }
    14  
    15  func newStatementBatch(statements ...templatedStatement) statementBatch {
    16  	return statementBatch{
    17  		statements: statements,
    18  	}
    19  }
    20  
    21  func (e statementBatch) execute(ctx context.Context, wrapper TxWrapper) error {
    22  	if len(e.statements) == 0 {
    23  		return errors.New("executor.migrate: No statements to migrate")
    24  	}
    25  
    26  	for _, stmt := range e.statements {
    27  		if _, err := wrapper.tx.ExecContext(ctx, stmt(wrapper.tables)); err != nil {
    28  			return fmt.Errorf("statementBatch.execute: failed to exec statement: %w", err)
    29  		}
    30  	}
    31  
    32  	return nil
    33  }