golang-lib/database/main.go

106 lines
2.4 KiB
Go
Raw Normal View History

2017-05-17 16:17:02 +02:00
package database
import (
"net/url"
gormigrate "github.com/genofire/gormigrate/v2"
2021-06-01 10:51:35 +02:00
"gorm.io/driver/postgres"
2021-06-01 10:15:46 +02:00
"gorm.io/gorm"
2021-06-01 10:51:35 +02:00
"gorm.io/gorm/logger"
2017-05-17 16:17:02 +02:00
)
type ConnectionURI struct {
URI string `config:"string"`
Hostname string `config:"hostname"`
Username string `config:"username"`
Password string `config:"password"`
DatabaseName string `config:"dbname"`
ExtraOptions string `config:"extra_options"`
}
func (uri *ConnectionURI) String() string {
u,_ := url.Parse(uri.URI)
if u.Scheme == "" {
u.Scheme = "postgresql"
}
if uri.Hostname != "" {
u.Host = uri.Hostname
}
if uri.Username != "" && uri.Password != "" {
u.User = url.UserPassword(uri.Username, uri.Password)
}
if uri.DatabaseName != "" {
u.Path = uri.DatabaseName
}
if uri.ExtraOptions != "" {
u.RawQuery = uri.ExtraOptions
}
return u.String()
}
2021-06-01 17:41:05 +02:00
// Database struct to read from config
2021-06-01 10:51:35 +02:00
type Database struct {
2022-08-14 16:50:52 +02:00
DB *gorm.DB `config:"-" toml:"-"`
Connection ConnectionURI `config:"connection" toml:"connection"`
2022-08-14 16:50:52 +02:00
Debug bool `config:"debug" toml:"debug"`
Testdata bool `config:"testdata" toml:"testdata"`
LogLevel logger.LogLevel `config:"log_level" toml:"log_level"`
2021-06-01 10:51:35 +02:00
migrations map[string]*gormigrate.Migration
migrationTestdata map[string]*gormigrate.Migration
2017-05-17 16:17:02 +02:00
}
2021-06-01 17:41:05 +02:00
// Run database config - connect and migrate
2021-06-01 10:51:35 +02:00
func (config *Database) Run() error {
2021-07-14 12:29:58 +02:00
if err := config.run(); err != nil {
return err
}
return config.migrate(config.Testdata)
}
// ReRun database config - connect and re migration
func (config *Database) ReRun() error {
if err := config.run(); err != nil {
return err
}
m, err := config.setupMigrator(true)
if err != nil {
return err
}
if err := m.RollbackAll(); err != nil {
2021-07-14 12:29:58 +02:00
return err
}
return config.migrate(config.Testdata)
}
func (config *Database) run() error {
db, err := gorm.Open(postgres.Open(config.Connection.String()), &gorm.Config{
2021-06-01 10:51:35 +02:00
Logger: logger.Default.LogMode(config.LogLevel),
})
2017-05-17 16:17:02 +02:00
if err != nil {
2021-06-01 10:51:35 +02:00
return err
2017-05-17 16:17:02 +02:00
}
2021-06-01 10:51:35 +02:00
db.Debug().Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")
if config.Debug {
db = db.Debug()
2017-05-17 16:17:02 +02:00
}
2021-06-01 10:51:35 +02:00
config.DB = db
return nil
2017-05-17 16:17:02 +02:00
}
2021-06-01 17:41:05 +02:00
// Status get status - is database pingable
2021-06-01 10:51:35 +02:00
func (config *Database) Status() error {
2021-06-02 18:00:53 +02:00
if config.DB == nil {
return ErrNotConnected
}
2021-06-01 10:51:35 +02:00
sqlDB, err := config.DB.DB()
if err != nil {
return err
}
if err = sqlDB.Ping(); err != nil {
return err
}
return nil
2017-05-17 16:17:02 +02:00
}