-
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(publications): add subtitle field migration
- Loading branch information
1 parent
0b6eb2d
commit bbfbf46
Showing
1 changed file
with
44 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,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 | ||
}) | ||
} |