mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-26 21:01:32 +08:00
feat(go): use the same schema;
This commit is contained in:
@@ -2,6 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ type postgresConnector struct{}
|
|||||||
// postgresConnector.connect connects and
|
// postgresConnector.connect connects and
|
||||||
// initializes a connection to postgres.
|
// initializes a connection to postgres.
|
||||||
func (pg *postgresConnector) connect() (*gorm.DB, error) {
|
func (pg *postgresConnector) connect() (*gorm.DB, error) {
|
||||||
pgConf := GetSQLConf()
|
pgConf := servercfg.GetSQLConf()
|
||||||
dsn := fmt.Sprintf(
|
dsn := fmt.Sprintf(
|
||||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=5",
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=5",
|
||||||
pgConf.Host,
|
pgConf.Host,
|
||||||
@@ -29,27 +30,11 @@ func (pg *postgresConnector) connect() (*gorm.DB, error) {
|
|||||||
pgConf.SSLMode,
|
pgConf.SSLMode,
|
||||||
)
|
)
|
||||||
|
|
||||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
Logger: logger.Default.LogMode(logger.Silent),
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSQLConf() config.SQLConfig {
|
func GetSQLConf() config.SQLConfig {
|
||||||
var cfg config.SQLConfig
|
var cfg config.SQLConfig
|
||||||
cfg.Host = GetSQLHost()
|
cfg.Host = GetSQLHost()
|
||||||
|
Reference in New Issue
Block a user