56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package table_analyzers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func PartitionRangeGenerator(
|
|
ctx context.Context,
|
|
tableAnalyzer etl.TableAnalyzer,
|
|
tableInfo config.TableInfo,
|
|
partitionColumn string,
|
|
rowsPerPartition int64,
|
|
jobRange config.RangeConfig,
|
|
) ([]models.Partition, error) {
|
|
if jobRange.Max > 0 {
|
|
return []models.Partition{{
|
|
Id: uuid.New(),
|
|
HasRange: true,
|
|
RetryCounter: 0,
|
|
Range: models.PartitionRange{
|
|
Min: jobRange.Min,
|
|
Max: jobRange.Max,
|
|
IsMinInclusive: jobRange.IsMinInclusive,
|
|
IsMaxInclusive: jobRange.IsMaxInclusive,
|
|
},
|
|
}}, nil
|
|
}
|
|
|
|
rowsCount, err := tableAnalyzer.EstimateTotalRows(ctx, tableInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if rowsCount <= rowsPerPartition {
|
|
return []models.Partition{{
|
|
Id: uuid.New(),
|
|
HasRange: false,
|
|
RetryCounter: 0,
|
|
}}, nil
|
|
|
|
}
|
|
|
|
partitionsCount := rowsCount / rowsPerPartition
|
|
partitions, err := tableAnalyzer.CalculatePartitionRanges(ctx, tableInfo, partitionColumn, partitionsCount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return partitions, nil
|
|
}
|