feat(postgres): using only gorm
All checks were successful
Gitea Build Check / Build (push) Successful in 10m40s

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-24 13:13:32 +02:00
parent b40d93c993
commit 57729359d2

View File

@ -2,7 +2,6 @@ package database
import (
"context"
"database/sql"
"embed"
"fmt"
log2 "log"
@ -27,10 +26,9 @@ type postgresqlConnection struct {
debug bool
}
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
func NewPostgresqlConnection() OtterSpace {
return &postgresqlConnection{
db: nil,
debug: debugOutput,
db: nil,
}
}
@ -45,13 +43,6 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone)
var err error
err = p.migrateDatabase(dsn)
if err != nil {
return err
}
log.Infof("OtterSpace: migration compleate")
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger2.Warn,
@ -67,7 +58,14 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database
return err
}
log.Infof("OtterSpace: postgres connection established")
log.Infof("OtterSpace: database connection established")
err = p.migrateDatabase(db)
if err != nil {
return err
}
log.Infof("OtterSpace: migration compleate")
return nil
}
@ -148,11 +146,11 @@ func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDoma
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
}
func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := sql.Open(dialect, connectionString)
db, err := dbPool.DB()
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
}