2024-08-09 19:39:22 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
2024-08-12 09:50:45 +00:00
|
|
|
|
2024-08-29 13:02:39 +00:00
|
|
|
"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"
|
2024-08-09 19:39:22 +00:00
|
|
|
migrate "github.com/rubenv/sql-migrate"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-08-11 20:17:00 +00:00
|
|
|
"go.opentelemetry.io/contrib/bridges/otellogrus"
|
|
|
|
"go.opentelemetry.io/otel"
|
2024-08-12 09:50:45 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-08-09 19:39:22 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-08-29 13:02:39 +00:00
|
|
|
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
|
2024-08-11 20:17:00 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed migrations/*.sql
|
|
|
|
embedMigrations embed.FS
|
|
|
|
client *gorm.DB
|
2024-08-29 08:44:40 +00:00
|
|
|
tracer = otel.Tracer(tracingName)
|
2024-08-12 09:50:45 +00:00
|
|
|
logger = log.New()
|
2024-08-11 20:17:00 +00:00
|
|
|
)
|
2024-08-09 19:39:22 +00:00
|
|
|
|
2024-08-13 08:54:21 +00:00
|
|
|
// Connect to the Database
|
2024-08-12 09:50:45 +00:00
|
|
|
func Connect(ctx context.Context, config models.DatabaseConfig) error {
|
2024-08-12 13:53:45 +00:00
|
|
|
|
2024-08-13 08:54:21 +00:00
|
|
|
hook := otellogrus.NewHook(tracingName)
|
|
|
|
logger.AddHook(hook)
|
|
|
|
|
|
|
|
// Debug enabled?
|
2024-08-12 13:53:45 +00:00
|
|
|
if config.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2024-08-12 09:50:45 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:50:45 +00:00
|
|
|
"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),
|
|
|
|
)
|
2024-08-09 19:39:22 +00:00
|
|
|
|
2024-08-12 09:50:45 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting database connection")
|
|
|
|
|
|
|
|
var localSSL string
|
2024-08-09 19:39:22 +00:00
|
|
|
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)
|
2024-08-13 12:00:36 +00:00
|
|
|
sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
|
|
TranslateError: true,
|
|
|
|
})
|
2024-08-09 19:39:22 +00:00
|
|
|
if err != nil {
|
2024-08-12 09:50:45 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, err)
|
2024-08-09 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 09:55:47 +00:00
|
|
|
if config.Migrate {
|
|
|
|
err = migrateDatabase(ctx, sqlDB, config)
|
|
|
|
if err != nil {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, err)
|
|
|
|
}
|
2024-08-09 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 20:17:00 +00:00
|
|
|
client = sqlDB
|
2024-08-12 09:50:45 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Database connected successfully")
|
2024-08-09 19:39:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-13 08:54:21 +00:00
|
|
|
// migrateDatabase handels the migration of ann SQL files in the migrations subfolder
|
2024-08-12 09:50:45 +00:00
|
|
|
func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error {
|
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:50:45 +00:00
|
|
|
"database": config.Database,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("database", config.Database),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting database migration")
|
|
|
|
|
2024-08-09 19:39:22 +00:00
|
|
|
dialect := "postgres"
|
|
|
|
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
|
|
|
|
|
|
|
db, err := dbPool.DB()
|
|
|
|
if err != nil {
|
2024-08-12 09:50:45 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, err)
|
2024-08-09 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
|
|
|
|
if err != nil {
|
2024-08-12 09:50:45 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, err)
|
2024-08-09 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 10:54:02 +00:00
|
|
|
if n != 0 {
|
|
|
|
localLogger.Debugf("applied %d migrations!", n)
|
|
|
|
} else {
|
|
|
|
localLogger.Debug("nothing to migrate")
|
2024-08-09 19:39:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:50:45 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Database migration completed successfully")
|
2024-08-09 19:39:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-08-10 22:26:20 +00:00
|
|
|
|
2024-08-13 08:54:21 +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) {
|
2024-08-12 09:50:45 +00:00
|
|
|
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 {
|
2024-08-12 09:50:45 +00:00
|
|
|
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:26:20 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:50:45 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "GORM client retrieved successfully")
|
2024-08-10 22:26:20 +00:00
|
|
|
return client, nil
|
|
|
|
}
|