Skip to content

Commit d53f042

Browse files
posgres support
1 parent a825810 commit d53f042

File tree

17 files changed

+1803
-1479
lines changed

17 files changed

+1803
-1479
lines changed

framework/configstore/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ConfigStoreType string
1111
// ConfigStoreTypeSQLite is the type of config store for SQLite.
1212
const (
1313
ConfigStoreTypeSQLite ConfigStoreType = "sqlite"
14+
ConfigStoreTypePostgres ConfigStoreType = "postgres"
1415
)
1516

1617
// Config represents the configuration for the config store.
@@ -51,7 +52,12 @@ func (c *Config) UnmarshalJSON(data []byte) error {
5152
return fmt.Errorf("failed to unmarshal sqlite config: %w", err)
5253
}
5354
c.Config = &sqliteConfig
54-
55+
case ConfigStoreTypePostgres:
56+
var postgresConfig PostgresConfig
57+
if err := json.Unmarshal(temp.Config, &postgresConfig); err != nil {
58+
return fmt.Errorf("failed to unmarshal postgres config: %w", err)
59+
}
60+
c.Config = &postgresConfig
5561
default:
5662
return fmt.Errorf("unknown config store type: %s", temp.Type)
5763
}

framework/configstore/postgres.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package configstore
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/maximhq/bifrost/core/schemas"
8+
"gorm.io/driver/postgres"
9+
"gorm.io/gorm"
10+
)
11+
12+
// PostgresConfig represents the configuration for a Postgres database.
13+
type PostgresConfig struct {
14+
Host string `json:"host"`
15+
Port string `json:"port"`
16+
User string `json:"user"`
17+
Password string `json:"password"`
18+
DBName string `json:"db_name"`
19+
SSLMode string `json:"ssl_mode"`
20+
}
21+
22+
// newPostgresConfigStore creates a new Postgres config store.
23+
func newPostgresConfigStore(ctx context.Context, config *PostgresConfig, logger schemas.Logger) (ConfigStore, error) {
24+
db, err := gorm.Open(postgres.Open(fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", config.Host, config.Port, config.User, config.Password, 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+
// Closing the DB connection
32+
if sqlDB, dbErr := db.DB(); dbErr == nil {
33+
if closeErr := sqlDB.Close(); closeErr != nil {
34+
logger.Error("failed to close DB connection: %v", closeErr)
35+
}
36+
}
37+
return nil, err
38+
}
39+
return d, nil
40+
}

0 commit comments

Comments
 (0)