Skip to content

Commit 8555779

Browse files
Merge pull request #8 from dipdup-io/GO-63-Add-Source-Config
Change Hasura configuration structure: * Now `source` is a structure but not string * Remove flag `add_source`
2 parents f305ca6 + 3487f43 commit 8555779

File tree

7 files changed

+73
-22
lines changed

7 files changed

+73
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*.so
66
*.dylib
77
.env
8+
.idea
9+
.DS_Store
810

911
# Test binary, built with `go test -c`
1012
*.test

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,8 @@ head, err := tzkt.GetHead(ctx)
233233
if err != nil {
234234
log.Panic(err)
235235
}
236-
```
236+
```
237+
238+
### `hasura`
239+
240+
Go wrapper for Hasura metadata methods, read docs [here](hasura/README.md)

config/config.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,28 @@ type Database struct {
5252

5353
// Hasura -
5454
type Hasura struct {
55-
URL string `yaml:"url" validate:"required,url"`
56-
Secret string `yaml:"admin_secret" validate:"required"`
57-
Source string `yaml:"source" validate:"omitempty"`
58-
RowsLimit uint64 `yaml:"select_limit" validate:"gt=0"`
59-
EnableAggregations bool `yaml:"allow_aggregation"`
60-
AddSource bool `yaml:"add_source"`
61-
Rest *bool `yaml:"rest"`
55+
URL string `yaml:"url" validate:"required,url"`
56+
Secret string `yaml:"admin_secret" validate:"required"`
57+
RowsLimit uint64 `yaml:"select_limit" validate:"gt=0"`
58+
EnableAggregations bool `yaml:"allow_aggregation"`
59+
Source *HasuraSource `yaml:"source"`
60+
Rest *bool `yaml:"rest"`
61+
}
62+
63+
type HasuraSource struct {
64+
Name string `yaml:"name" validate:"required"`
65+
DatabaseHost string `yaml:"database_host"`
66+
UsePreparedStatements bool `yaml:"use_prepared_statements"`
67+
IsolationLevel string `yaml:"isolation_level"`
6268
}
6369

6470
// UnmarshalYAML -
6571
func (h *Hasura) UnmarshalYAML(unmarshal func(interface{}) error) error {
66-
h.Source = "default"
72+
h.Source = &HasuraSource{
73+
Name: "default",
74+
UsePreparedStatements: false,
75+
IsolationLevel: "read-committed",
76+
}
6777

6878
type plain Hasura
6979
return unmarshal((*plain)(h))

hasura/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Go wrapper for Hasura metadata methods
2+
3+
Golang's library for registration data source in Hasura service
4+
5+
## Configuration
6+
7+
```yaml
8+
hasura:
9+
url: string # hasura url, required
10+
admin_secret: string # required
11+
select_limit: int
12+
allow_aggregation: bool
13+
source:
14+
name: string # name of data source, required. For more info, [hasura docs](https://hasura.io/docs/latest/api-reference/metadata-api/source/#metadata-pg-add-source-syntax).
15+
database_host: string # host of datasource, if omitted, used host from database config
16+
use_prepared_statements: bool # if set to true the server prepares statement before executing on the source database (default: false)
17+
isolation_level: bool # The transaction isolation level in which the queries made to the source will be run with (options: read-committed | repeatable-read | serializable) (default: read-committed)
18+
rest: bool # should REST endpoints be created?
19+
```

hasura/api.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (api *API) get(ctx context.Context, endpoint string, args map[string]string
6666
return api.client.Do(req)
6767
}
6868

69-
//nolint
69+
// nolint
7070
func (api *API) post(ctx context.Context, endpoint string, args map[string]string, body interface{}, output interface{}) error {
7171
url, err := api.buildURL(endpoint, args)
7272
if err != nil {
@@ -124,15 +124,27 @@ func (api *API) Health(ctx context.Context) error {
124124

125125
// AddSource -
126126
func (api *API) AddSource(ctx context.Context, hasura *config.Hasura, cfg config.Database) error {
127+
host := cfg.Host
128+
if hasura.Source.DatabaseHost != "" {
129+
host = hasura.Source.DatabaseHost
130+
}
131+
132+
databaseUrl := DatabaseUrl(fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", cfg.User, cfg.Password, host, cfg.Port, cfg.Database))
133+
134+
isolationLevel := "read-committed"
135+
if hasura.Source.IsolationLevel != "" {
136+
isolationLevel = hasura.Source.IsolationLevel
137+
}
138+
127139
req := Request{
128140
Type: "pg_add_source",
129141
Args: map[string]interface{}{
130-
"name": hasura.Source,
142+
"name": hasura.Source.Name,
131143
"configuration": Configuration{
132144
ConnectionInfo: ConnectionInfo{
133-
DatabaseUrl: DatabaseUrl(fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database)),
134-
UsePreparedStatements: true,
135-
IsolationLevel: "read-committed",
145+
DatabaseUrl: databaseUrl,
146+
UsePreparedStatements: hasura.Source.UsePreparedStatements,
147+
IsolationLevel: isolationLevel,
136148
},
137149
},
138150
"replace_configuration": true,

hasura/hasura.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Create(ctx context.Context, args GenerateArgs) error {
6464

6565
checkHealth(ctx, api)
6666

67-
if args.Config.AddSource {
67+
if args.Config.Source != nil {
6868
log.Info().Msg("Adding source...")
6969
if err := api.AddSource(ctx, args.Config, args.DatabaseConfig); err != nil {
7070
return err
@@ -85,13 +85,13 @@ func Create(ctx context.Context, args GenerateArgs) error {
8585
// Find our source in the existing metadata
8686
var selectedSource *Source = nil
8787
for idx := range export.Sources {
88-
if export.Sources[idx].Name == args.Config.Source {
88+
if export.Sources[idx].Name == args.Config.Source.Name {
8989
selectedSource = &export.Sources[idx]
9090
break
9191
}
9292
}
9393
if selectedSource == nil {
94-
return errors.Errorf("Source '%s' not found on exported metadata", args.Config.Source)
94+
return errors.Errorf("Source '%s' not found on exported metadata", args.Config.Source.Name)
9595
}
9696

9797
log.Info().Msg("Merging metadata...")
@@ -123,15 +123,15 @@ func Create(ctx context.Context, args GenerateArgs) error {
123123

124124
log.Info().Msg("Tracking views...")
125125
for i := range args.Views {
126-
if err := api.TrackTable(ctx, args.Views[i], args.Config.Source); err != nil {
126+
if err := api.TrackTable(ctx, args.Views[i], args.Config.Source.Name); err != nil {
127127
if !strings.Contains(err.Error(), "view/table already tracked") {
128128
return err
129129
}
130130
}
131-
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source, "user"); err != nil {
131+
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user"); err != nil {
132132
log.Warn().Err(err).Msg("")
133133
}
134-
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source, "user", Permission{
134+
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user", Permission{
135135
Limit: args.Config.RowsLimit,
136136
AllowAggs: args.Config.EnableAggregations,
137137
Columns: Columns{"*"},
@@ -155,7 +155,7 @@ func Create(ctx context.Context, args GenerateArgs) error {
155155
func Generate(hasura config.Hasura, cfg config.Database, models ...interface{}) (*Metadata, error) {
156156
schema := getSchema(cfg)
157157
source := Source{
158-
Name: hasura.Source,
158+
Name: hasura.Source.Name,
159159
Tables: make([]Table, 0),
160160
}
161161
for _, model := range models {

hasura/hasura_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ func TestGenerate(t *testing.T) {
142142
hasura: config.Hasura{
143143
EnableAggregations: true,
144144
RowsLimit: 5,
145-
Source: "mysql",
145+
Source: &config.HasuraSource{
146+
Name: "mysql",
147+
UsePreparedStatements: true,
148+
IsolationLevel: "read-committed",
149+
},
146150
},
147151
models: []interface{}{
148152
&testTable{}, &testTable2{}, &testTable3{}, &testTable4{},

0 commit comments

Comments
 (0)