Compare commits
5 Commits
3b1371a270
...
refactor/e
| Author | SHA1 | Date | |
|---|---|---|---|
|
ec96532d04
|
|||
|
46597c4ffd
|
|||
|
15d1b96849
|
|||
|
73b65e2a3f
|
|||
|
|
1c3db39b21 |
@@ -1,24 +0,0 @@
|
|||||||
# Skill Registry — go-migrate
|
|
||||||
|
|
||||||
Generated: 2026-04-21
|
|
||||||
|
|
||||||
## Compact Rules
|
|
||||||
|
|
||||||
### Go conventions
|
|
||||||
- Use existing error wrapping pattern: `fmt.Errorf("context: %w", err)`
|
|
||||||
- Channel-based pipeline — keep goroutine lifecycle clean (close channels in correct order)
|
|
||||||
- No comments unless non-obvious WHY; no docstrings
|
|
||||||
- Prefer named returns only when it aids clarity in short functions
|
|
||||||
- Use `strings.EqualFold` for case-insensitive column name comparison
|
|
||||||
|
|
||||||
### Project conventions
|
|
||||||
- Config structs live in `internal/app/config/`
|
|
||||||
- ETL interfaces live in `internal/app/etl/types.go`
|
|
||||||
- Transformer implementations in `internal/app/etl/transformers/`
|
|
||||||
- Azure operations via `internal/app/azure/main.go`
|
|
||||||
- Per-job transformer creation (not shared) when job has storage config
|
|
||||||
|
|
||||||
## User Skills
|
|
||||||
| Trigger | Skill |
|
|
||||||
|---------|-------|
|
|
||||||
| sdd-* | SDD workflow skills |
|
|
||||||
14
.env.example
14
.env.example
@@ -1,12 +1,2 @@
|
|||||||
SOURCE_DB_URL=sqlserver://sa:password@localhost:1433?database=master&packet+size=32767&loc=UTC
|
PG_FROM_DB_URL=postgresql://postgres:password@localhost:5432/db
|
||||||
TARGET_DB_URL=postgresql://postgres:password@localhost:5432/db
|
PG_TO_DB_URL=postgresql://postgres:password@localhost:5432/db
|
||||||
|
|
||||||
LOG_LEVEL=INFO
|
|
||||||
|
|
||||||
AZ_STORAGE_ENABLED=false
|
|
||||||
AZ_ACCOUNT_NAME=
|
|
||||||
AZ_CONTAINER=
|
|
||||||
AZ_ACCOUNT_KEY=
|
|
||||||
AZ_USE_HTTPS=true
|
|
||||||
AZ_SERVICE_URL=
|
|
||||||
AZ_PREFIX=
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -27,5 +27,5 @@ go.work.sum
|
|||||||
|
|
||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
.vscode/
|
# .vscode/
|
||||||
.temp
|
.temp
|
||||||
|
|||||||
77
cmd/go_migrate/connect.go
Normal file
77
cmd/go_migrate/connect.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
_ "github.com/microsoft/go-mssqldb"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func connectToSqlServer() (*sql.DB, error) {
|
||||||
|
db, err := sql.Open("sqlserver", config.App.SourceDbUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Unable to connect to sqlserver: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := db.PingContext(ctx); err != nil {
|
||||||
|
return nil, fmt.Errorf("Unable to ping sqlserver: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectToPostgres() (*pgxpool.Pool, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, config.App.TargetDbUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Unable to connect to postgres: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := pool.Ping(ctx); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
return nil, fmt.Errorf("Unable to ping postgres: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pool, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectToDatabases() (*sql.DB, *pgxpool.Pool, error) {
|
||||||
|
var sourceDbErr, targetDbErr error
|
||||||
|
var sourceDb *sql.DB
|
||||||
|
var targetDb *pgxpool.Pool
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
wg.Go(func() {
|
||||||
|
sourceDb, sourceDbErr = connectToSqlServer()
|
||||||
|
if sourceDbErr != nil {
|
||||||
|
log.Error("Unable to connect to source db: ", sourceDbErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
wg.Go(func() {
|
||||||
|
targetDb, targetDbErr = connectToPostgres()
|
||||||
|
if targetDbErr != nil {
|
||||||
|
log.Error("Unable to connect to target db: ", targetDbErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
if sourceDbErr != nil || targetDbErr != nil {
|
||||||
|
return nil, nil, errors.New("Unable to connect to databases")
|
||||||
|
}
|
||||||
|
|
||||||
|
return sourceDb, targetDb, nil
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,13 +13,5 @@ func configureLog() {
|
|||||||
DisableSorting: false,
|
DisableSorting: false,
|
||||||
PadLevelText: true,
|
PadLevelText: true,
|
||||||
})
|
})
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
logLevelEnv := config.App.LogLevel
|
|
||||||
logLevel, err := log.ParseLevel(logLevelEnv)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("Nivel de log inválido '%s', usando INFO por defecto", logLevelEnv)
|
|
||||||
logLevel = log.InfoLevel
|
|
||||||
}
|
|
||||||
|
|
||||||
log.SetLevel(logLevel)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/azure"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/extractors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/extractors"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/loaders"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/loaders"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/transformers"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@@ -96,10 +95,10 @@ func processMigrationJobs(
|
|||||||
targetDb dbwrapper.DbWrapper,
|
targetDb dbwrapper.DbWrapper,
|
||||||
jobs []config.Job,
|
jobs []config.Job,
|
||||||
maxParallelWorkers int,
|
maxParallelWorkers int,
|
||||||
) []models.JobResult {
|
) []JobResult {
|
||||||
if len(jobs) == 0 {
|
if len(jobs) == 0 {
|
||||||
log.Info("No migration jobs configured")
|
log.Info("No migration jobs configured")
|
||||||
return []models.JobResult{}
|
return []JobResult{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if maxParallelWorkers <= 0 {
|
if maxParallelWorkers <= 0 {
|
||||||
@@ -112,23 +111,15 @@ func processMigrationJobs(
|
|||||||
|
|
||||||
log.Infof("Starting migration with %d parallel worker(s)", maxParallelWorkers)
|
log.Infof("Starting migration with %d parallel worker(s)", maxParallelWorkers)
|
||||||
|
|
||||||
chJobResults := make(chan models.JobResult, len(jobs))
|
chJobResults := make(chan JobResult, len(jobs))
|
||||||
chJobs := make(chan config.Job, len(jobs))
|
chJobs := make(chan config.Job, len(jobs))
|
||||||
var wgJobs sync.WaitGroup
|
var wgJobs sync.WaitGroup
|
||||||
|
|
||||||
sourceTableAnalyzer := table_analyzers.NewMssqlTableAnalyzer(sourceDb)
|
sourceTableAnalyzer := table_analyzers.NewMssqlTableAnalyzer(sourceDb)
|
||||||
targetTableAnalyzer := table_analyzers.NewPostgresTableAnalyzer(targetDb)
|
targetTableAnalyzer := table_analyzers.NewPostgresTableAnalyzer(targetDb)
|
||||||
extractor := extractors.NewMssqlExtractor(sourceDb)
|
extractor := extractors.NewMssqlExtractor(sourceDb)
|
||||||
loader := loaders.NewGenericLoader(targetDb)
|
transformer := transformers.NewMssqlTransformer()
|
||||||
|
loader := loaders.NewPostgresLoader(targetDb)
|
||||||
var azureClient *azure.Client
|
|
||||||
if config.App.AzureStorage.Enabled {
|
|
||||||
var err error
|
|
||||||
azureClient, err = azure.NewClient(config.App.AzureStorage)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to create Azure storage client: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range maxParallelWorkers {
|
for i := range maxParallelWorkers {
|
||||||
wgJobs.Go(func() {
|
wgJobs.Go(func() {
|
||||||
@@ -140,10 +131,9 @@ func processMigrationJobs(
|
|||||||
sourceTableAnalyzer,
|
sourceTableAnalyzer,
|
||||||
targetTableAnalyzer,
|
targetTableAnalyzer,
|
||||||
extractor,
|
extractor,
|
||||||
azureClient,
|
transformer,
|
||||||
loader,
|
loader,
|
||||||
job,
|
job,
|
||||||
targetDb.GetDialect(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
chJobResults <- res
|
chJobResults <- res
|
||||||
@@ -161,7 +151,7 @@ func processMigrationJobs(
|
|||||||
close(chJobResults)
|
close(chJobResults)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var finalResults []models.JobResult
|
var finalResults []JobResult
|
||||||
for res := range chJobResults {
|
for res := range chJobResults {
|
||||||
finalResults = append(finalResults, res)
|
finalResults = append(finalResults, res)
|
||||||
}
|
}
|
||||||
|
|||||||
13
cmd/go_migrate/metrics.go
Normal file
13
cmd/go_migrate/metrics.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type JobResult struct {
|
||||||
|
JobName string
|
||||||
|
StartTime time.Time
|
||||||
|
Duration time.Duration
|
||||||
|
RowsRead int64
|
||||||
|
RowsLoaded int64
|
||||||
|
RowsFailed int64
|
||||||
|
Error error
|
||||||
|
}
|
||||||
@@ -7,49 +7,31 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/azure"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/extractors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/extractors"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/transformers"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildTruncateQuery(targetDbType, schema, table, truncateMethod string) string {
|
|
||||||
if truncateMethod == "DELETE" {
|
|
||||||
if targetDbType == "postgres" {
|
|
||||||
return fmt.Sprintf(`DELETE FROM "%s"."%s"`, schema, table)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(`DELETE FROM [%s].[%s]`, schema, table)
|
|
||||||
}
|
|
||||||
|
|
||||||
if targetDbType == "postgres" {
|
|
||||||
return fmt.Sprintf(`TRUNCATE TABLE "%s"."%s"`, schema, table)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(`TRUNCATE TABLE [%s].[%s]`, schema, table)
|
|
||||||
}
|
|
||||||
|
|
||||||
func processMigrationJob(
|
func processMigrationJob(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
targetDbWrapper dbwrapper.DbWrapper,
|
targetDbWrapper dbwrapper.DbWrapper,
|
||||||
sourceTableAnalyzer etl.TableAnalyzer,
|
sourceTableAnalyzer etl.TableAnalyzer,
|
||||||
targetTableAnalyzer etl.TableAnalyzer,
|
targetTableAnalyzer etl.TableAnalyzer,
|
||||||
extractor etl.Extractor,
|
extractor etl.Extractor,
|
||||||
azureClient *azure.Client,
|
transformer etl.Transformer,
|
||||||
loader etl.Loader,
|
loader etl.Loader,
|
||||||
job config.Job,
|
job config.Job,
|
||||||
targetDbType string,
|
) JobResult {
|
||||||
) models.JobResult {
|
|
||||||
transformer := transformers.NewMssqlTransformer(job.ToStorage, job.SourceTable, azureClient)
|
|
||||||
localCtx, cancel := context.WithCancel(ctx)
|
localCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
result := models.JobResult{
|
result := JobResult{
|
||||||
JobName: job.Name,
|
JobName: job.Name,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
}
|
}
|
||||||
@@ -85,13 +67,7 @@ func processMigrationJob(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
preSqlQueries := job.TargetTable.PreSQL
|
for _, query := range job.PreSQL {
|
||||||
if job.TruncateTarget {
|
|
||||||
truncateQuery := buildTruncateQuery(targetDbType, job.TargetTable.Schema, job.TargetTable.Table, job.TruncateMethod)
|
|
||||||
preSqlQueries = append([]string{truncateQuery}, job.TargetTable.PreSQL...)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, query := range preSqlQueries {
|
|
||||||
if _, err := targetDbWrapper.Exec(localCtx, query); err != nil {
|
if _, err := targetDbWrapper.Exec(localCtx, query); err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
@@ -104,14 +80,12 @@ func processMigrationJob(
|
|||||||
job.SourceTable.TableInfo,
|
job.SourceTable.TableInfo,
|
||||||
job.SourceTable.PrimaryKey,
|
job.SourceTable.PrimaryKey,
|
||||||
job.RowsPerPartition,
|
job.RowsPerPartition,
|
||||||
job.Range,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unexpected error calculating batch ranges: ", err)
|
log.Error("Unexpected error calculating batch ranges: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
chJobErrors := make(chan custom_errors.JobError, job.QueueSize)
|
chJobErrors := make(chan custom_errors.JobError, job.QueueSize)
|
||||||
chExtractorErrors := make(chan custom_errors.ExtractorError, job.QueueSize)
|
|
||||||
chLoadersErrors := make(chan custom_errors.LoaderError, job.QueueSize)
|
chLoadersErrors := make(chan custom_errors.LoaderError, job.QueueSize)
|
||||||
chPartitions := make(chan models.Partition, job.QueueSize)
|
chPartitions := make(chan models.Partition, job.QueueSize)
|
||||||
chBatchesRaw := make(chan models.Batch, job.QueueSize)
|
chBatchesRaw := make(chan models.Batch, job.QueueSize)
|
||||||
@@ -131,15 +105,6 @@ func processMigrationJob(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go custom_errors.ExtractorErrorHandler(
|
|
||||||
localCtx,
|
|
||||||
job.Retry,
|
|
||||||
job.MaxPartitionErrrors,
|
|
||||||
chExtractorErrors,
|
|
||||||
chPartitions,
|
|
||||||
chJobErrors,
|
|
||||||
&wgActivePartitions,
|
|
||||||
)
|
|
||||||
go custom_errors.LoaderErrorHandler(
|
go custom_errors.LoaderErrorHandler(
|
||||||
localCtx,
|
localCtx,
|
||||||
job.Retry,
|
job.Retry,
|
||||||
@@ -163,7 +128,6 @@ func processMigrationJob(
|
|||||||
job.BatchSize,
|
job.BatchSize,
|
||||||
chPartitions,
|
chPartitions,
|
||||||
chBatchesRaw,
|
chBatchesRaw,
|
||||||
chExtractorErrors,
|
|
||||||
chJobErrors,
|
chJobErrors,
|
||||||
&wgActivePartitions,
|
&wgActivePartitions,
|
||||||
&rowsRead,
|
&rowsRead,
|
||||||
@@ -217,8 +181,6 @@ func processMigrationJob(
|
|||||||
log.Debugf("wgActivePartitions is empty (%v)", job.Name)
|
log.Debugf("wgActivePartitions is empty (%v)", job.Name)
|
||||||
close(chPartitions)
|
close(chPartitions)
|
||||||
log.Debugf("chPartitions is closed (%v)", job.Name)
|
log.Debugf("chPartitions is closed (%v)", job.Name)
|
||||||
close(chExtractorErrors)
|
|
||||||
log.Debugf("chExtractorErrors is closed (%v)", job.Name)
|
|
||||||
|
|
||||||
wgExtractors.Wait()
|
wgExtractors.Wait()
|
||||||
log.Debugf("wgExtractors is empty (%v)", job.Name)
|
log.Debugf("wgExtractors is empty (%v)", job.Name)
|
||||||
@@ -241,7 +203,7 @@ func processMigrationJob(
|
|||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, query := range job.TargetTable.PostSQL {
|
for _, query := range job.PostSQL {
|
||||||
if _, err := targetDbWrapper.Exec(localCtx, query); err != nil {
|
if _, err := targetDbWrapper.Exec(localCtx, query); err != nil {
|
||||||
result.Error = err
|
result.Error = err
|
||||||
return result
|
return result
|
||||||
|
|||||||
16
config.yaml
16
config.yaml
@@ -30,6 +30,7 @@ jobs:
|
|||||||
table: MANZANA
|
table: MANZANA
|
||||||
pre_sql:
|
pre_sql:
|
||||||
- 'SELECT 1'
|
- 'SELECT 1'
|
||||||
|
# - 'TRUNCATE TABLE "Cartografia"."MANZANA"'
|
||||||
range:
|
range:
|
||||||
min: 1000000
|
min: 1000000
|
||||||
max: 2000000
|
max: 2000000
|
||||||
@@ -47,19 +48,6 @@ jobs:
|
|||||||
table: PUERTO
|
table: PUERTO
|
||||||
pre_sql:
|
pre_sql:
|
||||||
- 'SELECT 1'
|
- 'SELECT 1'
|
||||||
|
# - 'TRUNCATE TABLE "Red"."PUERTO"'
|
||||||
post_sql:
|
post_sql:
|
||||||
- "SELECT 1"
|
- "SELECT 1"
|
||||||
|
|
||||||
- name: red_terminal__attach
|
|
||||||
source:
|
|
||||||
schema: Red
|
|
||||||
table: TERMINAL__ATTACH
|
|
||||||
primary_key: GDB_ARCHIVE_OID
|
|
||||||
target:
|
|
||||||
schema: Red
|
|
||||||
table: TERMINAL__ATTACH
|
|
||||||
to_storage:
|
|
||||||
columns:
|
|
||||||
- source: DATA
|
|
||||||
target: FILE_URL
|
|
||||||
mode: REFERENCE_ONLY # REFERENCE_ONLY | DUPLICATE_WITH_REF
|
|
||||||
|
|||||||
8
go.mod
8
go.mod
@@ -1,10 +1,8 @@
|
|||||||
module git.ksdemosapps.com/kylesoda/go-migrate
|
module git.ksdemosapps.com/kylesoda/go-migrate
|
||||||
|
|
||||||
go 1.26
|
go 1.25.7
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4
|
|
||||||
github.com/caarlos0/env/v11 v11.4.0
|
|
||||||
github.com/gaspardle/go-mssqlclrgeo v0.0.0-20160129143314-97ceabf987a4
|
github.com/gaspardle/go-mssqlclrgeo v0.0.0-20160129143314-97ceabf987a4
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/jackc/pgx/v5 v5.9.1
|
github.com/jackc/pgx/v5 v5.9.1
|
||||||
@@ -17,17 +15,15 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0 // indirect
|
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
|
|
||||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||||
github.com/shopspring/decimal v1.4.0 // indirect
|
github.com/shopspring/decimal v1.4.0 // indirect
|
||||||
golang.org/x/crypto v0.48.0 // indirect
|
golang.org/x/crypto v0.48.0 // indirect
|
||||||
golang.org/x/net v0.51.0 // indirect
|
|
||||||
golang.org/x/sys v0.41.0 // indirect
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
golang.org/x/text v0.34.0 // indirect
|
golang.org/x/text v0.34.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
11
go.sum
11
go.sum
@@ -4,14 +4,10 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1 h1:Hk5QBxZQC1jb2Fwj6mpz
|
|||||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1/go.mod h1:IYus9qsFobWIc2YVwe/WPjcnyCkPKtnHAqUYeebc8z0=
|
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1/go.mod h1:IYus9qsFobWIc2YVwe/WPjcnyCkPKtnHAqUYeebc8z0=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 h1:9iefClla7iYpfYWdzPCRDozdmndjTm8DXdpCzPajMgA=
|
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 h1:9iefClla7iYpfYWdzPCRDozdmndjTm8DXdpCzPajMgA=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2/go.mod h1:XtLgD3ZD34DAaVIIAyG3objl5DynM3CQ/vMcbBNJZGI=
|
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2/go.mod h1:XtLgD3ZD34DAaVIIAyG3objl5DynM3CQ/vMcbBNJZGI=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1 h1:/Zt+cDPnpC3OVDm/JKLOs7M2DKmLRIIp3XIx9pHHiig=
|
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1/go.mod h1:Ng3urmn6dYe8gnbCMoHHVl5APYz2txho3koEkV2o2HA=
|
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0 h1:E4MgwLBGeVB5f2MdcIVD3ELVAWpr+WD6MUe1i+tM/PA=
|
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0 h1:E4MgwLBGeVB5f2MdcIVD3ELVAWpr+WD6MUe1i+tM/PA=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0/go.mod h1:Y2b/1clN4zsAoUd/pgNAQHjLDnTis/6ROkUfyob6psM=
|
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.4.0/go.mod h1:Y2b/1clN4zsAoUd/pgNAQHjLDnTis/6ROkUfyob6psM=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0 h1:nCYfgcSyHZXJI8J0IWE5MsCGlb2xp9fJiXyxWgmOFg4=
|
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0 h1:nCYfgcSyHZXJI8J0IWE5MsCGlb2xp9fJiXyxWgmOFg4=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0/go.mod h1:ucUjca2JtSZboY8IoUqyQyuuXvwbMBVwFOm0vdQPNhA=
|
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0/go.mod h1:ucUjca2JtSZboY8IoUqyQyuuXvwbMBVwFOm0vdQPNhA=
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4 h1:jWQK1GI+LeGGUKBADtcH2rRqPxYB1Ljwms5gFA2LqrM=
|
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4/go.mod h1:8mwH4klAm9DUgR2EEHyEEAQlRDvLPyg5fQry3y+cDew=
|
|
||||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 h1:XRzhVemXdgvJqCH0sFfrBUTnUJSBrBf7++ypk+twtRs=
|
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 h1:XRzhVemXdgvJqCH0sFfrBUTnUJSBrBf7++ypk+twtRs=
|
||||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0/go.mod h1:HKpQxkWaGLJ+D/5H8QRpyQXA1eKjxkFlOMwck5+33Jk=
|
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0/go.mod h1:HKpQxkWaGLJ+D/5H8QRpyQXA1eKjxkFlOMwck5+33Jk=
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
@@ -20,8 +16,7 @@ github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZ
|
|||||||
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||||
github.com/caarlos0/env/v11 v11.4.0 h1:Kcb6t5kIIr4XkoQC9AF2j+8E1Jsrl3Wz/hhm1LtoGAc=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/caarlos0/env/v11 v11.4.0/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@@ -47,8 +42,8 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
|
|||||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
package azure
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrInvalidConnectionString = errors.New("invalid connection string")
|
|
||||||
ErrContainerNotFound = errors.New("container not found")
|
|
||||||
ErrBlobNotFound = errors.New("blob not found")
|
|
||||||
ErrInvalidInput = errors.New("invalid input parameters")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
client *azblob.Client
|
|
||||||
azureStorageConfig config.AzureStorageConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClient(azureStorageConfig config.AzureStorageConfig) (*Client, error) {
|
|
||||||
protocol := "https"
|
|
||||||
if !azureStorageConfig.UseHTTPS {
|
|
||||||
protocol = "http"
|
|
||||||
}
|
|
||||||
|
|
||||||
blobEndpoint, _ := url.JoinPath(azureStorageConfig.ServiceURL, azureStorageConfig.AccountName)
|
|
||||||
connStr := fmt.Sprintf("DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s;BlobEndpoint=%s;",
|
|
||||||
protocol, azureStorageConfig.AccountName, azureStorageConfig.AccountKey, blobEndpoint)
|
|
||||||
|
|
||||||
client, err := azblob.NewClientFromConnectionString(connStr, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("creating azure storage client: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
|
||||||
client: client,
|
|
||||||
azureStorageConfig: azureStorageConfig,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) CreateContainer(ctx context.Context, containerName string) error {
|
|
||||||
if containerName == "" {
|
|
||||||
return ErrInvalidInput
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.client.CreateContainer(ctx, containerName, nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("creating container %s: %w", containerName, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) UploadBuffer(ctx context.Context, containerName, blobPath string, buffer []byte) error {
|
|
||||||
if containerName == "" || blobPath == "" || buffer == nil {
|
|
||||||
return ErrInvalidInput
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.client.UploadBuffer(ctx, containerName, blobPath, buffer, nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("uploading blob %s: %w", blobPath, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) UploadAndGetURL(ctx context.Context, blobPath string, buffer []byte) (string, error) {
|
|
||||||
if blobPath == "" || buffer == nil {
|
|
||||||
return "", ErrInvalidInput
|
|
||||||
}
|
|
||||||
|
|
||||||
fullPath := blobPath
|
|
||||||
if c.azureStorageConfig.Prefix != "" {
|
|
||||||
fullPath, _ = url.JoinPath(c.azureStorageConfig.Prefix, blobPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.UploadBuffer(ctx, c.azureStorageConfig.Container, fullPath, buffer); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
blobEndpoint, _ := url.JoinPath(c.azureStorageConfig.ServiceURL, c.azureStorageConfig.AccountName)
|
|
||||||
blobURL, _ := url.JoinPath(blobEndpoint, c.azureStorageConfig.Container, fullPath)
|
|
||||||
return blobURL, nil
|
|
||||||
}
|
|
||||||
@@ -1,40 +1,41 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/caarlos0/env/v11"
|
"os"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureStorageConfig struct {
|
type appConfig struct {
|
||||||
AccountName string `env:"AZ_ACCOUNT_NAME"`
|
SourceDbUrl string
|
||||||
Container string `env:"AZ_CONTAINER"`
|
TargetDbUrl string
|
||||||
AccountKey string `env:"AZ_ACCOUNT_KEY"`
|
|
||||||
UseHTTPS bool `env:"AZ_USE_HTTPS" envDefault:"true"`
|
|
||||||
ServiceURL string `env:"AZ_SERVICE_URL"`
|
|
||||||
Prefix string `env:"AZ_PREFIX"`
|
|
||||||
Enabled bool `env:"AZ_STORAGE_ENABLED"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type appConfig struct {
|
func loadEnv() {
|
||||||
SourceDbUrl string `env:"SOURCE_DB_URL,required"`
|
err := godotenv.Load()
|
||||||
TargetDbUrl string `env:"TARGET_DB_URL,required"`
|
if err != nil {
|
||||||
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
|
log.Warn("Warning: could not load .env file")
|
||||||
AzureStorage AzureStorageConfig
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAppConfig() appConfig {
|
func getAppConfig() appConfig {
|
||||||
if err := godotenv.Load(); err != nil {
|
loadEnv()
|
||||||
log.Warn("Could not load .env file")
|
|
||||||
|
sourceDbUrl := os.Getenv("SOURCE_DB_URL")
|
||||||
|
if sourceDbUrl == "" {
|
||||||
|
log.Fatal("SOURCE_DB_URL environment variable not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := appConfig{}
|
targetDbUrl := os.Getenv("TARGET_DB_URL")
|
||||||
|
if targetDbUrl == "" {
|
||||||
if err := env.Parse(&cfg); err != nil {
|
log.Fatal("TARGET_DB_URL environment variable not set")
|
||||||
log.Fatalf("Error al cargar variables de entorno: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg
|
return appConfig{
|
||||||
|
SourceDbUrl: sourceDbUrl,
|
||||||
|
TargetDbUrl: targetDbUrl,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var App appConfig = getAppConfig()
|
var App appConfig = getAppConfig()
|
||||||
|
|||||||
@@ -14,16 +14,6 @@ type RetryConfig struct {
|
|||||||
MaxJitterMs int `yaml:"max_jitter_ms"`
|
MaxJitterMs int `yaml:"max_jitter_ms"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToStorageColumnConfig struct {
|
|
||||||
Source string `yaml:"source"`
|
|
||||||
Target string `yaml:"target"`
|
|
||||||
Mode string `yaml:"mode"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ToStorageConfig struct {
|
|
||||||
Columns []ToStorageColumnConfig `yaml:"columns"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type JobConfig struct {
|
type JobConfig struct {
|
||||||
MaxExtractors int `yaml:"max_extractors"`
|
MaxExtractors int `yaml:"max_extractors"`
|
||||||
MaxLoaders int `yaml:"max_loaders"`
|
MaxLoaders int `yaml:"max_loaders"`
|
||||||
@@ -36,7 +26,6 @@ type JobConfig struct {
|
|||||||
MaxChunkErrors int `yaml:"max_chunk_errors"`
|
MaxChunkErrors int `yaml:"max_chunk_errors"`
|
||||||
Retry RetryConfig `yaml:"retry"`
|
Retry RetryConfig `yaml:"retry"`
|
||||||
RowsPerPartition int64
|
RowsPerPartition int64
|
||||||
ToStorage ToStorageConfig `yaml:"to_storage"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TableInfo struct {
|
type TableInfo struct {
|
||||||
@@ -44,31 +33,29 @@ type TableInfo struct {
|
|||||||
Table string `yaml:"table"`
|
Table string `yaml:"table"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TargetTableInfo struct {
|
||||||
|
TableInfo `yaml:",inline"`
|
||||||
|
}
|
||||||
|
|
||||||
type SourceTableInfo struct {
|
type SourceTableInfo struct {
|
||||||
TableInfo `yaml:",inline"`
|
TableInfo `yaml:",inline"`
|
||||||
PrimaryKey string `yaml:"primary_key"`
|
PrimaryKey string `yaml:"primary_key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TargetTableInfo struct {
|
|
||||||
TableInfo `yaml:",inline"`
|
|
||||||
PreSQL []string `yaml:"pre_sql"`
|
|
||||||
PostSQL []string `yaml:"post_sql"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RangeConfig struct {
|
|
||||||
Min int64 `yaml:"min"`
|
|
||||||
Max int64 `yaml:"max"`
|
|
||||||
IsMinInclusive bool `yaml:"is_min_inclusive"`
|
|
||||||
IsMaxInclusive bool `yaml:"is_max_inclusive"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Job struct {
|
type Job struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
SourceTable SourceTableInfo `yaml:"source"`
|
SourceTable SourceTableInfo `yaml:"source"`
|
||||||
TargetTable TargetTableInfo `yaml:"target"`
|
TargetTable TargetTableInfo `yaml:"target"`
|
||||||
|
PreSQL []string `yaml:"pre_sql"`
|
||||||
|
PostSQL []string `yaml:"post_sql"`
|
||||||
JobConfig `yaml:",inline"`
|
JobConfig `yaml:",inline"`
|
||||||
Range RangeConfig `yaml:"range"`
|
Range struct {
|
||||||
|
Min int64 `yaml:"min"`
|
||||||
|
Max int64 `yaml:"max"`
|
||||||
|
IsMinInclusive bool `yaml:"is_min_inclusive"`
|
||||||
|
IsMaxInclusive bool `yaml:"is_max_inclusive"`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MigrationConfig struct {
|
type MigrationConfig struct {
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
package custom_errors
|
package custom_errors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtractorError struct {
|
type ExtractorError struct {
|
||||||
@@ -20,100 +14,3 @@ type ExtractorError struct {
|
|||||||
func (e *ExtractorError) Error() string {
|
func (e *ExtractorError) Error() string {
|
||||||
return e.Msg
|
return e.Msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExtractorErrorHandler(
|
|
||||||
ctx context.Context,
|
|
||||||
retryConfig config.RetryConfig,
|
|
||||||
maxPartitionErrors int,
|
|
||||||
chErrorsIn <-chan ExtractorError,
|
|
||||||
chPartitionsOut chan<- models.Partition,
|
|
||||||
chJobErrorsOut chan<- JobError,
|
|
||||||
wgActivePartitions *sync.WaitGroup,
|
|
||||||
) {
|
|
||||||
definitiveErrors := 0
|
|
||||||
|
|
||||||
for {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
|
|
||||||
case err, ok := <-chErrorsIn:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Partition.RetryCounter >= retryConfig.Attempts {
|
|
||||||
wgActivePartitions.Done()
|
|
||||||
definitiveErrors++
|
|
||||||
jobError := JobError{
|
|
||||||
ShouldCancelJob: false,
|
|
||||||
Msg: fmt.Sprintf("Partition %v reached max retries (%d)", err.Partition.Id, retryConfig.Attempts),
|
|
||||||
Prev: &err,
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case chJobErrorsOut <- jobError:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if maxPartitionErrors > 0 && definitiveErrors >= maxPartitionErrors {
|
|
||||||
fatalError := JobError{
|
|
||||||
ShouldCancelJob: true,
|
|
||||||
Msg: fmt.Sprintf("Partition error limit reached (%d)", maxPartitionErrors),
|
|
||||||
Prev: &err,
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case chJobErrorsOut <- fatalError:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
jobError := JobError{
|
|
||||||
ShouldCancelJob: false,
|
|
||||||
Msg: fmt.Sprintf("Temporal error in partition %v (retries: %d)", err.Partition.Id, err.Partition.RetryCounter),
|
|
||||||
Prev: &err,
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case chJobErrorsOut <- jobError:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
newPartition := err.Partition
|
|
||||||
newPartition.RetryCounter++
|
|
||||||
|
|
||||||
delay := computeBackoffDelay(
|
|
||||||
newPartition.RetryCounter,
|
|
||||||
retryConfig.BaseDelayMs,
|
|
||||||
retryConfig.MaxDelayMs,
|
|
||||||
retryConfig.MaxJitterMs,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err.HasLastId {
|
|
||||||
newPartition.ParentId = err.Partition.Id
|
|
||||||
newPartition.Id = uuid.New()
|
|
||||||
newPartition.Range.Min = err.LastId
|
|
||||||
newPartition.Range.IsMinInclusive = false
|
|
||||||
}
|
|
||||||
|
|
||||||
requeueWithBackoff(ctx, delay, func() {
|
|
||||||
select {
|
|
||||||
case chPartitionsOut <- newPartition:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
92
internal/app/etl/extractors/consumer.go
Normal file
92
internal/app/etl/extractors/consumer.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package extractors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Consume(
|
||||||
|
ctx context.Context,
|
||||||
|
extractor etl.Extractor,
|
||||||
|
tableInfo config.SourceTableInfo,
|
||||||
|
columns []models.ColumnType,
|
||||||
|
batchSize int,
|
||||||
|
chPartitionsIn <-chan models.Partition,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
chErrorsOut chan<- custom_errors.JobError,
|
||||||
|
wgActivePartitions *sync.WaitGroup,
|
||||||
|
rowsRead *int64,
|
||||||
|
) {
|
||||||
|
indexPrimaryKey := slices.IndexFunc(columns, func(col models.ColumnType) bool {
|
||||||
|
return strings.EqualFold(col.Name(), tableInfo.PrimaryKey)
|
||||||
|
})
|
||||||
|
|
||||||
|
if indexPrimaryKey == -1 {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chErrorsOut <- custom_errors.JobError{
|
||||||
|
ShouldCancelJob: true,
|
||||||
|
Msg: "Primary key not found in provided columns",
|
||||||
|
}:
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case partition, ok := <-chPartitionsIn:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsReadResult, err := extractWithRetries(
|
||||||
|
ctx,
|
||||||
|
extractor,
|
||||||
|
tableInfo,
|
||||||
|
columns,
|
||||||
|
batchSize,
|
||||||
|
partition,
|
||||||
|
indexPrimaryKey,
|
||||||
|
chBatchesOut,
|
||||||
|
)
|
||||||
|
wgActivePartitions.Done()
|
||||||
|
|
||||||
|
if rowsReadResult > 0 {
|
||||||
|
atomic.AddInt64(rowsRead, rowsReadResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
var jobError *custom_errors.JobError
|
||||||
|
if errors.As(err, &jobError) {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chErrorsOut <- *jobError:
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chErrorsOut <- custom_errors.JobError{ShouldCancelJob: false, Msg: err.Error(), Prev: err}:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
internal/app/etl/extractors/extract-with-retries.go
Normal file
70
internal/app/etl/extractors/extract-with-retries.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package extractors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func extractWithRetries(
|
||||||
|
ctx context.Context,
|
||||||
|
extractor etl.Extractor,
|
||||||
|
tableInfo config.SourceTableInfo,
|
||||||
|
columns []models.ColumnType,
|
||||||
|
batchSize int,
|
||||||
|
partition models.Partition,
|
||||||
|
indexPrimaryKey int,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
) (int64, error) {
|
||||||
|
var totalRowsRead int64
|
||||||
|
delay := time.Duration(time.Second * 1)
|
||||||
|
currentParitition := partition
|
||||||
|
|
||||||
|
for {
|
||||||
|
rowsRead, err := extractor.Exec(
|
||||||
|
ctx,
|
||||||
|
tableInfo,
|
||||||
|
columns,
|
||||||
|
batchSize,
|
||||||
|
currentParitition,
|
||||||
|
indexPrimaryKey,
|
||||||
|
chBatchesOut,
|
||||||
|
)
|
||||||
|
totalRowsRead += rowsRead
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return totalRowsRead, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var exError *custom_errors.ExtractorError
|
||||||
|
if errors.As(err, &exError) {
|
||||||
|
currentParitition.RetryCounter++
|
||||||
|
|
||||||
|
if currentParitition.RetryCounter > 3 {
|
||||||
|
return totalRowsRead, &custom_errors.JobError{
|
||||||
|
Msg: fmt.Sprintf("Partition %v reached max retries", exError.Partition.Id),
|
||||||
|
Prev: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if exError.HasLastId {
|
||||||
|
currentParitition.ParentId = exError.Partition.Id
|
||||||
|
currentParitition.Id = uuid.New()
|
||||||
|
currentParitition.Range.Min = exError.LastId
|
||||||
|
currentParitition.Range.IsMinInclusive = false
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(delay)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalRowsRead, err
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,100 +2,63 @@ package extractors
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/convert"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Consume(
|
func errorFromLastPartitionRow(
|
||||||
ctx context.Context,
|
lastRow models.UnknownRowValues,
|
||||||
extractor etl.Extractor,
|
indexPrimaryKey int,
|
||||||
tableInfo config.SourceTableInfo,
|
partition models.Partition,
|
||||||
columns []models.ColumnType,
|
previousError error,
|
||||||
batchSize int,
|
) error {
|
||||||
chPartitionsIn <-chan models.Partition,
|
lastIdRawValue := lastRow[indexPrimaryKey]
|
||||||
chBatchesOut chan<- models.Batch,
|
|
||||||
chErrorsOut chan<- custom_errors.ExtractorError,
|
|
||||||
chJobErrorsOut chan<- custom_errors.JobError,
|
|
||||||
wgActivePartitions *sync.WaitGroup,
|
|
||||||
rowsRead *int64,
|
|
||||||
) {
|
|
||||||
indexPrimaryKey := slices.IndexFunc(columns, func(col models.ColumnType) bool {
|
|
||||||
return strings.EqualFold(col.Name(), tableInfo.PrimaryKey)
|
|
||||||
})
|
|
||||||
|
|
||||||
if indexPrimaryKey == -1 {
|
lastId, ok := convert.ToInt64(lastIdRawValue)
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chJobErrorsOut <- custom_errors.JobError{
|
|
||||||
ShouldCancelJob: true,
|
|
||||||
Msg: "Primary key not found in provided columns",
|
|
||||||
}:
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case partition, ok := <-chPartitionsIn:
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
currentPartition := partition
|
||||||
|
currentPartition.RetryCounter = 3
|
||||||
|
return &custom_errors.ExtractorError{
|
||||||
|
Partition: currentPartition,
|
||||||
|
HasLastId: true,
|
||||||
|
Msg: fmt.Sprintf("Couldn't cast last id value as int: %s", previousError.Error()),
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsReadResult, err := extractor.Exec(
|
|
||||||
ctx,
|
|
||||||
tableInfo,
|
|
||||||
columns,
|
|
||||||
batchSize,
|
|
||||||
partition,
|
|
||||||
indexPrimaryKey,
|
|
||||||
chBatchesOut,
|
|
||||||
)
|
|
||||||
|
|
||||||
if rowsReadResult > 0 {
|
|
||||||
atomic.AddInt64(rowsRead, int64(rowsReadResult))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
return &custom_errors.ExtractorError{
|
||||||
if exError, ok := errors.AsType[*custom_errors.ExtractorError](err); ok {
|
Partition: partition,
|
||||||
|
HasLastId: true,
|
||||||
|
LastId: lastId,
|
||||||
|
Msg: previousError.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendBatch(ctx context.Context, chBatchesOut chan<- models.Batch, batch models.Batch) error {
|
||||||
select {
|
select {
|
||||||
|
case chBatchesOut <- batch:
|
||||||
|
return nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return ctx.Err()
|
||||||
case chErrorsOut <- *exError:
|
|
||||||
}
|
|
||||||
} else if jobError, ok := errors.AsType[*custom_errors.JobError](err); ok {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chJobErrorsOut <- *jobError:
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chErrorsOut <- custom_errors.ExtractorError{Partition: partition, Msg: err.Error()}:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
func flush(
|
||||||
|
ctx context.Context,
|
||||||
|
partition *models.Partition,
|
||||||
|
batchSize int,
|
||||||
|
batchRows []models.UnknownRowValues,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
) error {
|
||||||
|
if len(batchRows) == 0 {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
wgActivePartitions.Done()
|
batch := models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows}
|
||||||
}
|
batchRows = make([]models.UnknownRowValues, 0, batchSize)
|
||||||
}
|
return sendBatch(ctx, chBatchesOut, batch)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,13 @@ package extractors
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/convert"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
|
||||||
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MssqlExtractor struct {
|
type MssqlExtractor struct {
|
||||||
@@ -29,9 +25,6 @@ func buildExtractQueryMssql(
|
|||||||
columns []models.ColumnType,
|
columns []models.ColumnType,
|
||||||
includeRange bool,
|
includeRange bool,
|
||||||
isMinInclusive bool,
|
isMinInclusive bool,
|
||||||
isMaxInclusive bool,
|
|
||||||
hasMin bool,
|
|
||||||
hasMax bool,
|
|
||||||
) string {
|
) string {
|
||||||
var sbQuery strings.Builder
|
var sbQuery strings.Builder
|
||||||
|
|
||||||
@@ -55,32 +48,15 @@ func buildExtractQueryMssql(
|
|||||||
|
|
||||||
fmt.Fprintf(&sbQuery, " FROM [%s].[%s]", tableInfo.Schema, tableInfo.Table)
|
fmt.Fprintf(&sbQuery, " FROM [%s].[%s]", tableInfo.Schema, tableInfo.Table)
|
||||||
|
|
||||||
if includeRange && (hasMin || hasMax) {
|
if includeRange {
|
||||||
sbQuery.WriteString(" WHERE ")
|
fmt.Fprintf(&sbQuery, " WHERE [%s]", tableInfo.PrimaryKey)
|
||||||
|
|
||||||
if hasMin {
|
|
||||||
fmt.Fprintf(&sbQuery, "[%s]", tableInfo.PrimaryKey)
|
|
||||||
if isMinInclusive {
|
if isMinInclusive {
|
||||||
sbQuery.WriteString(" >=")
|
sbQuery.WriteString(" >=")
|
||||||
} else {
|
} else {
|
||||||
sbQuery.WriteString(" >")
|
sbQuery.WriteString(" >")
|
||||||
}
|
}
|
||||||
sbQuery.WriteString(" @min")
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasMin && hasMax {
|
fmt.Fprintf(&sbQuery, " @min AND [%s] <= @max", tableInfo.PrimaryKey)
|
||||||
sbQuery.WriteString(" AND ")
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasMax {
|
|
||||||
fmt.Fprintf(&sbQuery, "[%s]", tableInfo.PrimaryKey)
|
|
||||||
if isMaxInclusive {
|
|
||||||
sbQuery.WriteString(" <=")
|
|
||||||
} else {
|
|
||||||
sbQuery.WriteString(" <")
|
|
||||||
}
|
|
||||||
sbQuery.WriteString(" @max")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", tableInfo.PrimaryKey)
|
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", tableInfo.PrimaryKey)
|
||||||
@@ -88,34 +64,6 @@ func buildExtractQueryMssql(
|
|||||||
return sbQuery.String()
|
return sbQuery.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorFromLastRow(
|
|
||||||
lastRow models.UnknownRowValues,
|
|
||||||
indexPrimaryKey int,
|
|
||||||
partition models.Partition,
|
|
||||||
previousError error,
|
|
||||||
) *custom_errors.ExtractorError {
|
|
||||||
lastIdRawValue := lastRow[indexPrimaryKey]
|
|
||||||
|
|
||||||
lastId, ok := convert.ToInt64(lastIdRawValue)
|
|
||||||
if !ok {
|
|
||||||
currentPartition := partition
|
|
||||||
currentPartition.RetryCounter = 3
|
|
||||||
return &custom_errors.ExtractorError{
|
|
||||||
Partition: currentPartition,
|
|
||||||
HasLastId: true,
|
|
||||||
Msg: fmt.Sprintf("Couldn't cast last id value as int: %s", previousError.Error()),
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return &custom_errors.ExtractorError{
|
|
||||||
Partition: partition,
|
|
||||||
HasLastId: true,
|
|
||||||
LastId: lastId,
|
|
||||||
Msg: previousError.Error(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mssqlEx *MssqlExtractor) Exec(
|
func (mssqlEx *MssqlExtractor) Exec(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.SourceTableInfo,
|
tableInfo config.SourceTableInfo,
|
||||||
@@ -124,86 +72,50 @@ func (mssqlEx *MssqlExtractor) Exec(
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int, error) {
|
) (int64, error) {
|
||||||
hasMin := partition.HasRange && partition.Range.Min > 0
|
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive)
|
||||||
hasMax := partition.HasRange && partition.Range.Max > 0
|
|
||||||
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive, hasMin, hasMax)
|
|
||||||
|
|
||||||
var queryArgs []any
|
var queryArgs []any
|
||||||
if hasMin {
|
if partition.HasRange {
|
||||||
queryArgs = append(queryArgs, sql.Named("min", partition.Range.Min))
|
queryArgs = append(queryArgs, sql.Named("min", partition.Range.Min), sql.Named("max", partition.Range.Max))
|
||||||
}
|
|
||||||
if hasMax {
|
|
||||||
queryArgs = append(queryArgs, sql.Named("max", partition.Range.Max))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsRead := 0
|
|
||||||
rows, err := mssqlEx.db.Query(ctx, query, queryArgs...)
|
rows, err := mssqlEx.db.Query(ctx, query, queryArgs...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
return 0, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
batchRows := make([]models.UnknownRowValues, 0, batchSize)
|
batchRows := make([]models.UnknownRowValues, 0, batchSize)
|
||||||
|
var rowsRead int64 = 0
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
rowValues := make([]any, len(columns))
|
values, err := rows.Values()
|
||||||
scanArgs := make([]any, len(columns))
|
if err != nil {
|
||||||
|
if len(batchRows) == 0 {
|
||||||
for i := range rowValues {
|
return rowsRead, err
|
||||||
scanArgs[i] = &rowValues[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := rows.Scan(scanArgs...); err != nil {
|
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
||||||
if len(batchRows) == 0 {
|
return rowsRead, err
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lastRow := batchRows[len(batchRows)-1]
|
lastRow := batchRows[len(batchRows)-1]
|
||||||
|
return rowsRead, errorFromLastPartitionRow(lastRow, indexPrimaryKey, partition, err)
|
||||||
select {
|
|
||||||
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return rowsRead, ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
return rowsRead, errorFromLastRow(lastRow, indexPrimaryKey, partition, err)
|
|
||||||
}
|
}
|
||||||
rowsRead++
|
rowsRead++
|
||||||
|
|
||||||
batchRows = append(batchRows, rowValues)
|
batchRows = append(batchRows, values)
|
||||||
if len(batchRows) >= batchSize {
|
if len(batchRows) >= batchSize {
|
||||||
select {
|
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
||||||
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
return rowsRead, err
|
||||||
case <-ctx.Done():
|
|
||||||
return rowsRead, ctx.Err()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
batchRows = make([]models.UnknownRowValues, 0, batchSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
if errors.Is(err, ctx.Err()) {
|
|
||||||
return rowsRead, ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(batchRows) > 0 {
|
|
||||||
lastRow := batchRows[len(batchRows)-1]
|
|
||||||
return rowsRead, errorFromLastRow(lastRow, indexPrimaryKey, partition, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(batchRows) > 0 {
|
|
||||||
select {
|
|
||||||
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return rowsRead, ctx.Err()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rowsRead, nil
|
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
||||||
|
return rowsRead, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowsRead, rows.Err()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package extractors
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -21,15 +22,7 @@ func NewPostgresExtractor(db dbwrapper.DbWrapper) etl.Extractor {
|
|||||||
return &PostgresExtractor{db: db}
|
return &PostgresExtractor{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildExtractQueryPostgres(
|
func buildExtractQueryPostgres(sourceDbInfo config.SourceTableInfo, columns []models.ColumnType) string {
|
||||||
sourceDbInfo config.SourceTableInfo,
|
|
||||||
columns []models.ColumnType,
|
|
||||||
includeRange bool,
|
|
||||||
isMinInclusive bool,
|
|
||||||
isMaxInclusive bool,
|
|
||||||
hasMin bool,
|
|
||||||
hasMax bool,
|
|
||||||
) string {
|
|
||||||
var sbColumns strings.Builder
|
var sbColumns strings.Builder
|
||||||
|
|
||||||
if len(columns) == 0 {
|
if len(columns) == 0 {
|
||||||
@@ -54,41 +47,7 @@ func buildExtractQueryPostgres(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`SELECT %s FROM "%s"."%s"`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table)
|
return fmt.Sprintf(`SELECT %s FROM "%s"."%s" ORDER BY "%s" ASC`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table, sourceDbInfo.PrimaryKey)
|
||||||
|
|
||||||
if includeRange && (hasMin || hasMax) {
|
|
||||||
query += " WHERE "
|
|
||||||
paramIdx := 1
|
|
||||||
|
|
||||||
if hasMin {
|
|
||||||
query += fmt.Sprintf(`"%s"`, sourceDbInfo.PrimaryKey)
|
|
||||||
if isMinInclusive {
|
|
||||||
query += " >="
|
|
||||||
} else {
|
|
||||||
query += " >"
|
|
||||||
}
|
|
||||||
query += fmt.Sprintf(" $%d", paramIdx)
|
|
||||||
paramIdx++
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasMin && hasMax {
|
|
||||||
query += " AND "
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasMax {
|
|
||||||
query += fmt.Sprintf(`"%s"`, sourceDbInfo.PrimaryKey)
|
|
||||||
if isMaxInclusive {
|
|
||||||
query += " <="
|
|
||||||
} else {
|
|
||||||
query += " <"
|
|
||||||
}
|
|
||||||
query += fmt.Sprintf(" $%d", paramIdx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
query += fmt.Sprintf(` ORDER BY "%s" ASC`, sourceDbInfo.PrimaryKey)
|
|
||||||
|
|
||||||
return query
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (postgresEx *PostgresExtractor) Exec(
|
func (postgresEx *PostgresExtractor) Exec(
|
||||||
@@ -99,21 +58,15 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int, error) {
|
) (int64, error) {
|
||||||
hasMin := partition.HasRange && partition.Range.Min > 0
|
query := buildExtractQueryPostgres(tableInfo, columns)
|
||||||
hasMax := partition.HasRange && partition.Range.Max > 0
|
|
||||||
query := buildExtractQueryPostgres(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive, hasMin, hasMax)
|
|
||||||
|
|
||||||
var queryArgs []any
|
if partition.HasRange {
|
||||||
if hasMin {
|
return 0, errors.New("Batch config not yet supported")
|
||||||
queryArgs = append(queryArgs, partition.Range.Min)
|
|
||||||
}
|
|
||||||
if hasMax {
|
|
||||||
queryArgs = append(queryArgs, partition.Range.Max)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsRead := 0
|
var rowsRead int64 = 0
|
||||||
rows, err := postgresEx.db.Query(ctx, query, queryArgs...)
|
rows, err := postgresEx.db.Query(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
||||||
}
|
}
|
||||||
@@ -124,7 +77,7 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
values, err := rows.Values()
|
values, err := rows.Values()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
return rowsRead, errors.New("Unexpected error reading rows from source")
|
||||||
}
|
}
|
||||||
rowsRead++
|
rowsRead++
|
||||||
|
|
||||||
@@ -142,7 +95,7 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
return rowsRead, errors.New("Unexpected error reading rows from source")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(batchRows) > 0 {
|
if len(batchRows) > 0 {
|
||||||
|
|||||||
@@ -15,21 +15,31 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgconn"
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GenericLoader struct {
|
type PostgresLoader struct {
|
||||||
db dbwrapper.DbWrapper
|
db dbwrapper.DbWrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenericLoader(db dbwrapper.DbWrapper) etl.Loader {
|
func NewPostgresLoader(db dbwrapper.DbWrapper) etl.Loader {
|
||||||
return &GenericLoader{db: db}
|
return &PostgresLoader{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gl *GenericLoader) ProcessBatch(
|
func mapSlice[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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (postgresLd *PostgresLoader) ProcessBatch(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.TargetTableInfo,
|
tableInfo config.TargetTableInfo,
|
||||||
colNames []string,
|
colNames []string,
|
||||||
batch models.Batch,
|
batch models.Batch,
|
||||||
) (int, error) {
|
) (int, error) {
|
||||||
_, err := gl.db.SaveMassive(
|
_, err := postgresLd.db.SaveMassive(
|
||||||
ctx,
|
ctx,
|
||||||
tableInfo.Schema,
|
tableInfo.Schema,
|
||||||
tableInfo.Table,
|
tableInfo.Table,
|
||||||
@@ -55,7 +65,7 @@ func (gl *GenericLoader) ProcessBatch(
|
|||||||
return len(batch.Rows), nil
|
return len(batch.Rows), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gl *GenericLoader) Exec(
|
func (postgresLd *PostgresLoader) Exec(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.TargetTableInfo,
|
tableInfo config.TargetTableInfo,
|
||||||
columns []models.ColumnType,
|
columns []models.ColumnType,
|
||||||
@@ -82,7 +92,7 @@ func (gl *GenericLoader) Exec(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
processedRows, err := gl.ProcessBatch(ctx, tableInfo, colNames, batch)
|
processedRows, err := postgresLd.ProcessBatch(ctx, tableInfo, colNames, batch)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var ldError *custom_errors.LoaderError
|
var ldError *custom_errors.LoaderError
|
||||||
1
internal/app/etl/loaders/types.go
Normal file
1
internal/app/etl/loaders/types.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package loaders
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package loaders
|
|
||||||
|
|
||||||
func mapSlice[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
|
|
||||||
}
|
|
||||||
@@ -15,22 +15,7 @@ func PartitionRangeGenerator(
|
|||||||
tableInfo config.TableInfo,
|
tableInfo config.TableInfo,
|
||||||
partitionColumn string,
|
partitionColumn string,
|
||||||
rowsPerPartition int64,
|
rowsPerPartition int64,
|
||||||
jobRange config.RangeConfig,
|
|
||||||
) ([]models.Partition, error) {
|
) ([]models.Partition, error) {
|
||||||
if jobRange.Min > 0 {
|
|
||||||
return []models.Partition{{
|
|
||||||
Id: uuid.New(),
|
|
||||||
HasRange: true,
|
|
||||||
RetryCounter: 0,
|
|
||||||
Range: models.PartitionRange{
|
|
||||||
Min: jobRange.Min,
|
|
||||||
Max: jobRange.Max,
|
|
||||||
IsMinInclusive: jobRange.IsMinInclusive,
|
|
||||||
IsMaxInclusive: jobRange.IsMaxInclusive,
|
|
||||||
},
|
|
||||||
}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsCount, err := tableAnalyzer.EstimateTotalRows(ctx, tableInfo)
|
rowsCount, err := tableAnalyzer.EstimateTotalRows(ctx, tableInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -3,32 +3,18 @@ package transformers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/azure"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
"github.com/google/uuid"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MssqlTransformer struct {
|
type MssqlTransformer struct{}
|
||||||
toStorage config.ToStorageConfig
|
|
||||||
sourceTable config.SourceTableInfo
|
|
||||||
azureClient *azure.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMssqlTransformer(toStorage config.ToStorageConfig, sourceTable config.SourceTableInfo, azureClient *azure.Client) etl.Transformer {
|
func NewMssqlTransformer() etl.Transformer {
|
||||||
return &MssqlTransformer{
|
return &MssqlTransformer{}
|
||||||
toStorage: toStorage,
|
|
||||||
sourceTable: sourceTable,
|
|
||||||
azureClient: azureClient,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeTransformationPlan(columns []models.ColumnType) []etl.ColumnTransformPlan {
|
func computeTransformationPlan(columns []models.ColumnType) []etl.ColumnTransformPlan {
|
||||||
@@ -74,63 +60,6 @@ func computeTransformationPlan(columns []models.ColumnType) []etl.ColumnTransfor
|
|||||||
return plan
|
return plan
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeStorageTransformationPlan(
|
|
||||||
ctx context.Context,
|
|
||||||
azureClient *azure.Client,
|
|
||||||
toStorage config.ToStorageConfig,
|
|
||||||
sourceColumns []models.ColumnType,
|
|
||||||
sourceTable config.SourceTableInfo,
|
|
||||||
) []etl.ColumnTransformPlan {
|
|
||||||
if azureClient == nil || len(toStorage.Columns) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
colIndex := make(map[string]int, len(sourceColumns))
|
|
||||||
for i, col := range sourceColumns {
|
|
||||||
colIndex[strings.ToUpper(col.Name())] = i
|
|
||||||
}
|
|
||||||
|
|
||||||
var plan []etl.ColumnTransformPlan
|
|
||||||
for _, storageCol := range toStorage.Columns {
|
|
||||||
if storageCol.Mode != "REFERENCE_ONLY" {
|
|
||||||
log.Warnf("to_storage: unsupported mode %q for column %s — skipping", storageCol.Mode, storageCol.Source)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
idx, ok := colIndex[strings.ToUpper(storageCol.Source)]
|
|
||||||
if !ok {
|
|
||||||
log.Warnf("to_storage: source column %q not found in source schema — skipping", storageCol.Source)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceColName := storageCol.Source
|
|
||||||
schema := sourceTable.Schema
|
|
||||||
table := sourceTable.Table
|
|
||||||
|
|
||||||
plan = append(plan, etl.ColumnTransformPlan{
|
|
||||||
Index: idx,
|
|
||||||
Fn: func(v any) (any, error) {
|
|
||||||
if v == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
b, ok := v.([]byte)
|
|
||||||
if !ok {
|
|
||||||
log.Warnf("to_storage: expected []byte for %s.%s.%s, got %T — passing through",
|
|
||||||
schema, table, sourceColName, v)
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
blobPath := fmt.Sprintf("%s/%s/%s/%s.bin", schema, table, sourceColName, uuid.New().String())
|
|
||||||
blobURL, err := azureClient.UploadAndGetURL(ctx, blobPath, b)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("uploading %s.%s.%s: %w", schema, table, sourceColName, err)
|
|
||||||
}
|
|
||||||
return blobURL, nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return plan
|
|
||||||
}
|
|
||||||
|
|
||||||
const processBatchCtxCheck = 4096
|
const processBatchCtxCheck = 4096
|
||||||
|
|
||||||
func (mssqlTr *MssqlTransformer) ProcessBatch(
|
func (mssqlTr *MssqlTransformer) ProcessBatch(
|
||||||
@@ -145,6 +74,10 @@ func (mssqlTr *MssqlTransformer) ProcessBatch(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rowValues == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
for _, task := range transformationPlan {
|
for _, task := range transformationPlan {
|
||||||
val := rowValues[task.Index]
|
val := rowValues[task.Index]
|
||||||
if val == nil {
|
if val == nil {
|
||||||
@@ -171,8 +104,6 @@ func (mssqlTr *MssqlTransformer) Exec(
|
|||||||
wgActiveBatches *sync.WaitGroup,
|
wgActiveBatches *sync.WaitGroup,
|
||||||
) {
|
) {
|
||||||
transformationPlan := computeTransformationPlan(columns)
|
transformationPlan := computeTransformationPlan(columns)
|
||||||
storagePlan := computeStorageTransformationPlan(ctx, mssqlTr.azureClient, mssqlTr.toStorage, columns, mssqlTr.sourceTable)
|
|
||||||
transformationPlan = append(transformationPlan, storagePlan...)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type Extractor interface {
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int, error)
|
) (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransformerFunc func(any) (any, error)
|
type TransformerFunc func(any) (any, error)
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import "github.com/google/uuid"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UnknownRowValues = []any
|
type UnknownRowValues = []any
|
||||||
|
|
||||||
@@ -29,13 +25,3 @@ type Partition struct {
|
|||||||
HasRange bool
|
HasRange bool
|
||||||
RetryCounter int
|
RetryCounter int
|
||||||
}
|
}
|
||||||
|
|
||||||
type JobResult struct {
|
|
||||||
JobName string
|
|
||||||
StartTime time.Time
|
|
||||||
Duration time.Duration
|
|
||||||
RowsRead int64
|
|
||||||
RowsLoaded int64
|
|
||||||
RowsFailed int64
|
|
||||||
Error error
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"math/rand"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/azure"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cfg := config.App.AzureStorage
|
|
||||||
containerName := cfg.Container
|
|
||||||
|
|
||||||
client, err := azure.NewClient(cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error creando cliente: %v", err)
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for i := 1; i <= 10; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(id int) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
blobName := fmt.Sprintf("%sarchivo-%d.txt", cfg.Prefix, id)
|
|
||||||
content := fmt.Sprintf("Contenido aleatorio: %d", rand.Intn(100000))
|
|
||||||
|
|
||||||
err := client.UploadBuffer(ctx, containerName, blobName, []byte(content))
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Fallo al subir %s: %v", blobName, err)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Subido exitosamente: %s\n", blobName)
|
|
||||||
}
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user