otter-space-sdk/pkg/database/client.go

135 lines
3.6 KiB
Go

package database
import (
"context"
"embed"
"fmt"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
var (
//go:embed migrations/*.sql
embedMigrations embed.FS
client *gorm.DB
tracer = otel.Tracer(tracingName)
logger = log.New()
)
// Connect to the Database
func Connect(ctx context.Context, config models.DatabaseConfig) error {
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
// Debug enabled?
if config.Debug {
log.SetLevel(log.DebugLevel)
}
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"endpoint": config.Endpoint,
"port": config.Port,
"database": config.Database,
})
span.SetAttributes(
attribute.String("endpoint", config.Endpoint),
attribute.Int("port", config.Port),
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database connection")
var localSSL string
if config.SSL {
localSSL = "require"
} else {
localSSL = "disable"
}
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)
sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
TranslateError: true,
})
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
err = migrateDatabase(ctx, sqlDB, config)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
client = sqlDB
utils.HandleEvent(span, localLogger, "Database connected successfully")
return nil
}
// migrateDatabase handels the migration of ann SQL files in the migrations subfolder
func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"database": config.Database,
})
span.SetAttributes(
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database migration")
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB()
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
if n != 0 {
localLogger.Debugf("applied %d migrations!", n)
} else {
localLogger.Debug("nothing to migrate")
}
utils.HandleEvent(span, localLogger, "Database migration completed successfully")
return nil
}
// GetGorm returns a ready to use gorm.DB client
func GetGorm(ctx context.Context) (*gorm.DB, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm")
defer span.End()
utils.HandleEvent(span, localLogger, "Retrieving GORM client")
if client == nil {
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
utils.HandleEvent(span, localLogger, "GORM client retrieved successfully")
return client, nil
}