Compare commits
2 Commits
bfa074fd9b
...
57729359d2
Author | SHA1 | Date | |
---|---|---|---|
57729359d2 | |||
b40d93c993 |
@ -2,12 +2,13 @@ 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, 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 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
|
||||||
|
@ -2,9 +2,12 @@ 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"
|
||||||
@ -13,9 +16,6 @@ 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,23 +26,22 @@ type postgresqlConnection struct {
|
|||||||
debug bool
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
|
func NewPostgresqlConnection() OtterSpace {
|
||||||
return &postgresqlConnection{
|
return &postgresqlConnection{
|
||||||
db: nil,
|
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 {
|
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) 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 localSSL string
|
||||||
var err error
|
if config.SSL {
|
||||||
|
localSSL = "require"
|
||||||
err = p.migrateDatabase(dsn)
|
} else {
|
||||||
if err != nil {
|
localSSL = "disable"
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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{
|
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
|
||||||
SlowThreshold: 200 * time.Millisecond,
|
SlowThreshold: 200 * time.Millisecond,
|
||||||
@ -59,7 +58,14 @@ func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, usern
|
|||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@ -140,11 +146,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(connectionString string) error {
|
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
|
||||||
dialect := "postgres"
|
dialect := "postgres"
|
||||||
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
||||||
|
|
||||||
db, err := sql.Open(dialect, connectionString)
|
db, err := dbPool.DB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("postgres migration: %v", err)
|
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