populate.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package stable_source_config_id_population
  2. import (
  3. "github.com/porter-dev/porter/internal/models"
  4. lr "github.com/porter-dev/porter/pkg/logger"
  5. _gorm "gorm.io/gorm"
  6. )
  7. func PopulateStableSourceConfigId(db *_gorm.DB, logger *lr.Logger) {
  8. logger.Info().Msg("Start populating stable source config id")
  9. var dest []*models.StackRevision
  10. db.Model(&models.StackRevision{}).Preload("SourceConfigs").Find(&dest)
  11. // Create a map that will separate source configs based on stack and name
  12. // this will allow us to check all the source configs revisions that correspond
  13. // to the same source config object.
  14. sourceConfigsPerStack := make(map[uint]map[string][]models.StackSourceConfig)
  15. for _, revision := range dest {
  16. if sourceConfigsPerStack[revision.StackID] == nil {
  17. sourceConfigsPerStack[revision.StackID] = make(map[string][]models.StackSourceConfig)
  18. }
  19. for _, sc := range revision.SourceConfigs {
  20. sourceConfigsPerStack[revision.StackID][sc.Name] = append(sourceConfigsPerStack[revision.StackID][sc.Name], sc)
  21. }
  22. }
  23. populatedSourceConfigs := []models.StackSourceConfig{}
  24. // Populate the stable source config id for each revision of the source config
  25. for _, sourceConfigsWithSameNameMap := range sourceConfigsPerStack {
  26. for _, sc := range sourceConfigsWithSameNameMap {
  27. // If all source configs have the same stable source config id, then we can skip this step
  28. if allSourceConfigsHaveSameStableSourceConfigID(sc) {
  29. logger.Info().Msgf("Skipping source configs with stable source config id %s", sc[0].StableSourceConfigID)
  30. continue
  31. }
  32. sortedSourceConfigs := sortSourceConfigsByCreationDate(sc)
  33. stableSourceConfigId := findSourceConfigWithStableSourceConfigID(sortedSourceConfigs)
  34. if stableSourceConfigId == "" {
  35. stableSourceConfigId = sortedSourceConfigs[0].UID
  36. }
  37. for _, sourceConfig := range sortedSourceConfigs {
  38. if sourceConfig.StableSourceConfigID != "" && sourceConfig.StableSourceConfigID != stableSourceConfigId {
  39. logger.Warn().Msgf("source config %s has a different stable source config id %s than %s", sourceConfig.UID, sourceConfig.StableSourceConfigID, stableSourceConfigId)
  40. logger.Warn().Msg("This may cause issues with the stack, continuing anyways.")
  41. }
  42. logger.Info().Msg("Populating stable source config id for source config " + sourceConfig.UID)
  43. sourceConfig.StableSourceConfigID = stableSourceConfigId
  44. populatedSourceConfigs = append(populatedSourceConfigs, sourceConfig)
  45. }
  46. }
  47. }
  48. for _, sc := range populatedSourceConfigs {
  49. if err := db.Save(sc).Error; err != nil {
  50. logger.Error().Err(err).Msgf("Failed to save source config with UID %s", sc.UID)
  51. }
  52. }
  53. }
  54. func findSourceConfigWithStableSourceConfigID(sourceConfigs []models.StackSourceConfig) string {
  55. for _, sc := range sourceConfigs {
  56. if sc.StableSourceConfigID != "" {
  57. return sc.StableSourceConfigID
  58. }
  59. }
  60. return ""
  61. }
  62. // sort source configs by creation date
  63. func sortSourceConfigsByCreationDate(sourceConfigs []models.StackSourceConfig) []models.StackSourceConfig {
  64. for i := 0; i < len(sourceConfigs); i++ {
  65. for j := i + 1; j < len(sourceConfigs); j++ {
  66. if sourceConfigs[i].CreatedAt.After(sourceConfigs[j].CreatedAt) {
  67. sourceConfigs[i], sourceConfigs[j] = sourceConfigs[j], sourceConfigs[i]
  68. }
  69. }
  70. }
  71. return sourceConfigs
  72. }
  73. // Check if all source configs in the array have populated the same StableSourceConfigID
  74. func allSourceConfigsHaveSameStableSourceConfigID(sourceConfigs []models.StackSourceConfig) bool {
  75. if len(sourceConfigs) == 0 {
  76. return true
  77. }
  78. stableSourceConfigID := sourceConfigs[0].StableSourceConfigID
  79. if stableSourceConfigID == "" {
  80. return false
  81. }
  82. for _, sc := range sourceConfigs {
  83. if sc.StableSourceConfigID != stableSourceConfigID {
  84. return false
  85. }
  86. }
  87. return true
  88. }