Compare commits
5 Commits
2bbfc79e43
...
00b8fe6b71
Author | SHA1 | Date | |
---|---|---|---|
00b8fe6b71 | |||
5e9b4ff532 | |||
450e06eb25 | |||
de28bdbb0b | |||
b7e3877b68 |
@ -16,14 +16,6 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource")
|
||||
defer span.End()
|
||||
|
||||
localLogger = localLogger.WithFields(log.Fields{
|
||||
"source_id": source.ID,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("source_id", string(source.ID)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Starting source creation")
|
||||
|
||||
if client == nil {
|
||||
@ -38,6 +30,14 @@ func CreateSource(ctx context.Context, source models.Source) (models.Source, err
|
||||
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
||||
}
|
||||
|
||||
localLogger = localLogger.WithFields(log.Fields{
|
||||
"source_id": source.ID,
|
||||
})
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("source_id", string(source.ID)),
|
||||
)
|
||||
|
||||
utils.HandleEvent(span, localLogger, "Source created successfully")
|
||||
return source, nil
|
||||
}
|
||||
@ -82,6 +82,11 @@ func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSource updates th source information in the database.
|
||||
// Only a few parameter can be updated:
|
||||
// - DisplayName
|
||||
// - Domain
|
||||
// - Icon
|
||||
func UpdateSource(ctx context.Context, source models.Source) error {
|
||||
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource")
|
||||
defer span.End()
|
||||
@ -105,12 +110,15 @@ func UpdateSource(ctx context.Context, source models.Source) error {
|
||||
}
|
||||
|
||||
updateSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: source.ID,
|
||||
},
|
||||
DisplayName: source.DisplayName,
|
||||
Domain: source.Domain,
|
||||
Icon: source.Icon,
|
||||
}
|
||||
|
||||
result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{})
|
||||
result := client.WithContext(ctx).Updates(&updateSource)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
||||
|
546
pkg/database/source_test.go
Normal file
546
pkg/database/source_test.go
Normal file
@ -0,0 +1,546 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
|
||||
"go.opentelemetry.io/contrib/bridges/otellogrus"
|
||||
"go.opentelemetry.io/otel"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestCreateSource(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Source to test with
|
||||
validSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
Icon: "e621.net/icon.png",
|
||||
}
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
source models.Source
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want models.Source
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Valid Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: validSource,
|
||||
},
|
||||
want: validSource,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Duplicate Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: validSource,
|
||||
},
|
||||
want: models.Source{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := CreateSource(tt.args.ctx, tt.args.source)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("CreateSource() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSourceInBatch(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Sources to test with
|
||||
validSources := test.GenerateRandomSources(5)
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
source []models.Source
|
||||
batchSize int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Valid Sources",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: validSources,
|
||||
batchSize: len(validSources),
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Duplicate Sources",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: validSources,
|
||||
batchSize: len(validSources),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 03: Nil Sources",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: nil,
|
||||
batchSize: len(validSources),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 04: Empty Sources",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: []models.Source{},
|
||||
batchSize: len(validSources),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 08: Empty Batch Size",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: []models.Source{},
|
||||
batchSize: 0,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := CreateSourceInBatch(tt.args.ctx, tt.args.source, tt.args.batchSize); (err != nil) != tt.wantErr {
|
||||
t.Errorf("CreateSourceInBatch() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateSource(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Source to test with
|
||||
validSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
Icon: "e621.net/icon.png",
|
||||
}
|
||||
|
||||
validSource, err = CreateSource(ctx, validSource)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSource err: %v", err)
|
||||
}
|
||||
// --
|
||||
|
||||
// -- Create Updates models for UserSource
|
||||
validUpdateSource := validSource
|
||||
validUpdateSource.DisplayName = "eeeee"
|
||||
validUpdateSource.Domain = "aaaaa"
|
||||
validUpdateSource.Icon = "nnnn"
|
||||
|
||||
invalidUpdateSource := models.Source{}
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
source models.Source
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Valid Update for Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: validUpdateSource,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Invalid Update for Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: invalidUpdateSource,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 03: Empty ID for Update for Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
source: models.Source{BaseModel: models.BaseModel[models.SourceID]{ID: ""}},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := UpdateSource(tt.args.ctx, tt.args.source); (err != nil) != tt.wantErr {
|
||||
t.Errorf("UpdateSource() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSourceByID(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Source to test with
|
||||
validSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
Icon: "e621.net/icon.png",
|
||||
}
|
||||
|
||||
validSource, err = CreateSource(ctx, validSource)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSource err: %v", err)
|
||||
}
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
id models.SourceID
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want models.Source
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Valid Source ID",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: validSource.ID,
|
||||
},
|
||||
want: validSource,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 03: Empty SourceID",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 04: Short SourceID",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: "111",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetSourceByID(tt.args.ctx, tt.args.id)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetSourceByID() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !checkSourceID(got, tt.want) {
|
||||
t.Errorf("GetSourceByID() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSourceByDomain(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Source to test with
|
||||
validSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
Icon: "e621.net/icon.png",
|
||||
}
|
||||
|
||||
validSource, err = CreateSource(ctx, validSource)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSource err: %v", err)
|
||||
}
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
sourceDomain models.SourceDomain
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want models.Source
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Valid SourceURL",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
sourceDomain: validSource.Domain,
|
||||
},
|
||||
want: validSource,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Empty SourceURL",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
sourceDomain: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !checkSourceID(got, tt.want) {
|
||||
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSource(t *testing.T) {
|
||||
// Setup trow away container
|
||||
ctx := context.Background()
|
||||
container, gormDB, err := test.StartPostgresContainer(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf("Could not start PostgreSQL container: %v", err)
|
||||
}
|
||||
|
||||
client = gormDB
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
hook := otellogrus.NewHook(tracingName)
|
||||
logger.AddHook(hook)
|
||||
|
||||
defer container.Terminate(ctx)
|
||||
|
||||
// -- -- Setup Tests
|
||||
|
||||
// -- Create Source to test with
|
||||
validSource := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
Icon: "e621.net/icon.png",
|
||||
}
|
||||
|
||||
validSource, err = CreateSource(ctx, validSource)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSource err: %v", err)
|
||||
}
|
||||
// --
|
||||
|
||||
// -- -- Tests
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
id models.SourceID
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Delete Valid Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: validSource.ID,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Delete not existed Source",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: validSource.ID,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Test 03: Empty SourceID",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 04: Short SourceID",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
id: "111",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := DeleteSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DeleteSource() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func checkSourceID(got models.Source, want models.Source) bool {
|
||||
if got.ID != want.ID {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
@ -4,7 +4,7 @@ package models
|
||||
type Source struct {
|
||||
BaseModel[SourceID]
|
||||
DisplayName string `json:"display_name" `
|
||||
Domain string `json:"domain" gorm:"not null;unique"`
|
||||
Domain SourceDomain `json:"domain" gorm:"not null;unique"`
|
||||
Icon string `json:"icon" gorm:"not null"`
|
||||
UserSources []UserSource `json:"-" gorm:"foreignKey:SourceID"`
|
||||
References []PostReference `json:"references" gorm:"foreignKey:SourceID"`
|
||||
|
@ -3,10 +3,12 @@ package test
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
gonanoid "github.com/matoous/go-nanoid/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GenerateRandomTags(numTags int) []models.Tag {
|
||||
@ -67,3 +69,33 @@ func GenerateRandomTagAlias(tags []models.Tag, numGroups int) []models.TagAlias
|
||||
|
||||
return tagAliases
|
||||
}
|
||||
|
||||
func GenerateRandomSources(numTags int) []models.Source {
|
||||
var sources []models.Source
|
||||
|
||||
for i := 0; i < numTags; i++ {
|
||||
id, _ := gonanoid.New(10)
|
||||
displayName, _ := gonanoid.New(10)
|
||||
domain, _ := gonanoid.New(10)
|
||||
icon, _ := gonanoid.New(10)
|
||||
id = spew.Sprintf("source_name_%s", id)
|
||||
|
||||
source := models.Source{
|
||||
BaseModel: models.BaseModel[models.SourceID]{
|
||||
ID: models.SourceID(id),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
},
|
||||
DisplayName: displayName,
|
||||
Domain: models.SourceDomain(domain),
|
||||
Icon: icon,
|
||||
UserSources: nil,
|
||||
References: nil,
|
||||
}
|
||||
|
||||
sources = append(sources, source)
|
||||
}
|
||||
|
||||
return sources
|
||||
}
|
||||
|
@ -3,12 +3,8 @@ package test
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"gorm.io/driver/postgres"
|
||||
@ -84,41 +80,3 @@ func getGormDB(connectionString string) (*gorm.DB, error) {
|
||||
TranslateError: true,
|
||||
})
|
||||
}
|
||||
|
||||
func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (*models.DatabaseConfig, error) {
|
||||
var err error
|
||||
|
||||
connectionString, err := pgContainer.ConnectionString(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
connectionStringUrl, err := url.Parse(connectionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
split := strings.Split(connectionStringUrl.Host, ":")
|
||||
host := split[0]
|
||||
|
||||
port, err := strconv.Atoi(split[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
database := strings.TrimPrefix(connectionStringUrl.Path, "/")
|
||||
|
||||
username := connectionStringUrl.User.Username()
|
||||
password, _ := connectionStringUrl.User.Password()
|
||||
|
||||
return &models.DatabaseConfig{
|
||||
Endpoint: host,
|
||||
Username: username,
|
||||
Password: password,
|
||||
Database: database,
|
||||
Port: port,
|
||||
SSL: false,
|
||||
Timezone: "Europe/Berlin",
|
||||
Debug: true,
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user