From efbfbbb3b58e01d63feea0c62d6beb19f77a2adf Mon Sep 17 00:00:00 2001 From: Haris Sudrajat Date: Wed, 29 May 2024 13:42:18 +0700 Subject: [PATCH 1/2] task-queue-worker: bugfix command to get mongo version --- codebase/app/task_queue_worker/persistent_mongo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codebase/app/task_queue_worker/persistent_mongo.go b/codebase/app/task_queue_worker/persistent_mongo.go index c5ea0700..3ae13a3b 100644 --- a/codebase/app/task_queue_worker/persistent_mongo.go +++ b/codebase/app/task_queue_worker/persistent_mongo.go @@ -597,7 +597,7 @@ func (s *MongoPersistent) Type() string { var commandResult struct { Version string `bson:"version"` } - err := s.db.RunCommand(s.ctx, bson.D{{Key: "serverStatus", Value: 1}}).Decode(&commandResult) + err := s.db.RunCommand(s.ctx, bson.D{{Key: "buildInfo", Value: 1}}).Decode(&commandResult) logger.LogIfError(err) if commandResult.Version != "" { commandResult.Version = ", version: " + commandResult.Version From be9204e64648d10397a5764431ed2e948f68526a Mon Sep 17 00:00:00 2001 From: Haris Sudrajat Date: Sun, 2 Jun 2024 23:20:57 +0700 Subject: [PATCH 2/2] database_update_tool: add function to extract sql tag --- candishared/database_update_tools.go | 9 +++ candishared/database_update_tools_test.go | 67 +++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/candishared/database_update_tools.go b/candishared/database_update_tools.go index 40aa0a1d..280d7143 100644 --- a/candishared/database_update_tools.go +++ b/candishared/database_update_tools.go @@ -58,6 +58,15 @@ func DBUpdateMongoExtractorKey(structField reflect.StructField) (string, bool) { return candihelper.ToDelimited(structField.Name, '_'), false } +// DBUpdateSqlExtractorKey struct field key extractor for mongo model +func DBUpdateSqlExtractorKey(structField reflect.StructField) (string, bool) { + sqlTag := structField.Tag.Get("sql") + if strings.HasPrefix(sqlTag, "column:") { + return strings.Split(strings.TrimPrefix(sqlTag, "column:"), ";")[0], false + } + return candihelper.ToDelimited(structField.Name, '_'), false +} + // DBUpdateTools for construct selected field to update type DBUpdateTools struct { KeyExtractorFunc func(structTag reflect.StructField) (key string, mustSet bool) diff --git a/candishared/database_update_tools_test.go b/candishared/database_update_tools_test.go index 5768c190..edab7dd1 100644 --- a/candishared/database_update_tools_test.go +++ b/candishared/database_update_tools_test.go @@ -114,3 +114,70 @@ func TestDBUpdateToolsMongo(t *testing.T) { ) assert.Equal(t, 9, len(updated)) } + +func TestDBUpdateSqlExtractorKey(t *testing.T) { + type SubModel struct { + Title string `sql:"column:title" json:"title"` + Profile string `sql:"column:profile" json:"profile"` + ActivatedAt sql.NullTime `sql:"column:activated_at" json:"activatedAt"` + CityAddress string `sql:"type:text"` + } + type Model struct { + ID int `sql:"column:id;" json:"id"` + Name *string `sql:"column:name;" json:"name"` + Address string `sql:"column:alamat" json:"address"` + No int + CreatedAt time.Time + IgnoreMe SubModel `sql:"column:test" json:"ignoreMe" ignoreUpdate:"true"` + Rel SubModel `sql:"foreignKey:ID" json:"rel"` + Log []byte + StrArr pq.StringArray + IntArr pq.Int64Array + Ch chan string + Multi []SubModel + Map map[int]SubModel + PtrModel *SubModel + DeletedAt *time.Time + NamedArg *sql.NamedArg + SubModel + } + var updated map[string]any + + updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap( + &Model{ID: 1, Name: candihelper.ToStringPtr("01"), Address: "street", + SubModel: SubModel{Title: "test", CityAddress: "Jakarta"}, Rel: SubModel{Title: "rel sub"}}, + DBUpdateSetUpdatedFields("ID", "Name", "Title", "CityAddress", "Address"), + ) + + assert.Equal(t, 5, len(updated)) + assert.Equal(t, 1, updated["id"]) + assert.Equal(t, "01", updated["name"]) + assert.Equal(t, "test", updated["title"]) + assert.Equal(t, "Jakarta", updated["city_address"]) + + updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap( + Model{ID: 1, Name: candihelper.ToStringPtr("01"), Address: "street", SubModel: SubModel{Title: "test", ActivatedAt: sql.NullTime{Valid: true}}}, + DBUpdateSetIgnoredFields("ID", "Name", "Title"), + ) + assert.Equal(t, 10, len(updated)) + assert.Equal(t, "street", updated["alamat"]) + + updated = DBUpdateTools{KeyExtractorFunc: DBUpdateSqlExtractorKey}.ToMap( + Model{ + No: 10, + Multi: make([]SubModel, 1), + Rel: SubModel{Title: "001"}, + CreatedAt: time.Now(), + SubModel: SubModel{ActivatedAt: sql.NullTime{Valid: true, Time: time.Now()}, CityAddress: "Jakarta"}, + PtrModel: &SubModel{CityAddress: "New"}, + Log: []byte(`123`), + StrArr: pq.StringArray{"1", "2", "3"}, + IntArr: pq.Int64Array{1, 2, 3}, + }, + ) + assert.Equal(t, 13, len(updated)) + assert.Equal(t, "Jakarta", updated["city_address"]) + assert.Equal(t, 10, updated["no"]) + assert.Equal(t, "{\"1\",\"2\",\"3\"}", updated["str_arr"]) + assert.Equal(t, []byte(`123`), updated["log"]) +}