Skip to content

Commit

Permalink
feat(publications): add subtitle field migration
Browse files Browse the repository at this point in the history
  • Loading branch information
catouberos authored and momo-p committed Oct 25, 2024
1 parent 0b6eb2d commit bbfbf46
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions migrations/1729845456_publications_extra_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package migrations

import (
"regexp"
"strings"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"tana.moe/momoka-lite/models"
)

func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db)

publications := []*models.Publication{}
if err := models.PublicationQuery(dao).All(&publications); err != nil {
return err
}
for _, publication := range publications {
// match last bracket and turn it into subtitle
if strings.HasSuffix(publication.Name, ")") {
reg := regexp.MustCompile(`(?s)\((.*)\)$`)
match := reg.FindAllStringSubmatch(publication.Name, -1)
publication.Subtitle = match[0][1]
publication.Name = reg.ReplaceAllString(publication.Name, "")
}

// trim outdated reprint string
if strings.Contains(publication.Subtitle, "Tái bản") {
publication.Subtitle = ""
}

if err := dao.Save(publication); err != nil {
return err
}
}

return nil
}, func(db dbx.Builder) error {
return nil
})
}

0 comments on commit bbfbf46

Please sign in to comment.