mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
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;
This commit is contained in:
49
db/postgres.go
Normal file
49
db/postgres.go
Normal file
@@ -0,0 +1,49 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user