Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MGMT-15114: remove event for cluster registration failed
Signed-off-by: Riccardo piccoli <rpiccoli@redhat.com>
- Loading branch information
Showing
8 changed files
with
81 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
internal/migrations/20230629113604_delete_events_with_unbound_cluster.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package migrations | ||
|
||
import ( | ||
gormigrate "github.com/go-gormigrate/gormigrate/v2" | ||
"github.com/openshift/assisted-service/models" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func deleteEventsWithUnboundCluster() *gormigrate.Migration { | ||
migrate := func(tx *gorm.DB) error { | ||
db := tx.Unscoped() | ||
return db.Where("cluster_id NOT IN (SELECT id FROM clusters)").Delete(&models.Event{}).Error | ||
} | ||
rollback := func(tx *gorm.DB) error { | ||
// Can't really rollback a delete | ||
return nil | ||
} | ||
|
||
return &gormigrate.Migration{ | ||
ID: "20230629113604", | ||
Migrate: gormigrate.MigrateFunc(migrate), | ||
Rollback: gormigrate.RollbackFunc(rollback), | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
internal/migrations/20230629113604_delete_events_with_unbound_cluster_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/go-openapi/strfmt" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/openshift/assisted-service/internal/common" | ||
"github.com/openshift/assisted-service/models" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var _ = Describe("deleteEventsWithUnboundCluster", func() { | ||
var ( | ||
db *gorm.DB | ||
dbName string | ||
clusterID = strfmt.UUID("46a8d745-dfce-4fd8-9df0-549ee8eabb3d") | ||
nonClusterID = strfmt.UUID("44444444-dfce-4fd8-9df0-549ee8eabb3d") | ||
) | ||
|
||
BeforeEach(func() { | ||
db, dbName = common.PrepareTestDB() | ||
cluster := common.Cluster{Cluster: models.Cluster{ID: &clusterID, OpenshiftClusterID: strfmt.UUID("134ce3ea-512e-4a11-bc4c-aeac1bdf3820"), UserName: "user1", OrgID: "org1"}} | ||
Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred()) | ||
message := "this is my event" | ||
event := common.Event{Event: models.Event{ClusterID: &clusterID, Message: &message}} | ||
Expect(db.Create(&event).Error).ShouldNot(HaveOccurred()) | ||
event = common.Event{Event: models.Event{ClusterID: &nonClusterID, Message: &message}} | ||
Expect(db.Create(&event).Error).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
common.DeleteTestDB(db, dbName) | ||
}) | ||
|
||
It("Migrates up", func() { | ||
numRawEvents := func(clusterID *strfmt.UUID) int { | ||
var count int64 | ||
db.Model(&models.Event{}).Where("cluster_id = ?", clusterID).Count(&count) | ||
return int(count) | ||
} | ||
|
||
err := migrateToBefore(db, "20230629113604") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(numRawEvents(&clusterID)).To(Equal(1)) | ||
Expect(numRawEvents(&nonClusterID)).To(Equal(1)) | ||
|
||
err = migrateTo(db, "20230629113604") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(numRawEvents(&clusterID)).To(Equal(1)) | ||
Expect(numRawEvents(&nonClusterID)).To(Equal(0)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters