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,
|
||||
loader,
|
||||
job,
|
||||
targetDb.GetDialect(),
|
||||
)
|
||||
|
||||
chJobResults <- res
|
||||
|
||||
@@ -18,6 +18,20 @@ import (
|
||||
"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(
|
||||
ctx context.Context,
|
||||
targetDbWrapper dbwrapper.DbWrapper,
|
||||
@@ -27,6 +41,7 @@ func processMigrationJob(
|
||||
transformer etl.Transformer,
|
||||
loader etl.Loader,
|
||||
job config.Job,
|
||||
targetDbType string,
|
||||
) models.JobResult {
|
||||
localCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
@@ -67,7 +82,13 @@ func processMigrationJob(
|
||||
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 {
|
||||
result.Error = err
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user