From c3a9eea92dd9c561decac6ba2c18387d25db440f Mon Sep 17 00:00:00 2001 From: Surya Asriadie Date: Sun, 13 Mar 2022 04:09:07 +0900 Subject: [PATCH] Support insert on conflict (#8) * Suport insert on conflict * bump rel --- go.mod | 2 +- go.sum | 18 +++++++----------- nop_adapter_test.go | 4 ++-- primary_replica.go | 8 ++++---- primary_replica_test.go | 4 ++-- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 00e74b1..4b51ce5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/go-rel/primaryreplica go 1.15 require ( - github.com/go-rel/rel v0.32.0 + github.com/go-rel/rel v0.33.1 github.com/stretchr/testify v1.7.0 ) diff --git a/go.sum b/go.sum index 453324d..2d8609c 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,12 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/go-rel/rel v0.28.0/go.mod h1:zaIYPmM3AfJrh0xBmm7KoVKRgTNvr0cgZfcJ88gVA2U= -github.com/go-rel/rel v0.30.0/go.mod h1:Wu6HB6x0oIiEOdUJClwmn7ByCmdL73U5gItAYObyyuo= -github.com/go-rel/rel v0.31.0/go.mod h1:fCMlDiwDSSBSoZQ6ovUyjSqS2FfpLnhwr6tW9TgnhC8= -github.com/go-rel/rel v0.32.0 h1:Y0JeQ/ZqQDB3x9QkSn8HiLtvt+5oFuYgS0tj+px59KU= -github.com/go-rel/rel v0.32.0/go.mod h1:Ulbt8kctHMw3W1JBEflFkleOk+tIqmcKHY4r1eRuNSY= -github.com/go-rel/reltest v0.4.0/go.mod h1:3udgrKCZGCMFWc8k+RH975gUdUFPE/yspG2iWN9gjqU= -github.com/go-rel/reltest v0.5.0/go.mod h1:onixPXWMVL+KILH7OdUufKDm1KN0H6FzXOP7OoNC9qM= -github.com/go-rel/reltest v0.6.0/go.mod h1:4jR+L2vn7qe6NDW1carPomp2SlbTsQ9dqkxC+n4db+k= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-rel/rel v0.33.1 h1:yi3u6FDsQnOKZjo/DxEAC1WOLEjoas3fE1+Vyh6m6uY= +github.com/go-rel/rel v0.33.1/go.mod h1:DhB9Xmt/ymaumJAB6Z3Kq+IybLwQhhrzr6ZLeXMygPo= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -27,8 +22,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -45,6 +38,9 @@ github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+ github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= +github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/nop_adapter_test.go b/nop_adapter_test.go index f88f4ab..0b5ca56 100644 --- a/nop_adapter_test.go +++ b/nop_adapter_test.go @@ -37,11 +37,11 @@ func (na *nopAdapter) Delete(_ context.Context, _ rel.Query) (int, error) { return 1, na.retError } -func (na *nopAdapter) Insert(_ context.Context, _ rel.Query, _ string, _ map[string]rel.Mutate) (interface{}, error) { +func (na *nopAdapter) Insert(_ context.Context, _ rel.Query, _ string, _ map[string]rel.Mutate, _ rel.OnConflict) (interface{}, error) { return 1, na.retError } -func (na *nopAdapter) InsertAll(_ context.Context, _ rel.Query, _ string, _ []string, bulkMutates []map[string]rel.Mutate) ([]interface{}, error) { +func (na *nopAdapter) InsertAll(_ context.Context, _ rel.Query, _ string, _ []string, bulkMutates []map[string]rel.Mutate, _ rel.OnConflict) ([]interface{}, error) { var ( ids = make([]interface{}, len(bulkMutates)) ) diff --git a/primary_replica.go b/primary_replica.go index 9c3a09b..e0c7bc2 100644 --- a/primary_replica.go +++ b/primary_replica.go @@ -58,12 +58,12 @@ func (pr *PrimaryReplica) Exec(ctx context.Context, stmt string, args []interfac return pr.writeAdapter().Exec(ctx, stmt, args) } -func (pr *PrimaryReplica) Insert(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate) (interface{}, error) { - return pr.writeAdapter().Insert(ctx, query, primaryField, mutates) +func (pr *PrimaryReplica) Insert(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate, onConflict rel.OnConflict) (interface{}, error) { + return pr.writeAdapter().Insert(ctx, query, primaryField, mutates, onConflict) } -func (pr *PrimaryReplica) InsertAll(ctx context.Context, query rel.Query, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate) ([]interface{}, error) { - return pr.writeAdapter().InsertAll(ctx, query, primaryField, fields, bulkMutates) +func (pr *PrimaryReplica) InsertAll(ctx context.Context, query rel.Query, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate, onConflict rel.OnConflict) ([]interface{}, error) { + return pr.writeAdapter().InsertAll(ctx, query, primaryField, fields, bulkMutates, onConflict) } func (pr *PrimaryReplica) Update(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate) (int, error) { diff --git a/primary_replica_test.go b/primary_replica_test.go index b1a1d8d..02c2419 100644 --- a/primary_replica_test.go +++ b/primary_replica_test.go @@ -68,13 +68,13 @@ func TestAdapter_Exec(t *testing.T) { func TestAdapter_Insert(t *testing.T) { adapter := New(&nopAdapter{}, &nopAdapter{retError: errors.New("should not use replica")}) - _, err := adapter.Insert(context.TODO(), rel.From("users"), "id", nil) + _, err := adapter.Insert(context.TODO(), rel.From("users"), "id", nil, rel.OnConflict{}) assert.Nil(t, err) } func TestAdapter_InsertAll(t *testing.T) { adapter := New(&nopAdapter{}, &nopAdapter{retError: errors.New("should not use replica")}) - _, err := adapter.InsertAll(context.TODO(), rel.From("users"), "id", nil, nil) + _, err := adapter.InsertAll(context.TODO(), rel.From("users"), "id", nil, nil, rel.OnConflict{}) assert.Nil(t, err) }