44 lines
980 B
Go
44 lines
980 B
Go
package loaders
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
func (gl *GenericLoader) ProcessBatch(
|
|
ctx context.Context,
|
|
tableInfo config.TargetTableInfo,
|
|
colNames []string,
|
|
batch models.Batch,
|
|
) (int64, error) {
|
|
_, err := gl.db.SaveMassive(
|
|
ctx,
|
|
tableInfo.Schema,
|
|
tableInfo.Table,
|
|
colNames,
|
|
batch.Rows,
|
|
)
|
|
|
|
if err != nil {
|
|
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
|
if pgErr.Code == "23505" {
|
|
return 0, &custom_errors.JobError{
|
|
ShouldCancelJob: true,
|
|
Msg: fmt.Sprintf("Fatal error in table %s.%s", tableInfo.Schema, tableInfo.Table),
|
|
Prev: err,
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0, &custom_errors.LoaderError{Batch: batch, Msg: err.Error()}
|
|
}
|
|
|
|
return int64(len(batch.Rows)), nil
|
|
}
|