diff --git a/scripts/mssql-copy-in/main.go b/scripts/mssql-copy-in/main.go index 1671861..14815a9 100644 --- a/scripts/mssql-copy-in/main.go +++ b/scripts/mssql-copy-in/main.go @@ -14,8 +14,8 @@ import ( const ( totalRows int = 1_000_000 chunkSize int = 50_000 - schema string = "Cartografia" - table string = "MANZANA" + schema string = "Red" + table string = "PUERTO" queueSize int = 4 ) @@ -39,7 +39,7 @@ func main() { var wgSeed sync.WaitGroup wgSeed.Go(func() { - seedManzanas(ctx, db) + seedPuertos(ctx, db) }) wgSeed.Wait() diff --git a/scripts/mssql-copy-in/puerto.go b/scripts/mssql-copy-in/puerto.go new file mode 100644 index 0000000..1e5b684 --- /dev/null +++ b/scripts/mssql-copy-in/puerto.go @@ -0,0 +1,227 @@ +package main + +import ( + "context" + "database/sql" + "math/rand" + "time" + + log "github.com/sirupsen/logrus" +) + +func seedPuertos(ctx context.Context, db *sql.DB) { + rowsChan := make(chan []UnknownRowValues, queueSize) + + // Column names for PUERTO table (excluding ID_PUERTO which is IDENTITY) + colNames := []string{ + "ID_EQUIPO", + "ID_TERMINAL", + "ID_TIPO_EQUIPO", + "ID_PROYECTO_RESERVA", + "ID_TIPO_PUERTO", + "NUMERO", + "CODIGO", + "ESTADO", + "FECHA_ALTA", + "FECHA_ACT", + "ID_SITE_HOLDER", + "ID_PROYECTO_RESERVA_INICIAL", + "ID_DIRECCION", + "ID_TIPO_PUERTO_MEMORY", + } + + // Start the data generator goroutine + go generatePuertoRows(ctx, totalRows, chunkSize, rowsChan) + + // Load rows into MSSQL + job := MigrationJob{ + Schema: "Red", + Table: "PUERTO", + } + + if err := loadRowsMssql(ctx, job, colNames, db, rowsChan); err != nil { + log.Fatal("Error loading PUERTO rows: ", err) + } + + log.Info("PUERTO data generation and loading completed successfully") +} + +// generatePuertoRows creates random row data for the PUERTO table and sends it through a channel +func generatePuertoRows( + ctx context.Context, + totalRows int, + chunkSize int, + out chan<- []UnknownRowValues, +) { + defer close(out) + + rand.Seed(time.Now().UnixNano()) + + rowsGenerated := 0 + currentChunk := make([]UnknownRowValues, 0, chunkSize) + + for i := 0; i < totalRows; i++ { + row := generatePuertoRow() + currentChunk = append(currentChunk, row) + rowsGenerated++ + + // Send chunk when it reaches the desired size + if len(currentChunk) == chunkSize { + select { + case out <- currentChunk: + log.Debugf("Sent PUERTO chunk with %d rows", len(currentChunk)) + case <-ctx.Done(): + log.Info("Context cancelled, stopping PUERTO row generation") + return + } + currentChunk = make([]UnknownRowValues, 0, chunkSize) + } + + if rowsGenerated%100_000 == 0 { + logPuertoSampleRow(rowsGenerated, row) + } + } + + // Send remaining rows + if len(currentChunk) > 0 { + select { + case out <- currentChunk: + log.Debugf("Sent final PUERTO chunk with %d rows", len(currentChunk)) + case <-ctx.Done(): + log.Info("Context cancelled, stopping PUERTO row generation") + } + } + + log.Infof("Finished generating %d PUERTO rows", rowsGenerated) +} + +// generatePuertoRow creates a single random row for the PUERTO table +func generatePuertoRow() UnknownRowValues { + dateLowerLimit, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59Z") + dateUpperLimit, _ := time.Parse(time.RFC3339, "2025-12-31T23:59:59Z") + + // Required columns + idEquipo := rand.Intn(10000) + 1 // ID_EQUIPO (1-10000) + idTipoEquipo := rand.Intn(100) + 1 // ID_TIPO_EQUIPO (1-100) + idTipoPuerto := rand.Intn(50) + 1 // ID_TIPO_PUERTO (1-50) + numero := rand.Intn(1000) + 1 // NUMERO (1-1000) + codigo := generateRandomString(100) // CODIGO: Random alphanumeric (up to 100 chars) + + // Optional columns - randomly decide whether to include NULL or a value + var idTerminal any + if rand.Intn(2) == 0 { + idTerminal = rand.Intn(5000) + 1 + } else { + idTerminal = nil + } + + var idProyectoReserva any + if rand.Intn(2) == 0 { + idProyectoReserva = rand.Intn(1000) + 1 + } else { + idProyectoReserva = nil + } + + var estado any + if rand.Intn(2) == 0 { + estados := []string{"ACTIVO", "LIBRE", "DISPONIBLE", "MANTENIMIENTO", "RESERVADO"} + estado = estados[rand.Intn(len(estados))] + } else { + estado = nil + } + + var fechaAlta any + if rand.Intn(2) == 0 { + fechaAlta = generateRandomTimestamp(dateLowerLimit, dateUpperLimit) + } else { + fechaAlta = nil + } + + var fechaAct any + if rand.Intn(2) == 0 { + fechaAct = generateRandomTimestamp(dateLowerLimit, dateUpperLimit) + } else { + fechaAct = nil + } + + var idSiteHolder any + if rand.Intn(2) == 0 { + idSiteHolder = rand.Intn(500) + 1 + } else { + idSiteHolder = nil + } + + var idProyectoReservaInicial any + if rand.Intn(2) == 0 { + idProyectoReservaInicial = rand.Intn(1000) + 1 + } else { + idProyectoReservaInicial = nil + } + + var idDireccion any + if rand.Intn(2) == 0 { + idDireccion = rand.Intn(100) + 1 + } else { + idDireccion = nil + } + + var idTipoPuertoMemory any + if rand.Intn(2) == 0 { + idTipoPuertoMemory = rand.Intn(50) + 1 + } else { + idTipoPuertoMemory = nil + } + + return UnknownRowValues{ + idEquipo, + idTerminal, + idTipoEquipo, + idProyectoReserva, + idTipoPuerto, + numero, + codigo, + estado, + fechaAlta, + fechaAct, + idSiteHolder, + idProyectoReservaInicial, + idDireccion, + idTipoPuertoMemory, + } +} + +func logPuertoSampleRow(id int, rowValues UnknownRowValues) { + log.Infof(` +Sample row #%d: +ID_EQUIPO (%T): %v +ID_TERMINAL (%T): %v +ID_TIPO_EQUIPO (%T): %v +ID_PROYECTO_RESERVA (%T): %v +ID_TIPO_PUERTO (%T): %v +NUMERO (%T): %v +CODIGO (%T): %v +ESTADO (%T): %v +FECHA_ALTA (%T): %v +FECHA_ACT (%T): %v +ID_SITE_HOLDER (%T): %v +ID_PROYECTO_RESERVA_INICIAL (%T): %v +ID_DIRECCION (%T): %v +ID_TIPO_PUERTO_MEMORY (%T): %v +`, + id, + rowValues[0], rowValues[0], + rowValues[1], rowValues[1], + rowValues[2], rowValues[2], + rowValues[3], rowValues[3], + rowValues[4], rowValues[4], + rowValues[5], rowValues[5], + rowValues[6], rowValues[6], + rowValues[7], rowValues[7], + rowValues[8], rowValues[8], + rowValues[9], rowValues[9], + rowValues[10], rowValues[10], + rowValues[11], rowValues[11], + rowValues[12], rowValues[12], + rowValues[13], rowValues[13], + ) +}