Skip to content

Commit

Permalink
refactor: cleanup strictUseDb (bytebase#9097)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianzhou authored Nov 1, 2023
1 parent 7f56d1e commit 59e70b9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions backend/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var demoFS embed.FS

// LoadDemoDataIfNeeded loads the demo data if specified.
func LoadDemoDataIfNeeded(ctx context.Context, storeDB *store.DB, strictUseDb bool, pgBinDir, demoName string) error {
func LoadDemoDataIfNeeded(ctx context.Context, storeDB *store.DB, pgBinDir, demoName string) error {
if demoName == "" {
slog.Debug("Skip setting up demo data. Demo not specified.")
return nil
Expand All @@ -30,12 +30,12 @@ func LoadDemoDataIfNeeded(ctx context.Context, storeDB *store.DB, strictUseDb bo
slog.Info(fmt.Sprintf("Setting up demo %q...", demoName))

databaseName := storeDB.ConnCfg.Database
if !strictUseDb {
if !storeDB.ConnCfg.StrictUseDb {
// The database storing metadata is the same as user name.
databaseName = storeDB.ConnCfg.Username
}
metadataConnConfig := storeDB.ConnCfg
if !strictUseDb {
if !storeDB.ConnCfg.StrictUseDb {
metadataConnConfig.Database = databaseName
}
metadataDriver, err := dbdriver.Open(
Expand Down
6 changes: 3 additions & 3 deletions backend/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
var migrationFS embed.FS

// MigrateSchema migrates the schema for metadata database.
func MigrateSchema(ctx context.Context, storeDB *store.DB, strictUseDb bool, pgBinDir, serverVersion string, mode common.ReleaseMode) (*semver.Version, error) {
func MigrateSchema(ctx context.Context, storeDB *store.DB, pgBinDir, serverVersion string, mode common.ReleaseMode) (*semver.Version, error) {
databaseName := storeDB.ConnCfg.Database
if !strictUseDb {
if !storeDB.ConnCfg.StrictUseDb {
// The database storing metadata is the same as user name.
databaseName = storeDB.ConnCfg.Username
}
metadataConnConfig := storeDB.ConnCfg
if !strictUseDb {
if !storeDB.ConnCfg.StrictUseDb {
metadataConnConfig.Database = databaseName
}
metadataDriver, err := dbdriver.Open(
Expand Down
1 change: 1 addition & 0 deletions backend/plugin/db/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ type ConnectionConfig struct {
// ReadOnly is only supported for Postgres at the moment.
ReadOnly bool
// StrictUseDb will only set as true if the user gives only a database instead of a whole instance to access.
// This is only used when connecting PG metadata database.
StrictUseDb bool
// SRV is only supported for MongoDB now.
SRV bool
Expand Down
4 changes: 2 additions & 2 deletions backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
if profile.Readonly {
slog.Info("Database is opened in readonly mode. Skip migration and demo data setup.")
} else {
if err := demo.LoadDemoDataIfNeeded(ctx, storeDB, !profile.UseEmbedDB(), s.pgBinDir, profile.DemoName); err != nil {
if err := demo.LoadDemoDataIfNeeded(ctx, storeDB, s.pgBinDir, profile.DemoName); err != nil {
return nil, errors.Wrapf(err, "failed to load demo data")
}
if _, err := migrator.MigrateSchema(ctx, storeDB, !profile.UseEmbedDB(), s.pgBinDir, profile.Version, profile.Mode); err != nil {
if _, err := migrator.MigrateSchema(ctx, storeDB, s.pgBinDir, profile.Version, profile.Mode); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/store/pg_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
)

// DB represents the database connection.
// DB represents the metdata database connection.
type DB struct {
metadataDriver dbdriver.Driver
db *sql.DB
Expand Down

0 comments on commit 59e70b9

Please sign in to comment.