-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(release): migrate additional fields
- Loading branch information
1 parent
dbb511c
commit 379f9de
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |