133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"fmt"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/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"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database"
|
|
|
|
var (
|
|
//go:embed migrations/*.sql
|
|
embedMigrations embed.FS
|
|
client *gorm.DB
|
|
tracer trace.Tracer
|
|
logger = log.New()
|
|
)
|
|
|
|
func Connect(ctx context.Context, config models.DatabaseConfig) error {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect")
|
|
defer span.End()
|
|
|
|
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{})
|
|
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)
|
|
}
|
|
|
|
setupTelemetry()
|
|
|
|
client = sqlDB
|
|
utils.HandleEvent(span, localLogger, "Database connected successfully")
|
|
return nil
|
|
}
|
|
|
|
func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase")
|
|
defer span.End()
|
|
|
|
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 config.Debug {
|
|
if n != 0 {
|
|
localLogger.Infof("postgres migration: applied %d migrations!", n)
|
|
} else {
|
|
localLogger.Info("postgres migration: nothing to migrate")
|
|
}
|
|
}
|
|
|
|
utils.HandleEvent(span, localLogger, "Database migration completed successfully")
|
|
return nil
|
|
}
|
|
|
|
func setupTelemetry() {
|
|
tracer = otel.Tracer(tracingName)
|
|
|
|
hook := otellogrus.NewHook(tracingName)
|
|
logger.AddHook(hook)
|
|
}
|
|
|
|
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
|
|
}
|