Compare commits
2 Commits
bfa074fd9b
...
57729359d2
Author | SHA1 | Date | |
---|---|---|---|
57729359d2 | |||
b40d93c993 |
@ -2,12 +2,13 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
||||
)
|
||||
|
||||
type OtterSpace interface {
|
||||
// Connect establishes a connection to the database.
|
||||
Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error
|
||||
Connect(ctx context.Context, config models.DatabaseConfig) error
|
||||
|
||||
// CreateUserWithRelationToSource adds a user with a relation to a source.
|
||||
CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error
|
||||
|
@ -2,9 +2,12 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
log2 "log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
|
||||
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
||||
_ "github.com/lib/pq"
|
||||
@ -13,9 +16,6 @@ import (
|
||||
gormPostgres "gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
logger2 "gorm.io/gorm/logger"
|
||||
log2 "log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
@ -26,23 +26,22 @@ type postgresqlConnection struct {
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
|
||||
func NewPostgresqlConnection() OtterSpace {
|
||||
return &postgresqlConnection{
|
||||
db: nil,
|
||||
debug: debugOutput,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error {
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone)
|
||||
var err error
|
||||
|
||||
err = p.migrateDatabase(dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error {
|
||||
var localSSL string
|
||||
if config.SSL {
|
||||
localSSL = "require"
|
||||
} else {
|
||||
localSSL = "disable"
|
||||
}
|
||||
|
||||
log.Infof("OtterSpace: migration compleate")
|
||||
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
|
||||
|
||||
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
|
||||
SlowThreshold: 200 * time.Millisecond,
|
||||
@ -59,7 +58,14 @@ func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, usern
|
||||
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
|
||||
}
|
||||
@ -140,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)
|
||||
}
|
||||
|
12
pkg/models/config.go
Normal file
12
pkg/models/config.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Endpoint string `env:"DB_ENDPOINT,required"`
|
||||
Username string `env:"DB_USERNAME,required"`
|
||||
Password string `env:"DB_PASSWORD,required,unset"`
|
||||
Database string `env:"DB_DATABASE,required"`
|
||||
Port int `env:"DB_PORT,required" envDefault:"5432"`
|
||||
SSL bool `env:"DB_SSL,required" envDefault:"true"`
|
||||
Timezone string `env:"DB_TIMEZONE,required" envDefault:"Europe/Berlin"`
|
||||
Debug bool `env:"DB_DEBUG" envDefault:"false"`
|
||||
}
|
Reference in New Issue
Block a user