Răsfoiți Sursa

Added logs for populate script

jnfrati 3 ani în urmă
părinte
comite
5faf7c993a

+ 4 - 4
cmd/migrate/main.go

@@ -63,7 +63,7 @@ func main() {
 	}
 
 	if shouldPopulateStableSourceConfigId() {
-		stable_source_config_id_population.PopulateStableSourceConfigId(db)
+		stable_source_config_id_population.PopulateStableSourceConfigId(db, logger)
 	}
 
 	if err := InstanceMigrate(db, envConf.DBConf); err != nil {
@@ -90,9 +90,9 @@ func shouldKeyRotate() (bool, string, string) {
 }
 
 type StableSourcePopulateConf struct {
-        // we add a dummy field to avoid empty struct issue with envdecode
-	DummyField       string `env:"ASDF,default=asdf"`
-	
+	// we add a dummy field to avoid empty struct issue with envdecode
+	DummyField string `env:"ASDF,default=asdf"`
+
 	// Simple env variable that will let us know if we should populate the stable_source_config_id column
 	POPULATE_SOURCE_CONFIG_ID bool `env:"POPULATE_SOURCE_CONFIG_ID"`
 }

+ 47 - 2
cmd/migrate/stable_source_config_id_population/populate.go

@@ -2,10 +2,13 @@ package stable_source_config_id_population
 
 import (
 	"github.com/porter-dev/porter/internal/models"
+	lr "github.com/porter-dev/porter/pkg/logger"
 	_gorm "gorm.io/gorm"
 )
 
-func PopulateStableSourceConfigId(db *_gorm.DB) {
+func PopulateStableSourceConfigId(db *_gorm.DB, logger *lr.Logger) {
+	logger.Info().Msg("Start populating stable source config id")
+
 	var dest []*models.StackRevision
 	db.Model(&models.StackRevision{}).Preload("SourceConfigs").Find(&dest)
 
@@ -23,9 +26,18 @@ func PopulateStableSourceConfigId(db *_gorm.DB) {
 		}
 	}
 
+	populatedSourceConfigs := []models.StackSourceConfig{}
+
 	// Populate the stable source config id for each revision of the source config
 	for _, sourceConfigsWithSameNameMap := range sourceConfigsPerStack {
 		for _, sc := range sourceConfigsWithSameNameMap {
+
+			// If all source configs have the same stable source config id, then we can skip this step
+			if allSourceConfigsHaveSameStableSourceConfigID(sc) {
+				logger.Info().Msgf("Skipping source configs with stable source config id %s", sc[0].StableSourceConfigID)
+				continue
+			}
+
 			sortedSourceConfigs := sortSourceConfigsByCreationDate(sc)
 
 			stableSourceConfigId := findSourceConfigWithStableSourceConfigID(sortedSourceConfigs)
@@ -35,11 +47,24 @@ func PopulateStableSourceConfigId(db *_gorm.DB) {
 			}
 
 			for _, sourceConfig := range sortedSourceConfigs {
+				if sourceConfig.StableSourceConfigID != "" && sourceConfig.StableSourceConfigID != stableSourceConfigId {
+					logger.Warn().Msgf("source config %s has a different stable source config id %s than %s", sourceConfig.UID, sourceConfig.StableSourceConfigID, stableSourceConfigId)
+					logger.Warn().Msg("This may cause issues with the stack, continuing anyways.")
+				}
+
+				logger.Info().Msg("Populating stable source config id for source config " + sourceConfig.UID)
 				sourceConfig.StableSourceConfigID = stableSourceConfigId
-				db.Save(sourceConfig)
+				populatedSourceConfigs = append(populatedSourceConfigs, sourceConfig)
 			}
 		}
 	}
+
+	for _, sc := range populatedSourceConfigs {
+		if err := db.Save(sc).Error; err != nil {
+			logger.Error().Err(err).Msgf("Failed to save source config with UID %s", sc.UID)
+		}
+	}
+
 }
 
 func findSourceConfigWithStableSourceConfigID(sourceConfigs []models.StackSourceConfig) string {
@@ -63,3 +88,23 @@ func sortSourceConfigsByCreationDate(sourceConfigs []models.StackSourceConfig) [
 
 	return sourceConfigs
 }
+
+// Check if all source configs in the array have populated the same StableSourceConfigID
+func allSourceConfigsHaveSameStableSourceConfigID(sourceConfigs []models.StackSourceConfig) bool {
+	if len(sourceConfigs) == 0 {
+		return true
+	}
+	stableSourceConfigID := sourceConfigs[0].StableSourceConfigID
+
+	if stableSourceConfigID == "" {
+		return false
+	}
+
+	for _, sc := range sourceConfigs {
+		if sc.StableSourceConfigID != stableSourceConfigID {
+			return false
+		}
+	}
+
+	return true
+}

+ 8 - 4
cmd/migrate/stable_source_config_id_population/populate_test.go

@@ -6,9 +6,11 @@ import (
 	"github.com/porter-dev/porter/cmd/migrate/stable_source_config_id_population"
 	"github.com/porter-dev/porter/internal/encryption"
 	"github.com/porter-dev/porter/internal/models"
+	lr "github.com/porter-dev/porter/pkg/logger"
 )
 
 func TestAllSourceConfigHaveSameStableSourceConfigID(t *testing.T) {
+	logger := lr.NewConsole(true)
 
 	tester := &tester{
 		dbFileName: "./porter_stable_source_config_id_population.db",
@@ -28,7 +30,7 @@ func TestAllSourceConfigHaveSameStableSourceConfigID(t *testing.T) {
 
 	createNewStackRevision(tester, t, stackName)
 
-	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB)
+	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB, logger)
 
 	sourceConfigs := []*models.StackSourceConfig{}
 
@@ -56,6 +58,7 @@ func TestAllSourceConfigHaveSameStableSourceConfigID(t *testing.T) {
 }
 
 func TestSourceConfigWithDifferentNamesShouldHaveDifferentStableSourceConfigID(t *testing.T) {
+	logger := lr.NewConsole(true)
 	tester := &tester{
 		dbFileName: "./porter_stable_source_config_id_population.db",
 	}
@@ -79,7 +82,7 @@ func TestSourceConfigWithDifferentNamesShouldHaveDifferentStableSourceConfigID(t
 
 	appendNewSourceConfig(t, tester, tester.initStacks[0], *newSourceConfig)
 
-	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB)
+	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB, logger)
 
 	sourceConfigs := []*models.StackSourceConfig{}
 
@@ -105,7 +108,7 @@ func TestSourceConfigWithDifferentNamesShouldHaveDifferentStableSourceConfigID(t
 	}
 
 	// check if all source configs that share name have the same StableSourceConfigID
-	for sourceConfigName, _ := range sourceConfigMap {
+	for sourceConfigName := range sourceConfigMap {
 		for _, sc := range sourceConfigMap[sourceConfigName] {
 			if sc.StableSourceConfigID != sourceConfigMap[sourceConfigName][0].StableSourceConfigID {
 				t.Fatalf("expected all StableSourceConfigID to be equal, got %s", sc.StableSourceConfigID)
@@ -115,6 +118,7 @@ func TestSourceConfigWithDifferentNamesShouldHaveDifferentStableSourceConfigID(t
 }
 
 func TestSourceConfigsFromDifferentStacksShouldHaveDifferentStableSourceConfigId(t *testing.T) {
+	logger := lr.NewConsole(true)
 	tester := &tester{
 		dbFileName: "./porter_stable_source_config_id_population.db",
 	}
@@ -131,7 +135,7 @@ func TestSourceConfigsFromDifferentStacksShouldHaveDifferentStableSourceConfigId
 	createNewStackRevision(tester, t, "first-stack")
 	createNewStackRevision(tester, t, "second-stack")
 
-	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB)
+	stable_source_config_id_population.PopulateStableSourceConfigId(tester.DB, logger)
 
 	sourceConfigs := []*models.StackSourceConfig{}