Преглед изворни кода

Created populate migration script

jnfrati пре 3 година
родитељ
комит
6401f0dfb5

+ 7 - 1
api/types/stacks.go

@@ -22,6 +22,12 @@ type CreateStackRequest struct {
 	EnvGroups []*CreateStackEnvGroupRequest `json:"env_groups,omitempty" form:"required,dive,required"`
 }
 
+type PutStackSourceConfigPaylod struct {
+	*CreateStackSourceConfigRequest
+
+	StableSourceConfigID string `json:"stable_source_config_id" form:"required"`
+}
+
 // swagger:model
 type PutStackSourceConfigRequest struct {
 	SourceConfigs []*CreateStackSourceConfigRequest `json:"source_configs,omitempty" form:"required,dive,required"`
@@ -262,7 +268,7 @@ type CreateStackSourceConfigRequest struct {
 	// required: true
 	ImageTag string `json:"image_tag" form:"required"`
 
-	StableSourceConfigID string `json:"source_config_id,omitempty"`
+	StableSourceConfigID string `json:"stable_source_config_id,omitempty"`
 
 	// If this field is empty, the resource is deployed directly from the image repo uri
 	StackSourceConfigBuild *StackSourceConfigBuild `json:"build,omitempty"`

+ 21 - 0
cmd/migrate/main.go

@@ -5,6 +5,7 @@ import (
 
 	"github.com/porter-dev/porter/api/server/shared/config/envloader"
 	"github.com/porter-dev/porter/cmd/migrate/keyrotate"
+	"github.com/porter-dev/porter/cmd/migrate/stable_source_config_id_population"
 
 	adapter "github.com/porter-dev/porter/internal/adapter"
 	"github.com/porter-dev/porter/internal/repository/gorm"
@@ -61,6 +62,10 @@ func main() {
 		}
 	}
 
+	if shouldPopulateStableSourceConfigId() {
+		stable_source_config_id_population.PopulateStableSourceConfigId(db)
+	}
+
 	if err := InstanceMigrate(db, envConf.DBConf); err != nil {
 		logger.Fatal().Err(err).Msg("vault migration failed")
 	}
@@ -83,3 +88,19 @@ func shouldKeyRotate() (bool, string, string) {
 
 	return c.OldEncryptionKey != "" && c.NewEncryptionKey != "", c.OldEncryptionKey, c.NewEncryptionKey
 }
+
+type StableSourcePopulateConf struct {
+	// Simple env variable that will let us know if we should populate the stable_source_config_id column
+	POPULATE_SOURCE_CONFIG_ID string `env:"POPULATE_SOURCE_CONFIG_ID"`
+}
+
+func shouldPopulateStableSourceConfigId() bool {
+	var c StableSourcePopulateConf
+
+	if err := envdecode.StrictDecode(&c); err != nil {
+		log.Fatalf("Failed to decode migration conf: %s", err)
+		return false
+	}
+
+	return c.POPULATE_SOURCE_CONFIG_ID == "true"
+}

+ 62 - 0
cmd/migrate/stable_source_config_id_population/populate.go

@@ -0,0 +1,62 @@
+package stable_source_config_id_population
+
+import (
+	"github.com/porter-dev/porter/internal/models"
+	_gorm "gorm.io/gorm"
+)
+
+func PopulateStableSourceConfigId(db *_gorm.DB) {
+	var dest []*models.StackRevision
+	db.Model(&models.StackRevision{}).Preload("SourceConfigs").Find(&dest)
+
+	// Create a map that will separate source configs based on stack and name
+	// this will allow us to check all the source configs revisions that correspond
+	// to the same source config object.
+
+	sourceConfigsPerStack := make(map[uint]map[string][]*models.StackSourceConfig)
+
+	for _, revision := range dest {
+		if sourceConfigsPerStack[revision.StackID] == nil {
+			sourceConfigsPerStack[revision.StackID] = make(map[string][]*models.StackSourceConfig)
+		}
+
+		for _, sc := range revision.SourceConfigs {
+			sourceConfigsPerStack[revision.StackID][sc.Name] = append(sourceConfigsPerStack[revision.StackID][sc.Name], &sc)
+		}
+	}
+
+	// Populate the stable source config id for each revision of the source config
+	for _, sourceConfigs := range sourceConfigsPerStack {
+		for _, v := range sourceConfigs {
+
+			stableSourceConfigId := findSourceConfigWithStableSourceConfigID(v)
+
+			if stableSourceConfigId == "" {
+				stableSourceConfigId = v[0].UID
+			}
+
+			for _, sourceConfig := range v {
+				sourceConfig.StableSourceConfigID = stableSourceConfigId
+			}
+		}
+	}
+
+	// Update the source configs in the database
+	for _, sourceConfigs := range sourceConfigsPerStack {
+		for _, v := range sourceConfigs {
+			for _, sc := range v {
+				db.Save(sc)
+			}
+		}
+	}
+
+}
+
+func findSourceConfigWithStableSourceConfigID(sourceConfigs []*models.StackSourceConfig) string {
+	for _, sc := range sourceConfigs {
+		if sc.StableSourceConfigID != "" {
+			return sc.StableSourceConfigID
+		}
+	}
+	return ""
+}

+ 10 - 2
internal/stacks/helpers.go

@@ -19,12 +19,20 @@ func CloneSourceConfigs(sourceConfigs []models.StackSourceConfig) ([]models.Stac
 			return nil, err
 		}
 
-		res = append(res, models.StackSourceConfig{
+		newSourceConfig := &models.StackSourceConfig{
 			UID:          uid,
 			Name:         sourceConfig.Name,
 			ImageRepoURI: sourceConfig.ImageRepoURI,
 			ImageTag:     sourceConfig.ImageTag,
-		})
+		}
+
+		if sourceConfig.StableSourceConfigID != "" {
+			newSourceConfig.StableSourceConfigID = sourceConfig.StableSourceConfigID
+		} else {
+			newSourceConfig.StableSourceConfigID = string(uid)
+		}
+
+		res = append(res, *newSourceConfig)
 	}
 
 	return res, nil