0

I want to insert to database from CSV file using gorm AutoMigrate and while inserting I want to avoid duplicate entry. How Can I achieve this? Please check the attached code.

type User struct {
    gorm.Model

    ID                           int64  `csv:"_" db:"id"`
    FirstName                    string `csv:"First name" db:"first_name"`
    LastName                     string `csv:"Last name" db:"last_name"`
    Emails                       string `csv:"Emails" db:"emails"`
}

func main() {
    file, err := os.Open(os.Args[1])
    defer file.Close()
    users := []User{}
    err = gocsv.Unmarshal(file, &users)
    db, err := gorm.Open(postgres.Open("host=xxx.xx.x.x user=database password=password dbname=database port=5432 sslmode=disable"))

    err = db.AutoMigrate(&User{})
    if err != nil {
        panic(err)
    }

    result := db.Create(users)
    if result.Error != nil {
        panic(result.Error)
    }
}

Example: Consider the below data

FIrst name Last name Emails
First Name first@example.com
Second Name second@example.com
Third Name
Forth Name first@example.com

If we pass the above data, the first 3 rows should insert into the database i.e. we have to avoid duplicate email entries to the database. Thanks.

Note: If the email is empty then the row should be inserted into the database.

Benk I
  • 185
  • 13

1 Answers1

0

You have to sanitize "users" after err = gocsv.Unmarshal(file, &users)

Somethink like

func sanytize(arr []User) []User {
   users := []User{}
   mail := []string{}
   for _, a := range arr {
       if !contains(mail, a.Emails){
           users = append(users, a)
       }
       mail = append(mail, a.Emails)
       
   }
   
   return users
}

func contains(arr []string, str string) bool {
   for _, a := range arr {
      if a == str {
         return true
      }
   }
   return false
}

....

err = gocsv.Unmarshal(file, &users)
users = sanytize(users)
Zeppi
  • 1,175
  • 6
  • 11