Skip to content

Commit 41734f2

Browse files
mysql support
1 parent 5114e45 commit 41734f2

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
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+
Username string `json:"username"`
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.Username, 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/postgres.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configstore
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/maximhq/bifrost/core/schemas"
78
"gorm.io/driver/postgres"
@@ -10,12 +11,19 @@ import (
1011

1112
// PostgresConfig represents the configuration for a Postgres database.
1213
type PostgresConfig struct {
13-
DSN string `json:"dsn"`
14+
Username string `json:"username"`
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"`
1420
}
1521

1622
// newPostgresConfigStore creates a new Postgres config store.
1723
func newPostgresConfigStore(ctx context.Context, config *PostgresConfig, logger schemas.Logger) (ConfigStore, error) {
18-
db, err := gorm.Open(postgres.Open(config.DSN), &gorm.Config{})
24+
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
25+
config.Host, config.Username, config.Password, config.DBName, config.Port, config.SSLMode)
26+
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
1927
if err != nil {
2028
return nil, err
2129
}

framework/configstore/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ func NewConfigStore(ctx context.Context, config *Config, logger schemas.Logger)
123123
return newPostgresConfigStore(ctx, postgresConfig, logger)
124124
}
125125
return nil, fmt.Errorf("invalid postgres config: %T", config.Config)
126+
case ConfigStoreTypeMySQL:
127+
if mysqlConfig, ok := config.Config.(*MySQLConfig); ok {
128+
return newMySQLConfigStore(ctx, mysqlConfig, logger)
129+
}
130+
return nil, fmt.Errorf("invalid mysql config: %T", config.Config)
126131
}
127132
return nil, fmt.Errorf("unsupported config store type: %s", config.Type)
128133
}

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=

0 commit comments

Comments
 (0)