Skip to content

chore(text): migrate text component to new fields #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ database:
host: pg-sql
port: 5432
name: pipeline
version: 22
version: 23
timezone: Etc/UTC
pool:
idleconnections: 5
Expand Down
Empty file.
Empty file.
174 changes: 174 additions & 0 deletions pkg/db/migration/convert/convert000023/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package convert000023

import (
"fmt"
"log"

"github.com/instill-ai/pipeline-backend/pkg/datamodel"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"gorm.io/gorm"
)

const batchSize = 100

type TextFieldsConverter struct {
DB *gorm.DB
Logger *zap.Logger
}

func (c *TextFieldsConverter) Migrate() error {
if err := c.migratePipeline(); err != nil {
return err
}

Check warning on line 23 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L20-L23

Added lines #L20 - L23 were not covered by tests

return c.migratePipelineRelease()

Check warning on line 25 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L25

Added line #L25 was not covered by tests
}

func (c *TextFieldsConverter) migratePipeline() error {
pipelines := make([]*datamodel.Pipeline, 0, batchSize)

return c.DB.Select("uid", "recipe_yaml", "recipe").FindInBatches(&pipelines, batchSize, func(tx *gorm.DB, _ int) error {
for _, p := range pipelines {
isRecipeUpdated := false
l := c.Logger.With(zap.String("pipelineUID", p.UID.String()))

for id, comp := range p.Recipe.Component {
isComponentUpdated, err := c.updateTask(comp)
if err != nil {
l.With(zap.String("componentID", id), zap.Error(err)).
Error("Failed to update pipeline.")

return fmt.Errorf("updating pipeline component: %w", err)
}

Check warning on line 43 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L28-L43

Added lines #L28 - L43 were not covered by tests

isRecipeUpdated = isComponentUpdated || isRecipeUpdated

Check warning on line 45 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L45

Added line #L45 was not covered by tests
}

if isRecipeUpdated {
recipeYAML, err := yaml.Marshal(p.Recipe)
if err != nil {
return fmt.Errorf("marshalling recipe: %w", err)
}
result := tx.Model(p).Where("uid = ?", p.UID).Update("recipe_yaml", string(recipeYAML))
if result.Error != nil {
l.Error("Failed to update pipeline release.")
return fmt.Errorf("updating pipeline recipe: %w", result.Error)
}

Check warning on line 57 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L48-L57

Added lines #L48 - L57 were not covered by tests
}
}

return nil

Check warning on line 61 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L61

Added line #L61 was not covered by tests
}).Error
}

func (c *TextFieldsConverter) migratePipelineRelease() error {
pipelineReleases := make([]*datamodel.PipelineRelease, 0, batchSize)
return c.DB.Select("uid", "recipe_yaml", "recipe").FindInBatches(&pipelineReleases, batchSize, func(tx *gorm.DB, _ int) error {
for _, pr := range pipelineReleases {
isRecipeUpdated := false
l := c.Logger.With(zap.String("pipelineReleaseUID", pr.UID.String()))

for id, comp := range pr.Recipe.Component {
isComponentUpdated, err := c.updateTask(comp)
if err != nil {
l.With(zap.String("componentID", id), zap.Error(err)).
Error("Failed to update pipeline release.")

return fmt.Errorf("updating pipeline release component: %w", err)
}

Check warning on line 79 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L65-L79

Added lines #L65 - L79 were not covered by tests

isRecipeUpdated = isComponentUpdated || isRecipeUpdated

Check warning on line 81 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L81

Added line #L81 was not covered by tests
}

if isRecipeUpdated {
recipeYAML, err := yaml.Marshal(pr.Recipe)
if err != nil {
return fmt.Errorf("marshalling recipe: %w", err)
}

Check warning on line 88 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L84-L88

Added lines #L84 - L88 were not covered by tests

result := tx.Model(pr).Where("uid = ?", pr.UID).Update("recipe_yaml", string(recipeYAML))
if result.Error != nil {
l.Error("Failed to update pipeline release.")
return fmt.Errorf("updating pipeline release recipe: %w", result.Error)
}

Check warning on line 94 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L90-L94

Added lines #L90 - L94 were not covered by tests
}
}

return nil

Check warning on line 98 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L98

Added line #L98 was not covered by tests
}).Error
}

func (c *TextFieldsConverter) updateTask(comp *datamodel.Component) (bool, error) {

isConverted, err := c.convertField(comp)
if err != nil {
return false, fmt.Errorf("converting type from website to web and task from TASK_SCRAPE_WEBSITE to TASK_CRAWL_WEBSITE: %w", err)
}

Check warning on line 107 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L102-L107

Added lines #L102 - L107 were not covered by tests

return isConverted, nil

Check warning on line 109 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L109

Added line #L109 was not covered by tests
}

func (c *TextFieldsConverter) convertField(comp *datamodel.Component) (bool, error) {

if comp.Type == "iterator" {
isComponentUpdated := false
for _, comp := range comp.Component {
isSubComponentUpdated, err := c.convertField(comp)
if err != nil {
return false, fmt.Errorf("updating iterator component: %w", err)
}
isComponentUpdated = isSubComponentUpdated || isComponentUpdated

Check warning on line 121 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L112-L121

Added lines #L112 - L121 were not covered by tests
}

return isComponentUpdated, nil

Check warning on line 124 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L124

Added line #L124 was not covered by tests
}

if !(comp.Type == "text" && comp.Task == "TASK_CHUNK_TEXT") {
return false, nil
}

Check warning on line 129 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L127-L129

Added lines #L127 - L129 were not covered by tests

in, isMap := comp.Input.(map[string]any)

if !isMap {
log.Println("Invalid input type on TASK_CHUNK_TEXT")
return false, nil
}

Check warning on line 136 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L131-L136

Added lines #L131 - L136 were not covered by tests

strategyMap, ok := in["strategy"].(map[string]interface{})
if !ok {
log.Println("Missing field strategy on TASK_CHUNK_TEXT")
return false, nil
}

Check warning on line 142 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L138-L142

Added lines #L138 - L142 were not covered by tests

settingMap, ok := strategyMap["setting"].(map[string]interface{})
if !ok {
log.Println("Missing field setting on TASK_CHUNK_TEXT")
return false, nil
}

Check warning on line 148 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L144-L148

Added lines #L144 - L148 were not covered by tests

if !ok {
log.Println("Missing field chunk-method on TASK_CHUNK_TEXT")
return false, nil
}

Check warning on line 153 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L150-L153

Added lines #L150 - L153 were not covered by tests

modelName, ok := settingMap["model-name"].(string)

if !ok {
log.Println("Missing field model-name on TASK_CHUNK_TEXT")
return false, nil
}

Check warning on line 160 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L155-L160

Added lines #L155 - L160 were not covered by tests

tokenization := map[string]interface{}{
"choice": map[string]interface{}{
"model": modelName,
"tokenization-method": "Model",
},
}

in["tokenization"] = tokenization
delete(settingMap, "model-name")

return true, nil

Check warning on line 172 in pkg/db/migration/convert/convert000023/convert.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/convert/convert000023/convert.go#L162-L172

Added lines #L162 - L172 were not covered by tests

}
6 changes: 6 additions & 0 deletions pkg/db/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"github.com/instill-ai/pipeline-backend/pkg/db/migration/convert/convert000020"
"github.com/instill-ai/pipeline-backend/pkg/db/migration/convert/convert000021"
"github.com/instill-ai/pipeline-backend/pkg/db/migration/convert/convert000022"
"github.com/instill-ai/pipeline-backend/pkg/db/migration/convert/convert000023"
"github.com/instill-ai/pipeline-backend/pkg/external"
"github.com/instill-ai/pipeline-backend/pkg/logger"

Expand Down Expand Up @@ -67,6 +68,11 @@
DB: db,
Logger: l,
}
case 23:
m = &convert000023.TextFieldsConverter{
DB: db,
Logger: l,
}

Check warning on line 75 in pkg/db/migration/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/migration/migration.go#L71-L75

Added lines #L71 - L75 were not covered by tests
default:
return nil
}
Expand Down
Loading