Skip to content

Commit

Permalink
Add migration debugging statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed May 30, 2024
1 parent 76dd2c0 commit 053c54c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
76 changes: 58 additions & 18 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"strings"

"github.com/RedHatInsights/chrome-service-backend/rest/database"
Expand All @@ -9,6 +10,8 @@ import (
"github.com/sirupsen/logrus"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)

func getValidGridItems(items []models.GridItem) []models.GridItem {
Expand Down Expand Up @@ -36,6 +39,7 @@ func migrateDashboardWidgets(tx *gorm.DB) *gorm.DB {
return res
}

logrus.Infoln("transform grid items into gorm json type")
for _, t := range templates {
t.TemplateConfig.Xl = datatypes.NewJSONType(getValidGridItems(t.TemplateConfig.Xl.Data()))
t.TemplateConfig.Lg = datatypes.NewJSONType(getValidGridItems(t.TemplateConfig.Lg.Data()))
Expand All @@ -50,9 +54,12 @@ func migrateDashboardWidgets(tx *gorm.DB) *gorm.DB {

templates = []models.DashboardTemplate{}

logrus.Infoln("Removing all non default templates")
fmt.Println("Removing all non default templates")
// remove all non default templates
res = tx.Unscoped().Debug().Where(`"default" is false`).Limit(50).Delete(&[]models.DashboardTemplate{})
res = tx.Unscoped().Debug().Clauses(clause.Locking{
Strength: "SHARE",
Options: "NOWAIT",
}).Where(`"default" = ?`, false).Delete(&[]models.DashboardTemplate{})
if res.Error != nil {
return res
}
Expand All @@ -67,107 +74,140 @@ func main() {
var bundleRes *gorm.DB
var visitedRes *gorm.DB
var dashboardRes *gorm.DB
tx := database.DB.Begin()
tx := database.DB.Begin().Session(&gorm.Session{
Logger: logger.Default.LogMode(logger.Info),
})
defer func() {
if r := recover(); r != nil {
fmt.Println("Unable to migrate database!")
fmt.Println(r)
tx.Rollback()
}
}()

if err := tx.Error; err != nil {
logrus.Error("Unable to migrate database!", err.Error())
fmt.Println("Unable to migrate database!", err.Error())
tx.Rollback()
panic(err)
}

fmt.Println("Migrating last visited pages to user identity table")
// fk_user_identities_last_visited_pages
if tx.Migrator().HasConstraint(&models.UserIdentity{}, "fk_user_identities_last_visited_pages") {
if err := tx.Migrator().DropConstraint(&models.UserIdentity{}, "fk_user_identities_last_visited_pages"); err != nil {
logrus.Error("Unable to migrate database!", err.Error())
fmt.Println("Unable to migrate database!", err.Error())
tx.Rollback()
panic(err)
}
}

fmt.Println("Removing unfavorited pages")
//removes unfavorited pages from all users in favorite pages tables
if tx.Migrator().HasTable(&models.FavoritePage{}) {
if err := tx.Where("favorite = ?", false).Unscoped().Delete(&models.FavoritePage{}); err != nil {
logrus.Error("Unable to migrate database!", err.Error.Error())
err := tx.Where(`"favorite" = ?`, false).Unscoped().Delete(&models.FavoritePage{}).Error
if err != nil {
fmt.Println("Unable to migrate database!", err)
tx.Rollback()
panic(err)
}
}

fmt.Println("Removing dashboard templates sx variant")
// temporary - removes unused typo column in dashboard template tables
if tx.Migrator().HasColumn(&models.DashboardTemplate{}, "sx") {
if err := tx.Migrator().DropColumn(&models.DashboardTemplate{}, "sx"); err != nil {
logrus.Error("Unable to migrate database!", err.Error())
fmt.Println("Unable to migrate database!", err.Error())
tx.Rollback()
panic(err)
}
}

fmt.Println("Remove duplicate identities")
//Deletes Duplicate users from users table
if tx.Migrator().HasTable(&models.UserIdentity{}) {

var duplicates []models.UserIdentity
tx.Model(&models.UserIdentity{}).Select("AccountId").Group("AccountId").Having("COUNT(*) > 1").Find(&duplicates)
err := tx.Model(&models.UserIdentity{}).Select("account_id").Group("account_id").Having("COUNT(*) > 1").Find(&duplicates)
if err.Error != nil {
fmt.Println("Unable to migrate database!", err.Error.Error())
tx.Rollback()
panic(err.Error)
}

for _, dup := range duplicates {

var usersToDelete []models.UserIdentity
tx.Where("AccountId = ?", dup.AccountId).Order("UpdatedAt DESC").Find(&usersToDelete)
tx.Where("account_id = ?", dup.AccountId).Order("updated_at DESC").Find(&usersToDelete)
for i, user := range usersToDelete {
if i > 0 { // Skip the first entry, delete all others
if err := tx.Unscoped().Where(&models.FavoritePage{
UserIdentityID: user.ID,
}).Delete(&models.FavoritePage{}).Error; err != nil {
tx.Rollback()
fmt.Println("Unable to delete user favorite pages associations!", err.Error())
panic(err)
}

if err := tx.Unscoped().Where(&models.DashboardTemplate{
UserIdentityID: user.ID,
}).Delete(&models.DashboardTemplate{}).Error; err != nil {
tx.Rollback()
fmt.Println("Unable to delete user dashboard template associations!", err.Error())
panic(err)
}
if err := tx.Unscoped().Delete(&user).Error; err != nil {
tx.Rollback()
logrus.Error("Unable to delete duplicate users!", err.Error())
fmt.Println("Unable to delete duplicate users!", err.Error())
panic(err)
}
}
}
}
}

fmt.Println("Auto migrate relations")
if err := tx.AutoMigrate(&models.FavoritePage{}, &models.UserIdentity{}, &models.SelfReport{}, &models.ProductOfInterest{}, &models.DashboardTemplate{}); err != nil {
logrus.Error("Unable to migrate database!", err)
fmt.Println("Unable to migrate database!", err)
tx.Rollback()
panic(err)
}

fmt.Println("Remove last visited pages table")
// Drop old tables
if tx.Migrator().HasTable("last_visited_pages") {
if err := tx.Migrator().DropTable("last_visited_pages"); err != nil {
logrus.Error("Unable to migrate database!", err.Error())
fmt.Println("Unable to migrate database!", err.Error())
tx.Rollback()
panic(err)
}
}

fmt.Println("Seed default value to visited bundles")
bundleRes = tx.Model(&models.UserIdentity{}).Where("visited_bundles IS NULL").Update("visited_bundles", []byte(`{}`))
if bundleRes.Error != nil {
logrus.Error("Unable to migrate database!", bundleRes.Error.Error())
fmt.Println("Unable to migrate database!", bundleRes.Error.Error())
tx.Rollback()
panic(bundleRes.Error)
}

fmt.Println("Seed default value to last visited pages")
visitedRes = tx.Model(&models.UserIdentity{}).Where("last_visited_pages IS NULL").Update("last_visited_pages", []byte(`[]`))
if bundleRes.Error != nil {
logrus.Error("Unable to migrate database!", bundleRes.Error.Error())
fmt.Println("Unable to migrate database!", bundleRes.Error.Error())
tx.Rollback()
panic(bundleRes.Error)
}

dashboardRes = migrateDashboardWidgets(tx)
if dashboardRes.Error != nil {
logrus.Error("Unable to migrate database!", dashboardRes.Error.Error())
fmt.Println("Unable to migrate database!", dashboardRes.Error.Error())
tx.Rollback()
panic(dashboardRes.Error)
}

err := tx.Commit().Error

if err != nil {
logrus.Error("Unable to migrate database!", err.Error())
fmt.Println("Unable to migrate database!", err.Error())
tx.Rollback()
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions rest/service/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/sirupsen/logrus"

"gorm.io/datatypes"
"gorm.io/gorm/clause"
)

type IntercomApp string
Expand Down Expand Up @@ -131,7 +130,7 @@ func CreateIdentity(userId string, skipCache bool) (models.UserIdentity, error)
return cachedIdentity, nil
}

res := database.DB.Clauses(clause.Locking{Strength: "UPDATE"}).Where("account_id = ?", userId).FirstOrCreate(&identity)
res := database.DB.Where("account_id = ?", userId).FirstOrCreate(&identity)
err = res.Error

// set the cache after successful DB operation
Expand Down

0 comments on commit 053c54c

Please sign in to comment.