Skip to content

Commit

Permalink
fix: move logic before database creation attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabricio committed Feb 28, 2025
1 parent 5773391 commit 30a2d0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
38 changes: 20 additions & 18 deletions pkg/databaseclaim/databaseclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,19 +565,6 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context, reqInfo *r
}
defer dbClient.Close()

if dbClaim.Status.MigrationState == pgctl.S_Initial.String() {
// Verify if the `ib` schema exists to ensure the database isn't already managed,
// avoiding migration to a potentially populated database.
exists, err := dbClient.SchemaExists(dbclient.IBSchema)
if err != nil {
return r.statusManager.SetError(ctx, dbClaim, fmt.Errorf("failed to check schema existence: %w", err))
}
if exists {
dbClaim.Status.MigrationState = ""
return r.statusManager.SetError(ctx, dbClaim, fmt.Errorf("migration aborted: attempt to migrate to a previously managed database"))
}
}

// Series of partial updates to the status newb, get ready

// Update connection info to object
Expand All @@ -587,7 +574,7 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context, reqInfo *r
r.statusManager.UpdateClusterStatus(&dbClaim.Status.NewDB, &reqInfo.HostParams)

// Updates the database name
if err := r.createDatabaseAndExtensions(ctx, reqInfo, dbClient, &dbClaim.Status.NewDB, operationalMode); err != nil {
if err := r.createDatabaseAndExtensions(ctx, dbClient, dbClaim, operationalMode); err != nil {
logr.Error(err, "unable to create database and extensions")
return r.statusManager.SetError(ctx, dbClaim, err)
}
Expand Down Expand Up @@ -1159,14 +1146,28 @@ func (r *DatabaseClaimReconciler) getParameterGroupName(hostParams *hostparams.H
}
}

func (r *DatabaseClaimReconciler) createDatabaseAndExtensions(ctx context.Context, reqInfo *requestInfo, dbClient dbclient.Creater, status *v1.Status, operationalMode ModeEnum) error {
func (r *DatabaseClaimReconciler) createDatabaseAndExtensions(ctx context.Context, dbClient dbclient.Clienter, dbClaim *v1.DatabaseClaim, operationalMode ModeEnum) error {
logr := log.FromContext(ctx)
dbName := dbClaim.Spec.DatabaseName

dbName := reqInfo.MasterConnInfo.DatabaseName
created, err := dbClient.CreateDatabase(dbName)
if err != nil {
return err
}
// make sure not migrate to a previously managed database
if !created && dbClaim.Status.MigrationState == pgctl.S_Initial.String() {
// Verify if the `ib` schema exists to ensure the database isn't already managed,
// avoiding migration to a potentially populated database.
exists, err := dbClient.SchemaExists(dbclient.IBSchema)
if err != nil {
return fmt.Errorf("failed to check schema existence: %w", err)
}
if exists {
dbClaim.Status.MigrationState = ""
return fmt.Errorf("migration aborted: attempt to migrate to a previously managed database")
}
}

if created && operationalMode == M_UseNewDB {
//the migrations usecase takes care of copying extensions
//only in newDB workflow they need to be created explicitly
Expand All @@ -1177,9 +1178,10 @@ func (r *DatabaseClaimReconciler) createDatabaseAndExtensions(ctx context.Contex
return err
}
}
if created || status.ConnectionInfo.DatabaseName == "" {
r.statusManager.UpdateDBStatus(status, dbName)
if created || dbClaim.Status.NewDB.ConnectionInfo.DatabaseName == "" {
r.statusManager.UpdateDBStatus(&dbClaim.Status.NewDB, dbName)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/dbclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func (pc *client) ManageSystemFunctions(dbName string, functions map[string]stri

func (pc *client) SchemaExists(schemaName string) (bool, error) {
var exists bool
err := pc.adminDB.QueryRow("SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = $1)", schemaName).Scan(&exists)
err := pc.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = $1)", schemaName).Scan(&exists)
if err != nil {
return false, fmt.Errorf("schema_exists %s: %w", schemaName, err)
}
Expand Down

0 comments on commit 30a2d0c

Please sign in to comment.