118 lines
2.2 KiB
Go
118 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func main() {
|
|
log.SetFormatter(&log.TextFormatter{
|
|
FullTimestamp: true,
|
|
TimestampFormat: time.StampMilli,
|
|
})
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
log.Info("Starting migration...")
|
|
rowValues, colNames, err := extractData()
|
|
|
|
if err != nil {
|
|
log.Fatal("Unexpected error when extracting data")
|
|
}
|
|
|
|
for index, row := range rowValues {
|
|
log.Debugf("Values for row %d", index+1)
|
|
for i, v := range row {
|
|
log.Debugf("%s: %v", colNames[i], v)
|
|
}
|
|
}
|
|
|
|
log.Info("Migration completed successfully!")
|
|
}
|
|
|
|
func extractData() ([][]any, []string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
|
|
pool, err := db.Connect(ctx, config.App.FromDb.Url)
|
|
defer db.Close(pool)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Info("Successfully connected to from_db")
|
|
limit := 2
|
|
|
|
/* This query will become dinamyc later */
|
|
sql := `
|
|
SELECT
|
|
id,
|
|
nombre_producto,
|
|
descripcion,
|
|
stock,
|
|
precio,
|
|
es_activo,
|
|
fecha_creacion,
|
|
ultima_actualizacion,
|
|
configuracion_json,
|
|
etiquetas,
|
|
binario_test,
|
|
ip_servidor,
|
|
rango_prueba
|
|
FROM test.migration_test
|
|
LIMIT $1;
|
|
`
|
|
|
|
rows, err := pool.Query(ctx, sql, limit)
|
|
if err != nil {
|
|
if !errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, nil, fmt.Errorf("Unexpected error: %w", err)
|
|
}
|
|
|
|
log.Warn("Unexpected error", err)
|
|
return [][]any{}, []string{}, nil
|
|
}
|
|
defer rows.Close()
|
|
|
|
cols := rows.FieldDescriptions()
|
|
for _, c := range cols {
|
|
log.Debugf("Column: %+v", c)
|
|
}
|
|
|
|
rowValues := make([][]any, 0, limit)
|
|
|
|
for rows.Next() {
|
|
values := make([]any, len(cols))
|
|
valuePtrs := make([]any, len(cols))
|
|
|
|
for i := range values {
|
|
valuePtrs[i] = &values[i]
|
|
}
|
|
|
|
rows.Scan(valuePtrs...)
|
|
|
|
rowValues = append(rowValues, values)
|
|
}
|
|
|
|
return rowValues, Map(cols, func(col pgconn.FieldDescription) string {
|
|
return col.Name
|
|
}), nil
|
|
}
|
|
|
|
func Map[T any, V any](input []T, mapper func(T) V) []V {
|
|
result := make([]V, len(input))
|
|
|
|
for i, v := range input {
|
|
result[i] = mapper(v)
|
|
}
|
|
|
|
return result
|
|
}
|