Skip to content

Commit

Permalink
vtorc: cleanup init db handling
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Nov 6, 2024
1 parent 77da76b commit 0ba72f2
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 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 All @@ -65,38 +67,33 @@ func registerVTOrcDeployment(db *sql.DB) error {
?, datetime('now')
)
`
if _, err := execInternal(db, query, ""); err != nil {
log.Fatalf("Unable to write to vtorc_db_deployments: %+v", err)
}
return nil
_, err := execInternal(db, query, "")
return err
}

// deployStatements will issue given sql queries that are not already known to be deployed.
// This iterates both lists (to-run and already-deployed) and also verifies no contradictions.
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 +102,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 0ba72f2

Please sign in to comment.