Skip to content

Commit

Permalink
feat(release): migrate additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
catouberos authored and momo-p committed Jul 21, 2024
1 parent dbb511c commit 379f9de
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions migrations/1721574637_releases_extra_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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)

releases := []*models.Release{}
if err := models.ReleaseQuery(dao).All(&releases); err != nil {
return err
}
for _, release := range releases {
title := &models.Title{}
if err := models.TitleQuery(dao).Where(&dbx.HashExp{
"id": release.TitleId,
}).One(title); err != nil {
return err
}

// default title.name into release.name
release.Name = title.Name

if strings.Contains(release.Type, "Bản in đầu") {
release.Type = ""
}

// turn anything inside bracket into disambiguation
if strings.Contains(release.Type, "(") {
reg := regexp.MustCompile(`(?s)\((.*)\)`)
match := reg.FindAllStringSubmatch(release.Type, -1)
release.Disambiguation = match[0][1]
release.Type = reg.ReplaceAllString(release.Type, "")
}

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

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

0 comments on commit 379f9de

Please sign in to comment.