Skip to content

Commit

Permalink
add DB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes94 committed Jul 3, 2024
1 parent 72d13b9 commit 9098362
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/dinosaur/pkg/api/dbapi/central_request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type CentralRequest struct {
Secrets api.JSON `json:"secrets"`
// SecretDataSum is the b64 encoded hash of plain text data of the stored secrets.
// It used used for equality checks of secrets in the dataplane cluster with the secrets stored in DB
SecretDataSum string `json:"secrets_data_sum"`
SecretDataSum string `json:"secret_data_sum"`

Namespace string `json:"namespace"`
RoutesCreationID string `json:"routes_creation_id"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package migrations

// Migrations should NEVER use types from other packages. Types can change
// and then migrations run on a _new_ database will fail or behave unexpectedly.
// Instead of importing types, always re-create the type in the migration, as
// is done here, even though the same type is defined in pkg/api

import (
"github.com/go-gormigrate/gormigrate/v2"
"github.com/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/db"
"gorm.io/gorm"
)

func addSecretDataSum() *gormigrate.Migration {
type CentralRequest struct {
db.Model
SecretDataSum string `json:"secret_data_sum"`
}
migrationID := "20240703120000"

return &gormigrate.Migration{
ID: migrationID,
Migrate: func(tx *gorm.DB) error {
return addColumnIfNotExists(tx, &CentralRequest{}, "secret_data_sum")
},
Rollback: func(tx *gorm.DB) error {
return errors.Wrap(
tx.Migrator().DropColumn(&CentralRequest{}, "secret_data_sum"),
"failed to drop secret_data_sum column",
)
},
}
}
2 changes: 1 addition & 1 deletion internal/dinosaur/pkg/services/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (k *dinosaurService) ResetCentralSecretBackup(ctx context.Context, centralR
logStateChange("reset secrets", centralRequest.ID, nil)

dbConn := k.connectionFactory.New()
if err := dbConn.Model(centralRequest).Select("secrets", "secrets_data_hash").Updates(centralRequest).Error; err != nil {
if err := dbConn.Model(centralRequest).Select("secrets", "secret_data_sum").Updates(centralRequest).Error; err != nil {
return errors.NewWithCause(errors.ErrorGeneral, err, "Unable to reset secrets for central request")
}

Expand Down

0 comments on commit 9098362

Please sign in to comment.