Add a sort field to subforums

This commit is contained in:
Ben Visness 2021-09-23 01:18:45 -05:00
parent 4b13f99df8
commit 9c7acd7dbb
2 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,52 @@
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(AddSubforumSort{})
}
type AddSubforumSort struct{}
func (m AddSubforumSort) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 9, 23, 6, 12, 11, 0, time.UTC))
}
func (m AddSubforumSort) Name() string {
return "AddSubforumSort"
}
func (m AddSubforumSort) Description() string {
return "Adds a sort field to subforums."
}
func (m AddSubforumSort) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_subforum
ADD sort INTEGER NOT NULL DEFAULT 10;
`)
if err != nil {
return oops.New(err, "failed to add sort key")
}
return nil
}
func (m AddSubforumSort) Down(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE handmade_subforum
DROP sort;
`)
if err != nil {
return oops.New(err, "failed to drop sort key")
}
return nil
}

View File

@ -52,7 +52,7 @@ func GetFullSubforumTree(ctx context.Context, conn *pgxpool.Pool) SubforumTree {
SELECT $columns
FROM
handmade_subforum as sf
ORDER BY id ASC
ORDER BY sort, id ASC
`,
)
if err != nil {