Skip to content

Commit

Permalink
feat: postresql support extension and execute script;mongo support ex…
Browse files Browse the repository at this point in the history
…ecute script (#24)
  • Loading branch information
hysyeah authored Jun 7, 2024
1 parent 26ffbea commit c033613
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
25 changes: 22 additions & 3 deletions pkg/tapr/middleware_request_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
18 changes: 13 additions & 5 deletions pkg/tapr/middleware_request_tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,6 +35,12 @@ spec:
databases:
- distributed: true
name: mydb
extensions:
- vectors
- postgis
scripts:
- BEGIN
- COMMIT
password:
value: mypassword
user: myuser
Expand All @@ -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<redis> returned incorrect result.\nExpected:\n%s\nActual:\n%s", expected, result)
Expand All @@ -85,7 +90,10 @@ spec:
middleware: mongodb
mongodb:
databases:
- mydb
- name: mydb
scripts:
- BEGIN
- COMMIT
password:
value: mypassword
user: myuser
Expand Down
8 changes: 5 additions & 3 deletions pkg/tapr/middleware_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit c033613

Please sign in to comment.