From ca463c9215347df81a43fcb7c91f7ed6b4e4a979 Mon Sep 17 00:00:00 2001 From: Yoki Date: Mon, 16 Dec 2019 11:27:42 +0800 Subject: [PATCH] Fix duplicated columns --- csv.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/csv.go b/csv.go index 1b05a51..b51f032 100644 --- a/csv.go +++ b/csv.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "math/rand" "os" "strings" "unicode/utf8" @@ -63,12 +64,25 @@ func parseColumns(reader *csv.Reader, skipHeader bool, fields string) ([]string, } for i, col := range columns { - columns[i] = postgresify(col) + name := postgresify(col) + if inArray(columns[0:i], name) { + name += fmt.Sprintf("_%d", rand.Intn(10000)) + } + columns[i] = name } return columns, nil } +func inArray(arr []string, a string) bool { + for _, str := range arr { + if str == a { + return true + } + } + return false +} + func copyCSVRows(i *Import, reader *csv.Reader, ignoreErrors bool, delimiter string, columns []string, nullDelimiter string) (error, int, int) { success := 0