feat: add mssql data extraction script
This commit is contained in:
85
scripts/mssql-test/main.go
Normal file
85
scripts/mssql-test/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||
_ "github.com/microsoft/go-mssqldb"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: time.StampMilli,
|
||||
})
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
db, err := sql.Open("sqlserver", config.App.SourceDbUrl)
|
||||
if err != nil {
|
||||
log.Fatal("Unexpected error connecting to mssql", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
schema := "test"
|
||||
table := "migration_test"
|
||||
colNames := []string{"id", "nombre_producto", "descripcion", "stock", "precio", "es_activo", "fecha_creacion", "ultima_actualizacion", "configuracion_json", "etiquetas", "binario_test", "ip_servidor", "rango_prueba_min", "rango_prueba_max"}
|
||||
|
||||
query := buildExtractSqlSentence(schema, table, colNames)
|
||||
|
||||
rows, err := db.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
log.Fatal("Unexpected error extracting data", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
cols, _ := rows.Columns()
|
||||
|
||||
values := make([]any, len(cols))
|
||||
scanArgs := make([]any, len(cols))
|
||||
for i := range values {
|
||||
scanArgs[i] = &values[i]
|
||||
}
|
||||
|
||||
count := 0
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(scanArgs...); err != nil {
|
||||
log.Fatal("Error scan", err)
|
||||
}
|
||||
|
||||
count++
|
||||
if count%100000 == 0 {
|
||||
log.Infof("Procesadas %d filas...\n", count)
|
||||
}
|
||||
|
||||
if count < 2 {
|
||||
log.Debugf("Processed row (%d):", count)
|
||||
|
||||
for i, col := range cols {
|
||||
log.Debugf("%s: %v", col, values[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("Rows processed %d", count)
|
||||
}
|
||||
|
||||
func buildExtractSqlSentence(schema, table string, colNames []string) string {
|
||||
var sbColumns strings.Builder
|
||||
|
||||
for i, col := range colNames {
|
||||
sbColumns.WriteString(`[`)
|
||||
sbColumns.WriteString(col)
|
||||
sbColumns.WriteString(`]`)
|
||||
if i < len(colNames)-1 {
|
||||
sbColumns.WriteString(", ")
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`SELECT %s FROM [%s].[%s] WITH (NOLOCK)`, sbColumns.String(), schema, table)
|
||||
}
|
||||
Reference in New Issue
Block a user