Skip to content

Commit 17c4d8a

Browse files
mysql support
1 parent d470753 commit 17c4d8a

File tree

6 files changed

+153
-3
lines changed

6 files changed

+153
-3
lines changed

framework/configstore/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ConfigStoreType string
1212
const (
1313
ConfigStoreTypeSQLite ConfigStoreType = "sqlite"
1414
ConfigStoreTypePostgres ConfigStoreType = "postgres"
15+
ConfigStoreTypeMySQL ConfigStoreType = "mysql"
1516
)
1617

1718
// Config represents the configuration for the config store.
@@ -58,6 +59,12 @@ func (c *Config) UnmarshalJSON(data []byte) error {
5859
return fmt.Errorf("failed to unmarshal postgres config: %w", err)
5960
}
6061
c.Config = &postgresConfig
62+
case ConfigStoreTypeMySQL:
63+
var mysqlConfig MySQLConfig
64+
if err := json.Unmarshal(temp.Config, &mysqlConfig); err != nil {
65+
return fmt.Errorf("failed to unmarshal mysql config: %w", err)
66+
}
67+
c.Config = &mysqlConfig
6168
default:
6269
return fmt.Errorf("unknown config store type: %s", temp.Type)
6370
}

framework/configstore/mysql.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package configstore
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/maximhq/bifrost/core/schemas"
8+
"gorm.io/driver/mysql"
9+
"gorm.io/gorm"
10+
)
11+
12+
// MySQLConfig represents the configuration for a MySQL database.
13+
type MySQLConfig struct {
14+
User string `json:"user"`
15+
Password string `json:"password"`
16+
Host string `json:"host"`
17+
Port int `json:"port"`
18+
DBName string `json:"db_name"`
19+
SSLMode string `json:"ssl_mode"`
20+
}
21+
22+
// newMySQLConfigStore creates a new MySQL config store.
23+
func newMySQLConfigStore(ctx context.Context, config *MySQLConfig, logger schemas.Logger) (ConfigStore, error) {
24+
db, err := gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&sslmode=%s", config.User, config.Password, config.Host, config.Port, config.DBName, config.SSLMode)), &gorm.Config{})
25+
if err != nil {
26+
return nil, err
27+
}
28+
d := &RDBConfigStore{db: db, logger: logger}
29+
// Run migrations
30+
if err := triggerMigrations(ctx, db); err != nil {
31+
return nil, err
32+
}
33+
return d, nil
34+
}

framework/configstore/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func NewConfigStore(ctx context.Context, config *Config, logger schemas.Logger)
130130
return newPostgresConfigStore(ctx, postgresConfig, logger)
131131
}
132132
return nil, fmt.Errorf("invalid postgres config: %T", config.Config)
133+
case ConfigStoreTypeMySQL:
134+
if mysqlConfig, ok := config.Config.(*MySQLConfig); ok {
135+
return newMySQLConfigStore(ctx, mysqlConfig, logger)
136+
}
137+
return nil, fmt.Errorf("invalid mysql config: %T", config.Config)
133138
}
134139
return nil, fmt.Errorf("unsupported config store type: %s", config.Type)
135140
}

framework/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ require (
1616
)
1717

1818
require (
19+
filippo.io/edwards25519 v1.1.0 // indirect
20+
github.com/go-sql-driver/mysql v1.8.1 // indirect
1921
github.com/jackc/pgpassfile v1.0.0 // indirect
2022
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
2123
github.com/jackc/pgx/v5 v5.6.0 // indirect
@@ -93,5 +95,6 @@ require (
9395
google.golang.org/protobuf v1.36.8 // indirect
9496
gopkg.in/yaml.v2 v2.4.0 // indirect
9597
gopkg.in/yaml.v3 v3.0.1 // indirect
98+
gorm.io/driver/mysql v1.6.0
9699
gorm.io/driver/postgres v1.6.0
97100
)

framework/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cloud.google.com/go/compute/metadata v0.8.0 h1:HxMRIbao8w17ZX6wBnjhcDkW6lTFpgcaobyVfZWqRLA=
22
cloud.google.com/go/compute/metadata v0.8.0/go.mod h1:sYOGTp851OV9bOFJ9CH7elVvyzopvWQFNNghtDQ/Biw=
3+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
4+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
35
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
46
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
57
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
@@ -102,6 +104,8 @@ github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ
102104
github.com/go-openapi/validate v0.21.0/go.mod h1:rjnrwK57VJ7A8xqfpAOEKRH8yQSGUriMu5/zuPSQ1hg=
103105
github.com/go-openapi/validate v0.24.0 h1:LdfDKwNbpB6Vn40xhTdNZAnfLECL81w+VX3BumrGD58=
104106
github.com/go-openapi/validate v0.24.0/go.mod h1:iyeX1sEufmv3nPbBdX3ieNviWnOZaJ1+zquzJEf2BAQ=
107+
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
108+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
105109
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
106110
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
107111
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
@@ -360,6 +364,8 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C
360364
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
361365
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
362366
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
367+
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
368+
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
363369
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
364370
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
365371
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=

transports/config.schema.json

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://json-schema.org/draft/2019-09/schema",
3-
"$id": "https://www.getbifrost.ai/schema",
3+
"$id": "https://www.getbifrost.ai/schema",
44
"title": "Bifrost Configuration Schema",
55
"description": "Schema for Bifrost HTTP transport configuration",
66
"type": "object",
@@ -175,7 +175,8 @@
175175
"type": "string",
176176
"enum": [
177177
"sqlite",
178-
"postgres"
178+
"postgres",
179+
"mysql"
179180
],
180181
"description": "Configuration store type"
181182
},
@@ -249,6 +250,53 @@
249250
],
250251
"additionalProperties": false
251252
}
253+
},
254+
{
255+
"if": {
256+
"properties": {
257+
"type": {
258+
"const": "mysql"
259+
}
260+
}
261+
},
262+
"then": {
263+
"type": "object",
264+
"properties": {
265+
"host": {
266+
"type": "string",
267+
"description": "Database host"
268+
},
269+
"port": {
270+
"type": "string",
271+
"description": "Database port"
272+
},
273+
"user": {
274+
"type": "string",
275+
"description": "Database user"
276+
},
277+
"password": {
278+
"type": "string",
279+
"description": "Database password. Leave empty if you want to use IAM role authentication."
280+
},
281+
"db_name": {
282+
"type": "string",
283+
"description": "Database name"
284+
},
285+
"ssl_mode": {
286+
"type": "string",
287+
"description": "Database SSL mode"
288+
}
289+
},
290+
"required": [
291+
"host",
292+
"port",
293+
"user",
294+
"password",
295+
"db_name",
296+
"ssl_mode"
297+
],
298+
"additionalProperties": false
299+
}
252300
}
253301
]
254302
}
@@ -267,7 +315,8 @@
267315
"type": "string",
268316
"enum": [
269317
"sqlite",
270-
"postgres"
318+
"postgres",
319+
"mysql"
271320
],
272321
"description": "Logs store type"
273322
},
@@ -340,6 +389,52 @@
340389
],
341390
"additionalProperties": false
342391
}
392+
},
393+
{
394+
"if": {
395+
"properties": {
396+
"../type": {
397+
"const": "mysql"
398+
}
399+
}
400+
},
401+
"then": {
402+
"properties": {
403+
"host": {
404+
"type": "string",
405+
"description": "Database host"
406+
},
407+
"port": {
408+
"type": "integer",
409+
"description": "Database port"
410+
},
411+
"user": {
412+
"type": "string",
413+
"description": "Database user"
414+
},
415+
"password": {
416+
"type": "string",
417+
"description": "Database password. Leave empty if you want to use IAM role authentication."
418+
},
419+
"db_name": {
420+
"type": "string",
421+
"description": "Database name"
422+
},
423+
"ssl_mode": {
424+
"type": "string",
425+
"description": "Database SSL mode"
426+
}
427+
},
428+
"required": [
429+
"host",
430+
"port",
431+
"user",
432+
"password",
433+
"db_name",
434+
"ssl_mode"
435+
],
436+
"additionalProperties": false
437+
}
343438
}
344439
]
345440
}

0 commit comments

Comments
 (0)