mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-18 11:46:23 -07:00
Add thread post migration for pre2021
This commit is contained in:
parent
c80970c10e
commit
95460e91d6
4 changed files with 83 additions and 29 deletions
|
@ -1,6 +1,7 @@
|
|||
package pre2021
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
|
||||
|
@ -9,16 +10,11 @@ import (
|
|||
|
||||
type postTable struct {
|
||||
gcsql.Post
|
||||
// id int
|
||||
// boardID int
|
||||
// parentID int
|
||||
// name string
|
||||
// tripcode string
|
||||
// email string
|
||||
// subject string
|
||||
// message string
|
||||
// messageRaw string
|
||||
// password string
|
||||
autosage bool
|
||||
bumped time.Time
|
||||
stickied bool
|
||||
locked bool
|
||||
|
||||
filename string
|
||||
filenameOriginal string
|
||||
fileChecksum string
|
||||
|
@ -27,14 +23,7 @@ type postTable struct {
|
|||
imageH int
|
||||
thumbW int
|
||||
thumbH int
|
||||
// ip string
|
||||
// tag string
|
||||
// timestamp time.Time
|
||||
autosage bool
|
||||
bumped time.Time
|
||||
stickied bool
|
||||
locked bool
|
||||
// reviewed bool
|
||||
|
||||
oldID int
|
||||
boardID int
|
||||
oldBoardID int
|
||||
|
@ -94,11 +83,61 @@ func (m *Pre2021Migrator) migratePostsToNewDB() error {
|
|||
continue
|
||||
}
|
||||
|
||||
if thread.ThreadID, err = gcsql.CreateThread(tx, thread.boardID, thread.locked, thread.stickied, thread.autosage, false); err != nil {
|
||||
// create the thread as not locked so migration replies can be inserted. It will be locked after they are all inserted
|
||||
if thread.ThreadID, err = gcsql.CreateThread(tx, thread.boardID, false, thread.stickied, thread.autosage, false); err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Int("boardID", thread.boardID).
|
||||
Msg("Failed to create thread")
|
||||
}
|
||||
|
||||
// insert thread top post
|
||||
if err = thread.InsertWithContext(context.Background(), tx, true, thread.boardID, false, thread.stickied, thread.autosage, false); err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Int("boardID", thread.boardID).
|
||||
Int("threadID", thread.ThreadID).
|
||||
Msg("Failed to insert thread OP")
|
||||
}
|
||||
|
||||
// get and insert replies
|
||||
replyRows, err := m.db.QuerySQL(postsQuery+" AND parentid = ?", thread.oldID)
|
||||
if err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Int("parentID", thread.oldID).
|
||||
Msg("Failed to get reply rows")
|
||||
return err
|
||||
}
|
||||
defer replyRows.Close()
|
||||
|
||||
for replyRows.Next() {
|
||||
var reply postTable
|
||||
if err = replyRows.Scan(
|
||||
&reply.oldID, &reply.oldBoardID, &reply.oldParentID, &reply.Name, &reply.Tripcode, &reply.Email,
|
||||
&reply.Subject, &reply.Message, &reply.MessageRaw, &reply.Password, &reply.filename,
|
||||
&reply.filenameOriginal, &reply.fileChecksum, &reply.filesize, &reply.imageW, &reply.imageH,
|
||||
&reply.thumbW, &reply.thumbH, &reply.IP, &reply.CreatedOn, &reply.autosage,
|
||||
&reply.bumped, &reply.stickied, &reply.locked,
|
||||
); err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Int("parentID", thread.oldID).
|
||||
Msg("Failed to scan reply")
|
||||
return err
|
||||
}
|
||||
reply.ThreadID = thread.ThreadID
|
||||
if err = reply.InsertWithContext(context.Background(), tx, true, reply.boardID, false, false, false, false); err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Int("parentID", thread.oldID).
|
||||
Msg("Failed to insert reply post")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if thread.locked {
|
||||
if _, err = gcsql.ExecTxSQL(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")
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(threadIDsWithInvalidBoards) > 0 {
|
||||
errEv.Caller().
|
||||
|
|
|
@ -33,4 +33,10 @@ func TestMigratePostsToNewDB(t *testing.T) {
|
|||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, 2, numMigratedThreads, "Expected to have three migrated threads")
|
||||
|
||||
var locked bool
|
||||
if !assert.NoError(t, gcsql.QueryRowSQL("SELECT locked FROM DBPREFIXthreads WHERE id = 1", nil, []any{&locked})) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.True(t, locked, "Expected thread ID 1 to be locked")
|
||||
}
|
||||
|
|
|
@ -347,7 +347,7 @@ func (p *Post) Delete() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (p *Post) Insert(bumpThread bool, boardID int, locked bool, stickied bool, anchored bool, cyclical bool) error {
|
||||
func (p *Post) InsertWithContext(ctx context.Context, tx *sql.Tx, bumpThread bool, boardID int, locked bool, stickied bool, anchored bool, cyclical bool) error {
|
||||
if p.ID > 0 {
|
||||
// already inserted
|
||||
return ErrorPostAlreadySent
|
||||
|
@ -358,15 +358,7 @@ func (p *Post) Insert(bumpThread bool, boardID int, locked bool, stickied bool,
|
|||
VALUES(?,?,PARAM_ATON,CURRENT_TIMESTAMP,?,?,?,?,?,?,?,?,?,?)`
|
||||
bumpSQL := `UPDATE DBPREFIXthreads SET last_bump = CURRENT_TIMESTAMP WHERE id = ?`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gcdb.defaultTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := BeginContextTx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var err error
|
||||
if p.ThreadID == 0 {
|
||||
// thread doesn't exist yet, this is a new post
|
||||
p.IsTopPost = true
|
||||
|
@ -401,6 +393,23 @@ func (p *Post) Insert(bumpThread bool, boardID int, locked bool, stickied bool,
|
|||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Post) Insert(bumpThread bool, boardID int, locked bool, stickied bool, anchored bool, cyclical bool) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gcdb.defaultTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := BeginContextTx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if err = p.InsertWithContext(ctx, tx, bumpThread, boardID, locked, stickied, anchored, cyclical); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue