Eric Bower
·
08 Apr 24
dates.go
1package main
2
3import (
4 "context"
5 "database/sql"
6 "log/slog"
7 "os"
8 "time"
9
10 "github.com/picosh/pico/db"
11 "github.com/picosh/pico/db/postgres"
12 "github.com/picosh/pico/shared"
13)
14
15func findPosts(dbpool *sql.DB) ([]*db.Post, error) {
16 var posts []*db.Post
17 rs, err := dbpool.Query(`SELECT
18 id, user_id, filename, title, text, description,
19 created_at, publish_at, updated_at, hidden, cur_space
20 FROM posts
21 WHERE cur_space = 'prose' OR cur_space = 'lists'
22 `)
23 if err != nil {
24 return posts, err
25 }
26 for rs.Next() {
27 post := &db.Post{}
28 err := rs.Scan(
29 &post.ID,
30 &post.UserID,
31 &post.Filename,
32 &post.Title,
33 &post.Text,
34 &post.Description,
35 &post.CreatedAt,
36 &post.PublishAt,
37 &post.UpdatedAt,
38 &post.Hidden,
39 &post.Space,
40 )
41 if err != nil {
42 return posts, err
43 }
44
45 posts = append(posts, post)
46 }
47 if rs.Err() != nil {
48 return posts, rs.Err()
49 }
50 return posts, nil
51}
52
53func updateDates(tx *sql.Tx, postID string, date *time.Time) error {
54 _, err := tx.Exec("UPDATE posts SET publish_at = $1 WHERE id = $2", date, postID)
55 return err
56}
57
58func main() {
59 logger := slog.Default()
60
61 picoCfg := shared.NewConfigSite()
62 picoCfg.Logger = logger
63 picoCfg.DbURL = os.Getenv("DATABASE_URL")
64 picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)
65
66 logger.Info("fetching all posts")
67 posts, err := findPosts(picoDb.Db)
68 if err != nil {
69 panic(err)
70 }
71 logger.Info("found posts", "len", len(posts))
72
73 ctx := context.Background()
74 tx, err := picoDb.Db.BeginTx(ctx, nil)
75 if err != nil {
76 panic(err)
77 }
78
79 defer func() {
80 err = tx.Rollback()
81 panic(err)
82 }()
83
84 datesFixed := []string{}
85 logger.Info("updating dates")
86 for _, post := range posts {
87 if post.Space == "prose" {
88 parsed, err := shared.ParseText(post.Text)
89 if err != nil {
90 logger.Error(err.Error())
91 continue
92 }
93
94 if parsed.PublishAt != nil && !parsed.PublishAt.IsZero() {
95 err = updateDates(tx, post.ID, parsed.MetaData.PublishAt)
96 if err != nil {
97 logger.Error(err.Error())
98 continue
99 }
100
101 if !parsed.MetaData.PublishAt.Equal(*post.PublishAt) {
102 datesFixed = append(datesFixed, post.ID)
103 }
104 }
105 } else if post.Space == "lists" {
106 parsed := shared.ListParseText(post.Text)
107 if err != nil {
108 logger.Error(err.Error())
109 continue
110 }
111
112 if parsed.PublishAt != nil && !parsed.PublishAt.IsZero() {
113 err = updateDates(tx, post.ID, parsed.PublishAt)
114 if err != nil {
115 logger.Error(err.Error())
116 continue
117 }
118 if !parsed.PublishAt.Equal(*post.PublishAt) {
119 datesFixed = append(datesFixed, post.ID)
120 }
121 }
122 }
123 }
124
125 err = tx.Commit()
126 if err != nil {
127 panic(err)
128 }
129 logger.Info("dates fixed!", "len", len(datesFixed))
130}