From 9e146a540d659bfaa3d32cfd7988771cc4424418 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 27 Dec 2018 16:06:08 +0300 Subject: [PATCH] improve user save --- go.sum | 2 -- migrations/1545914948_app.down.sql | 2 ++ migrations/1545914948_app.up.sql | 2 ++ src/repository.go | 11 ++++++++++- 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 migrations/1545914948_app.down.sql create mode 100644 migrations/1545914948_app.up.sql diff --git a/go.sum b/go.sum index 605f1cd..ecb5e65 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/retailcrm/api-client-go v1.1.0 h1:APPO6ccJAeMV7Jz7BhrtDKSOm2r1j5Ft6fuEXNP2ij4= github.com/retailcrm/api-client-go v1.1.0/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= -github.com/retailcrm/mg-transport-api-client-go v1.1.20 h1:1aDJ2QVtSkMtSUm50exmzQFSsE+/FQiWYdw2pamUyLc= -github.com/retailcrm/mg-transport-api-client-go v1.1.20/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/retailcrm/mg-transport-api-client-go v1.1.21 h1:OKkey9uvB16ucICuO2hp/tfh0wfp3na0CxRiT2Iv3sE= github.com/retailcrm/mg-transport-api-client-go v1.1.21/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= diff --git a/migrations/1545914948_app.down.sql b/migrations/1545914948_app.down.sql new file mode 100644 index 0000000..2c24843 --- /dev/null +++ b/migrations/1545914948_app.down.sql @@ -0,0 +1,2 @@ +alter table mg_user alter column created_at drop default; +alter table mg_user alter column updated_at drop default; diff --git a/migrations/1545914948_app.up.sql b/migrations/1545914948_app.up.sql new file mode 100644 index 0000000..5145965 --- /dev/null +++ b/migrations/1545914948_app.up.sql @@ -0,0 +1,2 @@ +alter table mg_user alter column created_at set default current_timestamp; +alter table mg_user alter column updated_at set default current_timestamp; diff --git a/src/repository.go b/src/repository.go index 089651c..fc2a1fc 100644 --- a/src/repository.go +++ b/src/repository.go @@ -99,7 +99,16 @@ func getConnectionById(id int) *Connection { } func (u *User) save() error { - return orm.DB.Save(u).Error + return orm.DB.Exec( + "INSERT INTO mg_user (external_id, user_photo_url, user_photo_id) "+ + "VALUES (?, ?, ?) "+ + "ON CONFLICT (external_id) DO UPDATE SET "+ + "user_photo_url = excluded.user_photo_url, user_photo_id=excluded.user_photo_id, updated_at= ?", + u.ExternalID, + u.UserPhotoURL, + u.UserPhotoID, + time.Now(), + ).Error } func getUserByExternalID(eid int) *User {