- commit
- 6183647
- parent
- 6509ea2
- author
- Eric Bower
- date
- 2022-08-02 17:42:35 +0000 UTC
fix: migration script for post tags The only way that post tags get added to our database is if the post gets uploaded. If there is a post in our system that already has tags, the tags will show up in the post but not in the blog tag filter. This script backports tags for all posts that already had tags in their system.
1 files changed,
+86,
-0
1@@ -0,0 +1,86 @@
2+package main
3+
4+import (
5+ "database/sql"
6+ "log"
7+ "os"
8+
9+ "git.sr.ht/~erock/pico/prose"
10+ "git.sr.ht/~erock/pico/wish/cms/config"
11+ "git.sr.ht/~erock/pico/wish/cms/db"
12+ "git.sr.ht/~erock/pico/wish/cms/db/postgres"
13+ "go.uber.org/zap"
14+)
15+
16+func createLogger() *zap.SugaredLogger {
17+ logger, err := zap.NewProduction()
18+ if err != nil {
19+ log.Fatal(err)
20+ }
21+
22+ return logger.Sugar()
23+}
24+
25+func findPosts(dbpool *sql.DB) ([]*db.Post, error) {
26+ var posts []*db.Post
27+ rs, err := dbpool.Query(`SELECT
28+ posts.id, user_id, filename, title, text, description,
29+ posts.created_at, publish_at, posts.updated_at, hidden, COALESCE(views, 0) as views
30+ FROM posts
31+ WHERE cur_space = 'prose'
32+ `)
33+ if err != nil {
34+ return posts, err
35+ }
36+ for rs.Next() {
37+ post := &db.Post{}
38+ err := rs.Scan(
39+ &post.ID,
40+ &post.UserID,
41+ &post.Filename,
42+ &post.Title,
43+ &post.Text,
44+ &post.Description,
45+ &post.CreatedAt,
46+ &post.PublishAt,
47+ &post.UpdatedAt,
48+ &post.Hidden,
49+ &post.Views,
50+ )
51+ if err != nil {
52+ return posts, err
53+ }
54+
55+ posts = append(posts, post)
56+ }
57+ if rs.Err() != nil {
58+ return posts, rs.Err()
59+ }
60+ return posts, nil
61+}
62+
63+func main() {
64+ logger := createLogger()
65+
66+ picoCfg := config.NewConfigCms()
67+ picoCfg.Logger = logger
68+ picoCfg.DbURL = os.Getenv("DATABASE_URL")
69+ picoDb := postgres.NewDB(picoCfg)
70+
71+ logger.Info("fetching all posts")
72+ posts, err := findPosts(picoDb.Db)
73+ if err != nil {
74+ panic(err)
75+ }
76+
77+ logger.Info("replacing tags")
78+ for _, post := range posts {
79+ parsed, err := prose.ParseText(post.Text)
80+ if err != nil {
81+ continue
82+ }
83+ if len(parsed.Tags) > 0 {
84+ picoDb.ReplaceTagsForPost(parsed.Tags, post.ID)
85+ }
86+ }
87+}