From c03361373f7622ca29d2e58a49bc803fcee6e35a Mon Sep 17 00:00:00 2001 From: hysyeah Date: Fri, 7 Jun 2024 15:20:05 +0800 Subject: [PATCH] feat: postresql support extension and execute script;mongo support execute script (#24) --- pkg/tapr/middleware_request_tpl.go | 25 ++++++++++++++++++++++--- pkg/tapr/middleware_request_tpl_test.go | 18 +++++++++++++----- pkg/tapr/middleware_types.go | 8 +++++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/pkg/tapr/middleware_request_tpl.go b/pkg/tapr/middleware_request_tpl.go index f477e03..f2cc6c9 100644 --- a/pkg/tapr/middleware_request_tpl.go +++ b/pkg/tapr/middleware_request_tpl.go @@ -19,7 +19,20 @@ spec: {{- range $k, $v := .Middleware.Databases }} - distributed: {{ $v.Distributed }} name: {{ $v.Name }} - {{- end }} + {{- if gt (len $v.Extensions) 0 }} + extensions: + {{- range $i, $ext := $v.Extensions }} + - {{ $ext }} + {{- end }} + {{- end }} + {{- if gt (len $v.Scripts) 0 }} + scripts: + {{- range $i, $s := $v.Scripts }} + - '{{ $s }}' + {{- end }} + {{- end }} + + {{- end }} password: {{- if not (eq .Middleware.Password "") }} value: {{ .Middleware.Password }} @@ -66,8 +79,14 @@ spec: mongodb: databases: {{- range $k, $v := .Middleware.Databases }} - - {{ $v.Name }} - {{- end }} + - name: {{ $v.Name }} + {{- if gt (len $v.Scripts) 0 }} + scripts: + {{- range $i, $s := $v.Scripts }} + - '{{ $s }}' + {{- end }} + {{- end }} + {{- end }} password: {{- if not (eq .Middleware.Password "") }} value: {{ .Middleware.Password }} diff --git a/pkg/tapr/middleware_request_tpl_test.go b/pkg/tapr/middleware_request_tpl_test.go index 210a508..551683c 100644 --- a/pkg/tapr/middleware_request_tpl_test.go +++ b/pkg/tapr/middleware_request_tpl_test.go @@ -15,7 +15,8 @@ func TestGenMiddleRequest(t *testing.T) { password = "mypassword" ) // test genPostgresRequest function - databases := []Database{{Name: "mydb", Distributed: true}} + databases := []Database{{Name: "mydb", Distributed: true, + Extensions: []string{"vectors", "postgis"}, Scripts: []string{"BEGIN", "COMMIT"}}} result, err := GenMiddleRequest(middleware, appName, appNamespace, namespace, username, password, databases, []Index{}) if err != nil { @@ -34,6 +35,12 @@ spec: databases: - distributed: true name: mydb + extensions: + - vectors + - postgis + scripts: + - BEGIN + - COMMIT password: value: mypassword user: myuser @@ -58,11 +65,9 @@ spec: appNamespace: mynamespace middleware: redis redis: - databases: - - mydb + namespace: mydb password: value: mypassword - user: myuser `) if !bytes.Equal(result, expected) { t.Errorf("GenMiddleRequest returned incorrect result.\nExpected:\n%s\nActual:\n%s", expected, result) @@ -85,7 +90,10 @@ spec: middleware: mongodb mongodb: databases: - - mydb + - name: mydb + scripts: + - BEGIN + - COMMIT password: value: mypassword user: myuser diff --git a/pkg/tapr/middleware_types.go b/pkg/tapr/middleware_types.go index 254c24b..4f103ce 100644 --- a/pkg/tapr/middleware_types.go +++ b/pkg/tapr/middleware_types.go @@ -10,14 +10,16 @@ type Middleware struct { // Database specify database name and if distributed. type Database struct { - Name string `json:"name"` - Distributed bool `json:"distributed,omitempty"` + Name string `yaml:"name" json:"name"` + Extensions []string `yaml:"extensions,omitempty" json:"extensions,omitempty"` + Scripts []string `yaml:"scripts,omitempty" json:"scripts,omitempty"` + Distributed bool `yaml:"distributed,omitempty" json:"distributed,omitempty"` } // PostgresConfig contains fields for postgresql config. type PostgresConfig struct { Username string `yaml:"username" json:"username"` - Password string `yaml:"password,omitempty" json:"password"` + Password string `yaml:"password,omitempty" json:"password,omitempty"` Databases []Database `yaml:"databases" json:"databases"` }