Files
netmaker/db/postgres.go
Vishal Dalwadi 7f40371ffd Task/DB-Migration: Add Key-Value to SQL Migration functionality. (#3380)
* feat(go): add db package;

* feat(go): add jobs table;

* feat(go): add schema migration facade;

* refactor(go): use custom key type to avoid collisions;
2025-04-12 14:07:57 +04:00

50 lines
1.1 KiB
Go

package db
import (
"fmt"
"github.com/gravitl/netmaker/servercfg"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// postgresConnector for initializing and
// connecting to a postgres database.
type postgresConnector struct{}
// postgresConnector.connect connects and
// initializes a connection to postgres.
func (pg *postgresConnector) connect() (*gorm.DB, error) {
pgConf := servercfg.GetSQLConf()
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=5",
pgConf.Host,
pgConf.Port,
pgConf.Username,
pgConf.Password,
pgConf.DB,
pgConf.SSLMode,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return nil, err
}
// ensure netmaker_v1 schema exists.
err = db.Exec("CREATE SCHEMA IF NOT EXISTS netmaker_v1").Error
if err != nil {
return nil, err
}
// set the netmaker_v1 schema as the default schema.
err = db.Exec("SET search_path TO netmaker_v1").Error
if err != nil {
return nil, err
}
return db, nil
}