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 ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
) )
type OtterSpace interface { type OtterSpace interface {
// Connect establishes a connection to the database. // 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 adds a user with a relation to a source.
CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error

View File

@ -2,12 +2,9 @@ package database
import ( import (
"context" "context"
"database/sql"
"embed" "embed"
"fmt" "fmt"
log2 "log"
"os"
"time"
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -16,6 +13,9 @@ import (
gormPostgres "gorm.io/driver/postgres" gormPostgres "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
logger2 "gorm.io/gorm/logger" logger2 "gorm.io/gorm/logger"
log2 "log"
"os"
"time"
) )
//go:embed migrations/*.sql //go:embed migrations/*.sql
@ -26,22 +26,23 @@ type postgresqlConnection struct {
debug bool debug bool
} }
func NewPostgresqlConnection() OtterSpace { func NewPostgresqlConnection(debugOutput bool) OtterSpace {
return &postgresqlConnection{ return &postgresqlConnection{
db: nil, db: nil,
debug: debugOutput,
} }
} }
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error { func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error {
var localSSL string dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone)
if config.SSL { var err error
localSSL = "require"
} else { err = p.migrateDatabase(dsn)
localSSL = "disable" 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) log.Infof("OtterSpace: migration compleate")
var err error
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{ dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
SlowThreshold: 200 * time.Millisecond, SlowThreshold: 200 * time.Millisecond,
@ -58,14 +59,7 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database
return err return err
} }
log.Infof("OtterSpace: database connection established") log.Infof("OtterSpace: postgres connection established")
err = p.migrateDatabase(db)
if err != nil {
return err
}
log.Infof("OtterSpace: migration compleate")
return nil return nil
} }
@ -146,11 +140,11 @@ func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDoma
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain) return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
} }
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
dialect := "postgres" dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB() db, err := sql.Open(dialect, connectionString)
if err != nil { if err != nil {
return fmt.Errorf("postgres migration: %v", err) 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"`
}