- commit
- d3089f9
- parent
- 30c26ef
- author
- Eric Bower
- date
- 2022-07-28 13:11:01 +0000 UTC
preserve view counts
1 files changed,
+55,
-16
+55,
-16
1@@ -22,7 +22,45 @@ func createLogger() *zap.SugaredLogger {
2 return logger.Sugar()
3 }
4
5-func InsertUser(tx *sql.Tx, user *db.User) error {
6+func findPosts(dbpool *sql.DB) ([]*db.Post, error) {
7+ var posts []*db.Post
8+ rs, err := dbpool.Query(`SELECT
9+ posts.id, user_id, filename, title, text, description,
10+ posts.created_at, publish_at, posts.updated_at, hidden, COALESCE(views, 0) as views
11+ FROM posts
12+ LEFT OUTER JOIN post_analytics ON post_analytics.post_id = posts.id
13+ `)
14+ if err != nil {
15+ return posts, err
16+ }
17+ for rs.Next() {
18+ post := &db.Post{}
19+ err := rs.Scan(
20+ &post.ID,
21+ &post.UserID,
22+ &post.Filename,
23+ &post.Title,
24+ &post.Text,
25+ &post.Description,
26+ &post.CreatedAt,
27+ &post.PublishAt,
28+ &post.UpdatedAt,
29+ &post.Hidden,
30+ &post.Views,
31+ )
32+ if err != nil {
33+ return posts, err
34+ }
35+
36+ posts = append(posts, post)
37+ }
38+ if rs.Err() != nil {
39+ return posts, rs.Err()
40+ }
41+ return posts, nil
42+}
43+
44+func insertUser(tx *sql.Tx, user *db.User) error {
45 _, err := tx.Exec(
46 "INSERT INTO app_users (id, name, created_at) VALUES($1, $2, $3)",
47 user.ID,
48@@ -32,7 +70,7 @@ func InsertUser(tx *sql.Tx, user *db.User) error {
49 return err
50 }
51
52-func InsertPublicKey(tx *sql.Tx, pk *db.PublicKey) error {
53+func insertPublicKey(tx *sql.Tx, pk *db.PublicKey) error {
54 _, err := tx.Exec(
55 "INSERT INTO public_keys (id, user_id, public_key, created_at) VALUES ($1, $2, $3, $4)",
56 pk.ID,
57@@ -43,11 +81,11 @@ func InsertPublicKey(tx *sql.Tx, pk *db.PublicKey) error {
58 return err
59 }
60
61-func InsertPost(tx *sql.Tx, post *db.Post) error {
62+func insertPost(tx *sql.Tx, post *db.Post) error {
63 _, err := tx.Exec(
64 `INSERT INTO posts
65- (id, user_id, title, text, created_at, publish_at, updated_at, description, filename, hidden, cur_space)
66- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
67+ (id, user_id, title, text, created_at, publish_at, updated_at, description, filename, hidden, cur_space, views)
68+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
69 post.ID,
70 post.UserID,
71 post.Title,
72@@ -59,6 +97,7 @@ func InsertPost(tx *sql.Tx, post *db.Post) error {
73 post.Filename,
74 post.Hidden,
75 post.Space,
76+ post.Views,
77 )
78 return err
79 }
80@@ -112,7 +151,7 @@ func main() {
81 for _, proseUser := range proseUsers {
82 userMap[proseUser.Name] = proseUser
83
84- err = InsertUser(tx, proseUser)
85+ err = insertUser(tx, proseUser)
86 if err != nil {
87 panic(err)
88 }
89@@ -152,7 +191,7 @@ func main() {
90 for _, prosePK := range proseKeys {
91 pkMap[prosePK.Key] = true
92
93- err = InsertPublicKey(tx, prosePK)
94+ err = insertPublicKey(tx, prosePK)
95 if err != nil {
96 panic(err)
97 }
98@@ -176,13 +215,13 @@ func main() {
99
100 logger.Infof("Adding records with no conflicts (%d)", len(noconflicts))
101 for _, data := range noconflicts {
102- err = InsertUser(tx, data.User)
103+ err = insertUser(tx, data.User)
104 if err != nil {
105 panic(err)
106 }
107
108 for _, pk := range data.Pks {
109- err = InsertPublicKey(tx, pk)
110+ err = insertPublicKey(tx, pk)
111 if err != nil {
112 panic(err)
113 }
114@@ -192,20 +231,20 @@ func main() {
115 logger.Infof("Adding records with conflicts (%d)", len(conflicts))
116 for _, data := range conflicts {
117 data.User.Name = fmt.Sprintf("%stmp", data.User.Name)
118- err = InsertUser(tx, data.User)
119+ err = insertUser(tx, data.User)
120 if err != nil {
121 panic(err)
122 }
123
124 for _, pk := range data.Pks {
125- err = InsertPublicKey(tx, pk)
126+ err = insertPublicKey(tx, pk)
127 if err != nil {
128 panic(err)
129 }
130 }
131 }
132
133- prosePosts, err := proseDb.FindPosts()
134+ prosePosts, err := findPosts(proseDb.Db)
135 if err != nil {
136 panic(err)
137 }
138@@ -213,13 +252,13 @@ func main() {
139 logger.Info("Adding posts from prose.sh")
140 for _, post := range prosePosts {
141 post.Space = "prose"
142- err = InsertPost(tx, post)
143+ err = insertPost(tx, post)
144 if err != nil {
145 panic(err)
146 }
147 }
148
149- listPosts, err := listsDb.FindPosts()
150+ listPosts, err := findPosts(listsDb.Db)
151 if err != nil {
152 panic(err)
153 }
154@@ -233,7 +272,7 @@ func main() {
155 // because we were able to determine it was the same user
156 post.UserID = alreadyAdded.ReplaceWithID
157 post.Space = "lists"
158- err = InsertPost(tx, post)
159+ err = insertPost(tx, post)
160 if err != nil {
161 fmt.Println(post.Filename)
162 panic(err)
163@@ -248,7 +287,7 @@ func main() {
164 }
165
166 post.Space = "lists"
167- err = InsertPost(tx, post)
168+ err = insertPost(tx, post)
169 if err != nil {
170 panic(err)
171 }