Compare commits

..

No commits in common. "57729359d261e3b09239a6dd44e14e1777de6d56" and "bfa074fd9be4c2a259a16ca456c0a13796cc94a6" have entirely different histories.

3 changed files with 19 additions and 38 deletions

View File

@ -2,13 +2,12 @@ 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, config models.DatabaseConfig) error
Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) 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

View File

@ -2,12 +2,9 @@ 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"
@ -16,6 +13,9 @@ import (
gormPostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
logger2 "gorm.io/gorm/logger"
log2 "log"
"os"
"time"
)
//go:embed migrations/*.sql
@ -26,22 +26,23 @@ type postgresqlConnection struct {
debug bool
}
func NewPostgresqlConnection() OtterSpace {
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
return &postgresqlConnection{
db: nil,
db: nil,
debug: debugOutput,
}
}
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error {
var localSSL string
if config.SSL {
localSSL = "require"
} else {
localSSL = "disable"
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
}
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
log.Infof("OtterSpace: migration compleate")
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
SlowThreshold: 200 * time.Millisecond,
@ -58,14 +59,7 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database
return err
}
log.Infof("OtterSpace: database connection established")
err = p.migrateDatabase(db)
if err != nil {
return err
}
log.Infof("OtterSpace: migration compleate")
log.Infof("OtterSpace: postgres connection established")
return nil
}
@ -146,11 +140,11 @@ func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDoma
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
}
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB()
db, err := sql.Open(dialect, connectionString)
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
}

View File

@ -1,12 +0,0 @@
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"`
}