Update project last updated fields on new forum/blog posts
This commit is contained in:
parent
0f9a57f62b
commit
b2a35c469a
|
@ -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
|
||||||
|
}
|
|
@ -57,6 +57,9 @@ type Project struct {
|
||||||
Featured bool `db:"featured"`
|
Featured bool `db:"featured"`
|
||||||
DateApproved time.Time `db:"date_approved"`
|
DateApproved time.Time `db:"date_approved"`
|
||||||
AllLastUpdated time.Time `db:"all_last_updated"`
|
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"`
|
ForumEnabled bool `db:"forum_enabled"`
|
||||||
BlogEnabled bool `db:"blog_enabled"`
|
BlogEnabled bool `db:"blog_enabled"`
|
||||||
|
|
|
@ -3,8 +3,10 @@ package website
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.handmade.network/hmn/hmn/src/db"
|
"git.handmade.network/hmn/hmn/src/db"
|
||||||
|
@ -279,6 +281,30 @@ func CreateNewPost(
|
||||||
panic(oops.New(err, "failed to fix up thread post IDs"))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue