Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: use the right database for metadata database for embedded case (#5444)

Danny Xu4ba6736a7bb5
diff --git a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
index 980ae6c7bd..1337f18a3c 100644
--- a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
+++ b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
@@ -148,7 +148,7 @@ func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFa
startedNs := time.Now().UnixNano()
defer func() {
- if err := utils.EndMigration(ctx, stores, startedNs, insertedID, updatedSchema, db.BytebaseDatabase, resErr == nil /* isDone */); err != nil {
+ if err := utils.EndMigration(ctx, stores, startedNs, insertedID, updatedSchema, resErr == nil /* isDone */); err != nil {
log.Error("failed to update migration history record",
zap.Error(err),
zap.String("migration_id", migrationHistoryID),
diff --git a/backend/store/pg_engine.go b/backend/store/pg_engine.go
index db02cb1497..cd91b19dc5 100644
--- a/backend/store/pg_engine.go
+++ b/backend/store/pg_engine.go
@@ -148,7 +148,7 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
ctx,
dbdriver.Postgres,
dbdriver.DriverConfig{DbBinDir: db.binDir},
- db.connCfg,
+ metadataConnConfig,
dbdriver.ConnectionContext{},
)
if err != nil {
@@ -171,6 +171,22 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
db.db = metadataDriver.GetDB()
return ver, nil
}
+ db.db = metadataDriver.GetDB()
+ // Set the max open connections so that we won't exceed the connection limit of metaDB.
+ // The limit is the max connections minus connections reserved for superuser.
+ var maxConns, reservedConns int
+ if err := db.db.QueryRowContext(ctx, `SHOW max_connections`).Scan(&maxConns); err != nil {
+ return nil, errors.Wrap(err, "failed to get max_connections from metaDB")
+ }
+ if err := db.db.QueryRowContext(ctx, `SHOW superuser_reserved_connections`).Scan(&reservedConns); err != nil {
+ return nil, errors.Wrap(err, "failed to get superuser_reserved_connections from metaDB")
+ }
+ maxOpenConns := maxConns - reservedConns
+ if maxOpenConns > 50 {
+ // capped to 50
+ maxOpenConns = 50
+ }
+ db.db.SetMaxOpenConns(maxOpenConns)
// We are also using our own migration core to manage our own schema's migration history.
// So here we will create a "bytebase" database to store the migration history if the target
@@ -194,23 +210,6 @@ func (db *DB) Open(ctx context.Context) (schemaVersion *semver.Version, err erro
}
log.Info(fmt.Sprintf("Current schema version after migration: %s", verAfter))
- db.db = metadataDriver.GetDB()
- // Set the max open connections so that we won't exceed the connection limit of metaDB.
- // The limit is the max connections minus connections reserved for superuser.
- var maxConns, reservedConns int
- if err := db.db.QueryRowContext(ctx, `SHOW max_connections`).Scan(&maxConns); err != nil {
- return nil, errors.Wrap(err, "failed to get max_connections from metaDB")
- }
- if err := db.db.QueryRowContext(ctx, `SHOW superuser_reserved_connections`).Scan(&reservedConns); err != nil {
- return nil, errors.Wrap(err, "failed to get superuser_reserved_connections from metaDB")
- }
- maxOpenConns := maxConns - reservedConns
- if maxOpenConns > 50 {
- // capped to 50
- maxOpenConns = 50
- }
- db.db.SetMaxOpenConns(maxOpenConns)
-
if err := db.setupDemoData(); err != nil {
return nil, errors.Wrapf(err, "failed to setup demo data."+
" It could be Bytebase is running against an old Bytebase schema. If you are developing Bytebase, you can remove pgdata"+
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index 1e4fb1b640..31f958cf61 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -496,7 +496,7 @@ func ExecuteMigration(ctx context.Context, store *store.Store, driver db.Driver,
startedNs := time.Now().UnixNano()
defer func() {
- if err := EndMigration(ctx, store, startedNs, insertedID, updatedSchema, db.BytebaseDatabase, resErr == nil /* isDone */); err != nil {
+ if err := EndMigration(ctx, store, startedNs, insertedID, updatedSchema, resErr == nil /* isDone */); err != nil {
log.Error("Failed to update migration history record",
zap.Error(err),
zap.String("migration_id", migrationHistoryID),
@@ -613,7 +613,7 @@ func BeginMigration(ctx context.Context, store *store.Store, m *db.MigrationInfo
}
// EndMigration updates the migration history record to DONE or FAILED depending on migration is done or not.
-func EndMigration(ctx context.Context, store *store.Store, startedNs int64, insertedID string, updatedSchema string, _ string, isDone bool) error {
+func EndMigration(ctx context.Context, store *store.Store, startedNs int64, insertedID string, updatedSchema string, isDone bool) error {
var err error
migrationDurationNs := time.Now().UnixNano() - startedNs