104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
_ "github.com/microsoft/go-mssqldb"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type UnknownRowValues = []any
|
|
|
|
func extractFromMssql(ctx context.Context, job MigrationJob, columns []ColumnType, chunkSize int, db *sql.DB, out chan<- []UnknownRowValues) error {
|
|
query := buildExtractQueryMssql(job, columns)
|
|
log.Debug("Query used to extract data from mssql: ", query)
|
|
|
|
queryStartTime := time.Now()
|
|
rows, err := db.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
log.Debugf("Query executed in %v", time.Since(queryStartTime))
|
|
|
|
rowsChunk := make([]UnknownRowValues, 0, chunkSize)
|
|
totalRowsExtracted := 0
|
|
chunkCount := 0
|
|
chunkStartTime := time.Now()
|
|
|
|
for rows.Next() {
|
|
values := make([]any, len(columns))
|
|
scanArgs := make([]any, len(columns))
|
|
|
|
for i := range values {
|
|
scanArgs[i] = &values[i]
|
|
}
|
|
|
|
if err := rows.Scan(scanArgs...); err != nil {
|
|
return err
|
|
}
|
|
|
|
rowsChunk = append(rowsChunk, values)
|
|
totalRowsExtracted++
|
|
|
|
if len(rowsChunk) >= chunkSize {
|
|
chunkCount++
|
|
chunkDuration := time.Since(chunkStartTime)
|
|
rowsPerSec := float64(chunkSize) / chunkDuration.Seconds()
|
|
log.Infof("Extracted chunk #%d: %d rows in %v (%.0f rows/sec) - Total: %d rows", chunkCount, len(rowsChunk), chunkDuration, rowsPerSec, totalRowsExtracted)
|
|
out <- rowsChunk
|
|
rowsChunk = make([]UnknownRowValues, 0, chunkSize)
|
|
chunkStartTime = time.Now()
|
|
}
|
|
}
|
|
|
|
if len(rowsChunk) > 0 {
|
|
chunkCount++
|
|
chunkDuration := time.Since(chunkStartTime)
|
|
rowsPerSec := float64(len(rowsChunk)) / chunkDuration.Seconds()
|
|
log.Infof("Extracted final chunk #%d: %d rows in %v (%.0f rows/sec) - Total: %d rows",
|
|
chunkCount, len(rowsChunk), chunkDuration, rowsPerSec, totalRowsExtracted)
|
|
out <- rowsChunk
|
|
}
|
|
|
|
return rows.Err()
|
|
}
|
|
|
|
func extractFromPostgres(ctx context.Context, job MigrationJob, columns []ColumnType, chunkSize int, db *pgxpool.Pool, out chan<- []UnknownRowValues) error {
|
|
query := buildExtractQueryPostgres(job, columns)
|
|
log.Debug("Query used to extract data from postgres: ", query)
|
|
|
|
rows, err := db.Query(ctx, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
|
|
rowsChunk := make([]UnknownRowValues, 0, chunkSize)
|
|
|
|
for rows.Next() {
|
|
values, err := rows.Values()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rowsChunk = append(rowsChunk, values)
|
|
|
|
if len(rowsChunk) >= chunkSize {
|
|
out <- rowsChunk
|
|
rowsChunk = make([]UnknownRowValues, 0, chunkSize)
|
|
log.Infof("Chunk send... %+v", job)
|
|
}
|
|
}
|
|
|
|
if len(rowsChunk) > 0 {
|
|
out <- rowsChunk
|
|
log.Infof("Chunk send... %+v", job)
|
|
}
|
|
|
|
return nil
|
|
}
|