diff --git a/cmd/serve.go b/cmd/serve.go index 49c3598fb..2afad3365 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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 diff --git a/dbservice/dbservice.go b/dbservice/dbservice.go index 9ec304b0c..10b1a8faa 100644 --- a/dbservice/dbservice.go +++ b/dbservice/dbservice.go @@ -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) diff --git a/dbservice/dbservice_suite_test.go b/dbservice/dbservice_suite_test.go index 1003b3d84..33d36de49 100644 --- a/dbservice/dbservice_suite_test.go +++ b/dbservice/dbservice_suite_test.go @@ -1,4 +1,4 @@ -package dbservice_test +package dbservice import ( "testing" diff --git a/integrationtest/tf_dump_test.go b/integrationtest/tf_dump_test.go new file mode 100644 index 000000000..b580af1d2 --- /dev/null +++ b/integrationtest/tf_dump_test.go @@ -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")) + }) +})