Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only run database startup when running server #1012

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func init() {
func serve() {
logger := utils.NewLogger("cloud-service-broker")
logger.Info("starting", lager.Data{"version": utils.Version})
db := dbservice.New(logger)
db := dbservice.NewWithMigrations(logger)
encryptor := setupDBEncryption(db, logger)

// init broker
Expand Down
11 changes: 10 additions & 1 deletion dbservice/dbservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ import (

var once sync.Once

// New instantiates the db connection and runs migrations
// New instantiates the db connection without running migrations
func New(logger lager.Logger) *gorm.DB {
var db *gorm.DB
once.Do(func() {
db = SetupDB(logger)
})
return db
}

// NewWithMigrations instantiates the db connection and runs migrations
func NewWithMigrations(logger lager.Logger) *gorm.DB {
var db *gorm.DB
once.Do(func() {
db = SetupDB(logger)
Expand Down
2 changes: 1 addition & 1 deletion dbservice/dbservice_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dbservice_test
package dbservice

import (
"testing"
Expand Down
29 changes: 29 additions & 0 deletions integrationtest/tf_dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package integrationtest_test

import (
"fmt"
"os"
"os/exec"
"time"

. "github.com/onsi/gomega/gbytes"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)

var _ = Describe("TF Dump", func() {
It("does not run database migration", func() {
cmd := exec.Command(csb, "tf", "dump", "tf:fake-id:")
cmd.Env = append(
os.Environ(),
"DB_TYPE=sqlite3",
fmt.Sprintf("DB_PATH=%s", database),
)
session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).WithTimeout(time.Minute).Should(Exit(2))
Expect(session.Err).To(Say("panic: no such table: password_metadata"))
})
})
Loading