diff --git a/src/migration/migrations/2021-09-23T061211Z_AddSubforumSort.go b/src/migration/migrations/2021-09-23T061211Z_AddSubforumSort.go new file mode 100644 index 00000000..c7c39fdc --- /dev/null +++ b/src/migration/migrations/2021-09-23T061211Z_AddSubforumSort.go @@ -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 +} diff --git a/src/models/subforum.go b/src/models/subforum.go index f65570e2..38089f3d 100644 --- a/src/models/subforum.go +++ b/src/models/subforum.go @@ -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 {