From b2a35c469a1c4334f44178ed7f8098a217292084 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 8 Sep 2021 22:44:46 -0500 Subject: [PATCH] Update project last updated fields on new forum/blog posts --- .../2021-09-09T033556Z_DropUpdateColumns.go | 54 +++++++++++++++++++ src/models/project.go | 11 ++-- src/website/threads_and_posts_helper.go | 26 +++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 src/migration/migrations/2021-09-09T033556Z_DropUpdateColumns.go diff --git a/src/migration/migrations/2021-09-09T033556Z_DropUpdateColumns.go b/src/migration/migrations/2021-09-09T033556Z_DropUpdateColumns.go new file mode 100644 index 00000000..049b8eac --- /dev/null +++ b/src/migration/migrations/2021-09-09T033556Z_DropUpdateColumns.go @@ -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 +} diff --git a/src/models/project.go b/src/models/project.go index 1cd90670..5900312a 100644 --- a/src/models/project.go +++ b/src/models/project.go @@ -53,10 +53,13 @@ type Project struct { LogoLight string `db:"logolight"` LogoDark string `db:"logodark"` - Flags int `db:"flags"` // NOTE(asaf): Flags is currently only used to mark a project as hidden. Flags == 1 means hidden. Flags == 0 means visible. - Featured bool `db:"featured"` - DateApproved time.Time `db:"date_approved"` - AllLastUpdated time.Time `db:"all_last_updated"` + Flags int `db:"flags"` // NOTE(asaf): Flags is currently only used to mark a project as hidden. Flags == 1 means hidden. Flags == 0 means visible. + 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"` diff --git a/src/website/threads_and_posts_helper.go b/src/website/threads_and_posts_helper.go index 0d4ccf3f..6d39085e 100644 --- a/src/website/threads_and_posts_helper.go +++ b/src/website/threads_and_posts_helper.go @@ -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 }