Skip to content

Commit

Permalink
vtorc: cleanup init db handling (#17198)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
Co-authored-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
timvaillancourt and GuptaManan100 authored Nov 26, 2024
1 parent 1f23496 commit 8648264
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions go/vt/vtorc/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func OpenVTOrc() (db *sql.DB, err error) {
db, fromCache, err = sqlutils.GetSQLiteDB(config.Config.SQLite3DataFile)
if err == nil && !fromCache {
log.Infof("Connected to vtorc backend: sqlite on %v", config.Config.SQLite3DataFile)
_ = initVTOrcDB(db)
if err := initVTOrcDB(db); err != nil {
log.Fatalf("Cannot initiate vtorc: %+v", err)
}
}
if db != nil {
db.SetMaxOpenConns(1)
Expand Down Expand Up @@ -76,27 +78,24 @@ func registerVTOrcDeployment(db *sql.DB) error {
func deployStatements(db *sql.DB, queries []string) error {
tx, err := db.Begin()
if err != nil {
log.Fatal(err.Error())
return err
}
for _, query := range queries {
if _, err := tx.Exec(query); err != nil {
log.Fatalf("Cannot initiate vtorc: %+v; query=%+v", err, query)
return err
}
}
if err := tx.Commit(); err != nil {
log.Fatal(err.Error())
}
return nil
return tx.Commit()
}

// ClearVTOrcDatabase is used to clear the VTOrc database. This function is meant to be used by tests to clear the
// database to get a clean slate without starting a new one.
func ClearVTOrcDatabase() {
db, _, _ := sqlutils.GetSQLiteDB(config.Config.SQLite3DataFile)
if db != nil {
_ = initVTOrcDB(db)
if err := initVTOrcDB(db); err != nil {
log.Fatalf("Cannot re-initiate vtorc: %+v", err)
}
}
}

Expand All @@ -105,12 +104,18 @@ func ClearVTOrcDatabase() {
func initVTOrcDB(db *sql.DB) error {
log.Info("Initializing vtorc")
log.Info("Migrating database schema")
_ = deployStatements(db, vtorcBackend)
_ = registerVTOrcDeployment(db)

_, _ = ExecVTOrc(`PRAGMA journal_mode = WAL`)
_, _ = ExecVTOrc(`PRAGMA synchronous = NORMAL`)

if err := deployStatements(db, vtorcBackend); err != nil {
return err
}
if err := registerVTOrcDeployment(db); err != nil {
return err
}
if _, err := ExecVTOrc(`PRAGMA journal_mode = WAL`); err != nil {
return err
}
if _, err := ExecVTOrc(`PRAGMA synchronous = NORMAL`); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 8648264

Please sign in to comment.