Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrate

import (
"bufio"
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -118,7 +119,7 @@ func (m *Migration) LogString() string {

// Buffer buffers Body up to BufferSize.
// Calling this function blocks. Call with goroutine.
func (m *Migration) Buffer() error {
func (m *Migration) Buffer() (berr error) {
if m.Body == nil {
return nil
}
Expand All @@ -127,6 +128,21 @@ func (m *Migration) Buffer() error {

b := bufio.NewReaderSize(m.Body, int(m.BufferSize))

// defer closing buffer writer and body.
defer func() {
// close bufferWriter so Buffer knows that there is no
// more data coming.
if err := m.bufferWriter.Close(); err != nil {
berr = errors.Join(berr, err)
}

// it's safe to close the Body too.
if err := m.Body.Close(); err != nil {
berr = errors.Join(berr, err)
}

}()

// start reading from body, peek won't move the read pointer though
// poor man's solution?
if _, err := b.Peek(int(m.BufferSize)); err != nil && err != io.EOF {
Expand All @@ -145,16 +161,5 @@ func (m *Migration) Buffer() error {
m.FinishedReading = time.Now()
m.BytesRead = n

// close bufferWriter so Buffer knows that there is no
// more data coming
if err := m.bufferWriter.Close(); err != nil {
return err
}

// it's safe to close the Body too
if err := m.Body.Close(); err != nil {
return err
}

return nil
}