42 lines
897 B
Go
42 lines
897 B
Go
package extractors
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type GenericExtractor struct {
|
|
db dbwrapper.DbWrapper
|
|
}
|
|
|
|
func NewExtractor(db dbwrapper.DbWrapper) GenericExtractor {
|
|
return GenericExtractor{db: db}
|
|
}
|
|
|
|
func sendBatch(ctx context.Context, chBatchesOut chan<- models.Batch, batch models.Batch) error {
|
|
select {
|
|
case chBatchesOut <- batch:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func flush(
|
|
ctx context.Context,
|
|
batchSize int,
|
|
batchRows []models.UnknownRowValues,
|
|
chBatchesOut chan<- models.Batch,
|
|
) error {
|
|
if len(batchRows) == 0 {
|
|
return nil
|
|
}
|
|
|
|
batch := models.Batch{Id: uuid.New(), Rows: batchRows}
|
|
batchRows = make([]models.UnknownRowValues, 0, batchSize)
|
|
return sendBatch(ctx, chBatchesOut, batch)
|
|
}
|