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

139 lines
3.7 KiB
Go
Raw Normal View History

package database
import (
"context"
"embed"
"fmt"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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/v3/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,
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
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
}
2024-08-10 22:26:20 +00:00
// GetGorm returns a ready to use gorm.DB client
2024-08-10 22:26:20 +00:00
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")
2024-08-10 22:26:20 +00:00
if client == nil {
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
2024-08-10 22:26:20 +00:00
}
utils.HandleEvent(span, localLogger, "GORM client retrieved successfully")
2024-08-10 22:26:20 +00:00
return client, nil
}