Update project last updated fields on new forum/blog posts

This commit is contained in:
Ben Visness 2021-09-08 22:44:46 -05:00
parent 0f9a57f62b
commit b2a35c469a
3 changed files with 87 additions and 4 deletions

View File

@ -0,0 +1,54 @@
package migrations
import (
"context"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"git.handmade.network/hmn/hmn/src/oops"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(DropUpdateColumns{})
}
type DropUpdateColumns struct{}
func (m DropUpdateColumns) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 9, 9, 3, 35, 56, 0, time.UTC))
}
func (m DropUpdateColumns) Name() string {
return "DropUpdateColumns"
}
func (m DropUpdateColumns) Description() string {
return "Drop old columns related to update times"
}
func (m DropUpdateColumns) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_project
DROP profile_last_updated,
DROP static_last_updated;
`)
if err != nil {
return oops.New(err, "failed to drop update columns")
}
return nil
}
func (m DropUpdateColumns) Down(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_project
ADD profile_last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT 'epoch',
ADD static_last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT 'epoch';
`)
if err != nil {
return oops.New(err, "failed to add old update columns")
}
return nil
}

View File

@ -57,6 +57,9 @@ type Project struct {
Featured bool `db:"featured"`
DateApproved time.Time `db:"date_approved"`
AllLastUpdated time.Time `db:"all_last_updated"`
ForumLastUpdated time.Time `db:"forum_last_updated"`
BlogLastUpdated time.Time `db:"blog_last_updated"`
AnnotationLastUpdated time.Time `db:"annotation_last_updated"`
ForumEnabled bool `db:"forum_enabled"`
BlogEnabled bool `db:"blog_enabled"`

View File

@ -3,8 +3,10 @@ package website
import (
"context"
"errors"
"fmt"
"math"
"net"
"strings"
"time"
"git.handmade.network/hmn/hmn/src/db"
@ -279,6 +281,30 @@ func CreateNewPost(
panic(oops.New(err, "failed to fix up thread post IDs"))
}
// Track a project update
updateEntries := []string{"all_last_updated"}
switch threadType {
case models.ThreadTypeForumPost:
updateEntries = append(updateEntries, "forum_last_updated")
case models.ThreadTypeProjectBlogPost, models.ThreadTypePersonalBlogPost:
updateEntries = append(updateEntries, "blog_last_updated")
}
for i := range updateEntries {
updateEntries[i] = fmt.Sprintf("%s = $2", updateEntries[i])
}
updates := strings.Join(updateEntries, ", ")
_, err = tx.Exec(ctx,
`
UPDATE handmade_project
SET `+updates+`
WHERE
id = $1
`,
projectId,
time.Now(),
)
return
}