You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/11/14 07:29:29 UTC

[GitHub] [incubator-devlake] warren830 commented on a diff in pull request #3728: fix(framework): fix migration scripts

warren830 commented on code in PR #3728:
URL: https://github.com/apache/incubator-devlake/pull/3728#discussion_r1021151408


##########
models/migrationscripts/20220903_encrypt_blueprint.go:
##########
@@ -30,63 +28,59 @@ var _ core.MigrationScript = (*encryptBlueprint)(nil)
 
 type encryptBlueprint struct{}
 
-type blueprint20220903Before struct {
-	Name           string          `json:"name" validate:"required"`
-	Mode           string          `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Plan           json.RawMessage `json:"plan"`
-	Enable         bool            `json:"enable"`
-	CronConfig     string          `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
-	IsManual       bool            `json:"isManual"`
-	Settings       json.RawMessage `json:"settings" swaggertype:"array,string" example:"please check api: /blueprints/<PLUGIN_NAME>/blueprint-setting"`
-	archived.Model `swaggerignore:"true"`
+type BlueprintEncryption0904Before struct {
+	archived.Model
+	Plan     string
+	Settings string
+}
+
+func (BlueprintEncryption0904Before) TableName() string {

Review Comment:
   fixed



##########
models/migrationscripts/20220903_encrypt_blueprint.go:
##########
@@ -30,63 +28,59 @@ var _ core.MigrationScript = (*encryptBlueprint)(nil)
 
 type encryptBlueprint struct{}
 
-type blueprint20220903Before struct {
-	Name           string          `json:"name" validate:"required"`
-	Mode           string          `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Plan           json.RawMessage `json:"plan"`
-	Enable         bool            `json:"enable"`
-	CronConfig     string          `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
-	IsManual       bool            `json:"isManual"`
-	Settings       json.RawMessage `json:"settings" swaggertype:"array,string" example:"please check api: /blueprints/<PLUGIN_NAME>/blueprint-setting"`
-	archived.Model `swaggerignore:"true"`
+type BlueprintEncryption0904Before struct {
+	archived.Model
+	Plan     string
+	Settings string
+}
+
+func (BlueprintEncryption0904Before) TableName() string {
+	return "_devlake_blueprints"
+}
+
+type BlueprintEncryption0904After struct {

Review Comment:
   fixed



##########
models/migrationscripts/20220903_encrypt_blueprint.go:
##########
@@ -30,63 +28,59 @@ var _ core.MigrationScript = (*encryptBlueprint)(nil)
 
 type encryptBlueprint struct{}
 
-type blueprint20220903Before struct {
-	Name           string          `json:"name" validate:"required"`
-	Mode           string          `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Plan           json.RawMessage `json:"plan"`
-	Enable         bool            `json:"enable"`
-	CronConfig     string          `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
-	IsManual       bool            `json:"isManual"`
-	Settings       json.RawMessage `json:"settings" swaggertype:"array,string" example:"please check api: /blueprints/<PLUGIN_NAME>/blueprint-setting"`
-	archived.Model `swaggerignore:"true"`
+type BlueprintEncryption0904Before struct {
+	archived.Model
+	Plan     string
+	Settings string
+}
+
+func (BlueprintEncryption0904Before) TableName() string {
+	return "_devlake_blueprints"
+}
+
+type BlueprintEncryption0904After struct {
+	archived.Model
+	Plan     string
+	Settings string
 }
 
-type blueprint20220903After struct {
-	/* unchanged part */
-	Name           string `json:"name" validate:"required"`
-	Mode           string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Enable         bool   `json:"enable"`
-	CronConfig     string `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
-	IsManual       bool   `json:"isManual"`
-	archived.Model `swaggerignore:"true"`
-	/* changed part */
-	Plan     string `json:"plan"`
-	Settings string `json:"settings"`
+func (BlueprintEncryption0904After) TableName() string {
+	return "_devlake_blueprints"
 }
 
 func (script *encryptBlueprint) Up(basicRes core.BasicRes) errors.Error {
+	db := basicRes.GetDal()
 	encKey := basicRes.GetConfig(core.EncodeKeyEnvStr)
 	if encKey == "" {
 		return errors.BadInput.New("invalid encKey")
 	}
-
-	return migrationhelper.TransformTable(
+	blueprint := &BlueprintEncryption0904After{}
+	err := migrationhelper.TransformColumns(
 		basicRes,
 		script,
 		"_devlake_blueprints",
-		func(s *blueprint20220903Before) (*blueprint20220903After, errors.Error) {
-			encryptedPlan, err := core.Encrypt(encKey, string(s.Plan))
+		[]string{"plan", "settings"},
+		func(src *BlueprintEncryption0904Before) (*BlueprintEncryption0904After, errors.Error) {
+			plan, err := core.Encrypt(encKey, src.Plan)
 			if err != nil {
 				return nil, err
 			}
-			encryptedSettings, err := core.Encrypt(encKey, string(s.Settings))
+			settings, err := core.Encrypt(encKey, src.Settings)
 			if err != nil {
 				return nil, err
 			}
-
-			dst := &blueprint20220903After{
-				Name:       s.Name,
-				Mode:       s.Mode,
-				Enable:     s.Enable,
-				CronConfig: s.CronConfig,
-				IsManual:   s.IsManual,
-				Model:      archived.Model{ID: s.ID},
-				Plan:       encryptedPlan,
-				Settings:   encryptedSettings,
-			}
-			return dst, nil
+			return &BlueprintEncryption0904After{
+				Model:    src.Model,
+				Plan:     plan,
+				Settings: settings,
+			}, nil
 		},
 	)
+	if err != nil {
+		return err
+	}
+	_ = db.First(blueprint)

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org