feat: add truncate query handling in migration process; update job configuration to remove commented truncate SQL
This commit is contained in:
@@ -135,6 +135,7 @@ func processMigrationJobs(
|
|||||||
transformer,
|
transformer,
|
||||||
loader,
|
loader,
|
||||||
job,
|
job,
|
||||||
|
targetDb.GetDialect(),
|
||||||
)
|
)
|
||||||
|
|
||||||
chJobResults <- res
|
chJobResults <- res
|
||||||
|
|||||||
@@ -18,6 +18,20 @@ import (
|
|||||||
"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,
|
||||||
@@ -27,6 +41,7 @@ func processMigrationJob(
|
|||||||
transformer etl.Transformer,
|
transformer etl.Transformer,
|
||||||
loader etl.Loader,
|
loader etl.Loader,
|
||||||
job config.Job,
|
job config.Job,
|
||||||
|
targetDbType string,
|
||||||
) models.JobResult {
|
) models.JobResult {
|
||||||
localCtx, cancel := context.WithCancel(ctx)
|
localCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -67,7 +82,13 @@ func processMigrationJob(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, query := range job.PreSQL {
|
preSqlQueries := job.PreSQL
|
||||||
|
if job.TruncateTarget {
|
||||||
|
truncateQuery := buildTruncateQuery(targetDbType, job.TargetTable.Schema, job.TargetTable.Table, job.TruncateMethod)
|
||||||
|
preSqlQueries = append([]string{truncateQuery}, job.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
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ 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
|
||||||
@@ -48,6 +47,5 @@ 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"
|
||||||
|
|||||||
Reference in New Issue
Block a user