2024-08-09 19:39:22 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
2024-08-10 22:26:20 +00:00
|
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
|
2024-08-09 19:39:22 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
|
|
|
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"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-08-09 19:39:22 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-08-11 20:17:00 +00:00
|
|
|
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.Logger
|
|
|
|
)
|
2024-08-09 19:39:22 +00:00
|
|
|
|
|
|
|
func Connect(_ context.Context, config models.DatabaseConfig) error {
|
|
|
|
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 err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = migrateDatabase(sqlDB, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-11 20:17:00 +00:00
|
|
|
setupTelemetry()
|
|
|
|
|
|
|
|
client = sqlDB
|
2024-08-09 19:39:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateDatabase(dbPool *gorm.DB, config models.DatabaseConfig) error {
|
|
|
|
dialect := "postgres"
|
|
|
|
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
|
|
|
|
|
|
|
db, err := dbPool.DB()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("postgres migration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("postgres migration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
if n != 0 {
|
|
|
|
log.Infof("postgres migration: applied %d migrations!", n)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log.Info("postgres migration: nothing to migrate")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-10 22:26:20 +00:00
|
|
|
|
2024-08-11 20:17:00 +00:00
|
|
|
func setupTelemetry() {
|
|
|
|
tracer = otel.Tracer(tracingName)
|
|
|
|
|
|
|
|
hook := otellogrus.NewHook(tracingName)
|
|
|
|
logger.AddHook(hook)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-10 22:26:20 +00:00
|
|
|
func GetGorm(ctx context.Context) (*gorm.DB, error) {
|
|
|
|
if client == nil {
|
|
|
|
return nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|