feat: add extractData method for retrieving data from postgresql
This commit is contained in:
@@ -1,8 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"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"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,7 +18,100 @@ func main() {
|
|||||||
FullTimestamp: true,
|
FullTimestamp: true,
|
||||||
TimestampFormat: time.StampMilli,
|
TimestampFormat: time.StampMilli,
|
||||||
})
|
})
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
|
||||||
log.Info("Starting migration...")
|
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!")
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user