1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-18 11:46:23 -07:00

Add future-proof functions using struct for context, tx, etc

This commit is contained in:
Eggbertx 2025-02-05 17:32:10 -08:00
parent a38a519e4e
commit 10e0da4492
13 changed files with 332 additions and 286 deletions

View file

@ -80,7 +80,7 @@ func (m *Pre2021Migrator) migrateBan(tx *sql.Tx, ban *migrationBan, boardID *int
migratedBan.Message = ban.reason
migratedBan.StaffID = ban.staffID
migratedBan.StaffNote = ban.staffNote
if err := gcsql.NewIPBanTx(tx, migratedBan); err != nil {
if err := gcsql.NewIPBan(migratedBan, &gcsql.RequestOptions{Tx: tx}); err != nil {
errEv.Err(err).Caller().
Int("oldID", ban.oldID).Msg("Failed to migrate ban")
return err

View file

@ -37,7 +37,7 @@ func (m *Pre2021Migrator) migrateSections() error {
}
var sectionsToBeCreated []gcsql.Section
rows, err := m.db.QuerySQL(sectionsQuery)
rows, err := m.db.Query(nil, sectionsQuery)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to query old database sections")
return err
@ -126,7 +126,7 @@ func (m *Pre2021Migrator) MigrateBoards() error {
}
// get boards from old db
rows, err := m.db.QuerySQL(boardsQuery)
rows, err := m.db.Query(nil, boardsQuery)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to query old database boards")
return err
@ -162,7 +162,7 @@ func (m *Pre2021Migrator) MigrateBoards() error {
Int("migratedBoardID", newBoard.ID).
Msg("Board already exists in new db, updating values")
// don't update other values in the array since they don't affect migrating threads or posts
if _, err = gcsql.ExecSQL(`UPDATE DBPREFIXboards
if _, err = gcsql.Exec(nil, `UPDATE DBPREFIXboards
SET uri = ?, navbar_position = ?, title = ?, subtitle = ?, description = ?,
max_file_size = ?, max_threads = ?, default_style = ?, locked = ?,
anonymous_name = ?, force_anonymous = ?, autosage_after = ?, no_images_after = ?, max_message_length = ?,

View file

@ -1,7 +1,6 @@
package pre2021
import (
"context"
"database/sql"
"time"
@ -35,10 +34,10 @@ type migrationPost struct {
func (m *Pre2021Migrator) migratePost(tx *sql.Tx, post *migrationPost, errEv *zerolog.Event) error {
var err error
opts := &gcsql.RequestOptions{Tx: tx}
if post.oldParentID == 0 {
// migrating post was a thread OP, create the row in the threads table
if post.ThreadID, err = gcsql.CreateThread(tx, post.boardID, false, post.stickied, post.autosage, false); err != nil {
if post.ThreadID, err = gcsql.CreateThread(opts, post.boardID, false, post.stickied, post.autosage, false); err != nil {
errEv.Err(err).Caller().
Int("boardID", post.boardID).
Msg("Failed to create thread")
@ -46,7 +45,7 @@ func (m *Pre2021Migrator) migratePost(tx *sql.Tx, post *migrationPost, errEv *ze
}
// insert thread top post
if err = post.InsertWithContext(context.Background(), tx, true, post.boardID, false, post.stickied, post.autosage, false); err != nil {
if err = post.Insert(true, post.boardID, false, post.stickied, post.autosage, false, opts); err != nil {
errEv.Err(err).Caller().
Int("boardID", post.boardID).
Int("threadID", post.ThreadID).
@ -54,7 +53,7 @@ func (m *Pre2021Migrator) migratePost(tx *sql.Tx, post *migrationPost, errEv *ze
}
if post.filename != "" {
if err = post.AttachFileTx(tx, &gcsql.Upload{
if err = post.AttachFile(&gcsql.Upload{
PostID: post.ID,
OriginalFilename: post.filenameOriginal,
Filename: post.filename,
@ -64,7 +63,7 @@ func (m *Pre2021Migrator) migratePost(tx *sql.Tx, post *migrationPost, errEv *ze
ThumbnailHeight: post.thumbH,
Width: post.imageW,
Height: post.imageH,
}); err != nil {
}, opts); err != nil {
errEv.Err(err).Caller().
Int("oldPostID", post.oldID).
Msg("Failed to attach upload to migrated post")
@ -85,7 +84,7 @@ func (m *Pre2021Migrator) MigratePosts() error {
}
defer tx.Rollback()
rows, err := m.db.QuerySQL(threadsQuery)
rows, err := m.db.Query(nil, threadsQuery)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to get threads")
return err
@ -126,7 +125,7 @@ func (m *Pre2021Migrator) MigratePosts() error {
}
// get and insert replies
replyRows, err := m.db.QuerySQL(postsQuery+" AND parentid = ?", thread.oldID)
replyRows, err := m.db.Query(nil, postsQuery+" AND parentid = ?", thread.oldID)
if err != nil {
errEv.Err(err).Caller().
Int("parentID", thread.oldID).
@ -156,7 +155,7 @@ func (m *Pre2021Migrator) MigratePosts() error {
}
if thread.locked {
if _, err = gcsql.ExecTxSQL(tx, "UPDATE DBPREFIXthreads SET locked = TRUE WHERE id = ?", thread.ThreadID); err != nil {
if _, err = gcsql.Exec(&gcsql.RequestOptions{Tx: tx}, "UPDATE DBPREFIXthreads SET locked = TRUE WHERE id = ?", thread.ThreadID); err != nil {
errEv.Err(err).Caller().
Int("threadID", thread.ThreadID).
Msg("Unable to re-lock migrated thread")