|
| 1 | +package convert000040 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "go.uber.org/zap" |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | + "gorm.io/gorm" |
| 10 | + |
| 11 | + "github.com/instill-ai/pipeline-backend/pkg/datamodel" |
| 12 | + "github.com/instill-ai/pipeline-backend/pkg/db/migration/convert" |
| 13 | +) |
| 14 | + |
| 15 | +const batchSize = 100 |
| 16 | + |
| 17 | +type RenameFormatToType struct { |
| 18 | + convert.Basic |
| 19 | +} |
| 20 | + |
| 21 | +func (c *RenameFormatToType) Migrate() error { |
| 22 | + if err := c.migratePipeline(); err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + return c.migratePipelineRelease() |
| 27 | +} |
| 28 | + |
| 29 | +func (c *RenameFormatToType) migratePipeline() error { |
| 30 | + pipelines := make([]*datamodel.Pipeline, 0, batchSize) |
| 31 | + return c.DB.Select("uid", "recipe_yaml").FindInBatches(&pipelines, batchSize, func(tx *gorm.DB, _ int) error { |
| 32 | + for _, p := range pipelines { |
| 33 | + isRecipeUpdated := false |
| 34 | + l := c.Logger.With(zap.String("pipelineUID", p.UID.String())) |
| 35 | + |
| 36 | + var node yaml.Node |
| 37 | + if p.Recipe != nil { |
| 38 | + // Update recipe_yaml content |
| 39 | + if err := yaml.Unmarshal([]byte(p.RecipeYAML), &node); err != nil { |
| 40 | + return fmt.Errorf("unmarshaling recipe yaml: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + // Find and update the variable section |
| 44 | + if len(node.Content) > 0 && len(node.Content[0].Content) > 0 { |
| 45 | + for i := 0; i < len(node.Content[0].Content); i += 2 { |
| 46 | + if node.Content[0].Content[i].Value == "variable" { |
| 47 | + variableNode := node.Content[0].Content[i+1] |
| 48 | + for j := 0; j < len(variableNode.Content); j += 2 { |
| 49 | + varContent := variableNode.Content[j+1] |
| 50 | + for k := 0; k < len(varContent.Content); k += 2 { |
| 51 | + if varContent.Content[k].Value == "format" { |
| 52 | + varContent.Content[k].Value = "type" |
| 53 | + isRecipeUpdated = true |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if isRecipeUpdated { |
| 64 | + buf := bytes.Buffer{} |
| 65 | + encoder := yaml.NewEncoder(&buf) |
| 66 | + encoder.SetIndent(2) |
| 67 | + err := encoder.Encode(&node.Content[0]) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("marshalling recipe: %w", err) |
| 70 | + } |
| 71 | + recipeYAML := buf.String() |
| 72 | + result := tx.Model(p).Where("uid = ?", p.UID).Update("recipe_yaml", recipeYAML) |
| 73 | + if result.Error != nil { |
| 74 | + l.Error("Failed to update pipeline.") |
| 75 | + return fmt.Errorf("updating pipeline recipe: %w", result.Error) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | + }).Error |
| 82 | +} |
| 83 | + |
| 84 | +func (c *RenameFormatToType) migratePipelineRelease() error { |
| 85 | + releases := make([]*datamodel.PipelineRelease, 0, batchSize) |
| 86 | + return c.DB.Select("uid", "recipe_yaml").FindInBatches(&releases, batchSize, func(tx *gorm.DB, _ int) error { |
| 87 | + for _, r := range releases { |
| 88 | + isRecipeUpdated := false |
| 89 | + l := c.Logger.With(zap.String("pipelineReleaseUID", r.UID.String())) |
| 90 | + |
| 91 | + var node yaml.Node |
| 92 | + if r.Recipe != nil { |
| 93 | + // Update recipe_yaml content |
| 94 | + if err := yaml.Unmarshal([]byte(r.RecipeYAML), &node); err != nil { |
| 95 | + return fmt.Errorf("unmarshaling recipe yaml: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Find and update the variable section |
| 99 | + if len(node.Content) > 0 && len(node.Content[0].Content) > 0 { |
| 100 | + for i := 0; i < len(node.Content[0].Content); i += 2 { |
| 101 | + if node.Content[0].Content[i].Value == "variable" { |
| 102 | + variableNode := node.Content[0].Content[i+1] |
| 103 | + for j := 0; j < len(variableNode.Content); j += 2 { |
| 104 | + varContent := variableNode.Content[j+1] |
| 105 | + for k := 0; k < len(varContent.Content); k += 2 { |
| 106 | + if varContent.Content[k].Value == "format" { |
| 107 | + varContent.Content[k].Value = "type" |
| 108 | + isRecipeUpdated = true |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if isRecipeUpdated { |
| 119 | + buf := bytes.Buffer{} |
| 120 | + encoder := yaml.NewEncoder(&buf) |
| 121 | + encoder.SetIndent(2) |
| 122 | + err := encoder.Encode(&node.Content[0]) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("marshalling recipe: %w", err) |
| 125 | + } |
| 126 | + recipeYAML := buf.String() |
| 127 | + result := tx.Model(r).Where("uid = ?", r.UID).Update("recipe_yaml", recipeYAML) |
| 128 | + if result.Error != nil { |
| 129 | + l.Error("Failed to update pipeline release.") |
| 130 | + return fmt.Errorf("updating pipeline recipe: %w", result.Error) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | + }).Error |
| 137 | +} |
0 commit comments