From be2a7736b9bee996ac801625c2daca4309f56b5e Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Thu, 9 May 2024 14:30:56 +0300 Subject: [PATCH] run postgres cases in new db --- management/server/sql_store.go | 18 ++++++++++++++++++ management/server/sql_store_test.go | 9 --------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/management/server/sql_store.go b/management/server/sql_store.go index f3c9cb0dec8..21cdba7c0db 100644 --- a/management/server/sql_store.go +++ b/management/server/sql_store.go @@ -3,6 +3,7 @@ package server import ( "errors" "fmt" + "math/rand" "path/filepath" "runtime" "strings" @@ -610,6 +611,14 @@ func NewSqliteStoreFromFileStore(fileStore *FileStore, dataDir string, metrics t // NewPostgresqlStoreFromFileStore restores a store from FileStore and stores Postgres DB. func NewPostgresqlStoreFromFileStore(fileStore *FileStore, dsn string, metrics telemetry.AppMetrics) (*SqlStore, error) { + dbName := "store_" + randString(10) + db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + result := db.Exec(fmt.Sprintf("CREATE DATABASE %s ENCODING = 'UTF8'", dbName)) + if result.Error != nil { + return nil, result.Error + } + + dsn = fmt.Sprintf("%s dbname=%s ", dsn, dbName) store, err := NewPostgresqlStore(dsn, metrics) if err != nil { return nil, err @@ -629,3 +638,12 @@ func NewPostgresqlStoreFromFileStore(fileStore *FileStore, dsn string, metrics t return store, nil } + +func randString(n int) string { + var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890") + b := make([]rune, n) + for i := range b { + b[i] = letterRunes[rand.Intn(len(letterRunes))] + } + return string(b) +} diff --git a/management/server/sql_store_test.go b/management/server/sql_store_test.go index 1a2118ba045..70dbe756bb7 100644 --- a/management/server/sql_store_test.go +++ b/management/server/sql_store_test.go @@ -922,12 +922,3 @@ func TestPostgresql_GetUserByTokenID(t *testing.T) { require.NoError(t, err) require.Equal(t, id, user.PATs[id].ID) } - -func randString(n int) string { - var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890") - b := make([]rune, n) - for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] - } - return string(b) -}