package main import ( "context" "database/sql" "sync" "time" "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config" "github.com/jackc/pgx/v5/pgxpool" log "github.com/sirupsen/logrus" ) func main() { configureLog() migrationConfig, err := config.ReadMigrationConfig() if err != nil { log.Fatalf("error leyendo configuracion: %v", err) } log.Debugf("Config: %+v", migrationConfig) startTime := time.Now() ctx, cancel := context.WithCancel(context.Background()) defer cancel() log.Info("=== Starting migration ===") sourceDb, targetDb, connError := connectToDatabases() if connError != nil { log.Fatal("Connection error: ", connError) } defer sourceDb.Close() defer targetDb.Close() processMigrationJobs(ctx, sourceDb, targetDb, migrationConfig.Jobs, migrationConfig.MaxParallelWorkers) totalDuration := time.Since(startTime) log.Infof("=== Migration completed successfully! ===") log.Infof("Total migration time: %v", totalDuration) } func processMigrationJobs( ctx context.Context, sourceDb *sql.DB, targetDb *pgxpool.Pool, jobs []config.Job, maxParallelWorkers int, ) { if len(jobs) == 0 { log.Info("No migration jobs configured") return } if maxParallelWorkers <= 0 { maxParallelWorkers = 1 } if maxParallelWorkers > len(jobs) { maxParallelWorkers = len(jobs) } log.Infof("Starting migration with %d parallel worker(s)", maxParallelWorkers) chJobs := make(chan config.Job, len(jobs)) var wgJobs sync.WaitGroup for i := range maxParallelWorkers { wgJobs.Go(func() { for job := range chJobs { log.Infof("[worker %d] >>> Processing job: %s.%s <<<", i, job.SourceTable.Schema, job.SourceTable.Table) processMigrationJob(ctx, sourceDb, targetDb, job) } }) } for _, job := range jobs { chJobs <- job } close(chJobs) wgJobs.Wait() }