Skip to content

Commit

Permalink
fix: only run database startup when running server (#1012)
Browse files Browse the repository at this point in the history
An error was discovered where database startup, including database
migrations and failed operation recovery was being run for every command
that accessed the database, including "tf dump". This had the effect of
marking any in-progress operations as failed, even if they were still
really in progress. This has been a problem since the introduction of
in-progress operation recovery in v1.1.0 (commit
c845267).

[#187500102](https://www.pivotaltracker.com/story/show/187500102)
  • Loading branch information
blgm authored Apr 26, 2024
1 parent e61bd36 commit 5df368e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
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"))
})
})

0 comments on commit 5df368e

Please sign in to comment.