Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: use reader instead of scanner to fix "bufio.Scanner: token too long" (#4323)

rebelice397f750e412a
diff --git a/plugin/db/pg/dump.go b/plugin/db/pg/dump.go
index 245d46c37b..1f4629a9c2 100644
--- a/plugin/db/pg/dump.go
+++ b/plugin/db/pg/dump.go
@@ -87,73 +87,88 @@ func (driver *Driver) dumpOneDatabaseWithPgDump(ctx context.Context, database st
return err
}
defer outPipe.Close()
- outScanner := bufio.NewScanner(outPipe)
+ outReader := bufio.NewReader(outPipe)
errPipe, err := cmd.StderrPipe()
if err != nil {
return err
}
defer errPipe.Close()
- errScanner := bufio.NewScanner(errPipe)
+ errReader := bufio.NewReader(errPipe)
if err := cmd.Start(); err != nil {
return err
}
previousLineComment := false
previousLineEmpty := false
- for outScanner.Scan() {
- line := outScanner.Text()
- // Skip "SET SESSION AUTHORIZATION" till we can support it.
- if strings.HasPrefix(line, "SET SESSION AUTHORIZATION ") {
- continue
+ for {
+ line, readErr := outReader.ReadString('\n')
+ if readErr != nil && readErr != io.EOF {
+ return readErr
}
- // Skip comment lines.
- if strings.HasPrefix(line, "--") {
- previousLineComment = true
- continue
- }
- if previousLineComment && line == "" {
+
+ if err := func() error {
+ // Skip "SET SESSION AUTHORIZATION" till we can support it.
+ if strings.HasPrefix(line, "SET SESSION AUTHORIZATION ") {
+ return nil
+ }
+ // Skip comment lines.
+ if strings.HasPrefix(line, "--") {
+ previousLineComment = true
+ return nil
+ }
+ if previousLineComment && line == "" {
+ previousLineComment = false
+ return nil
+ }
previousLineComment = false
- continue
- }
- previousLineComment = false
- // Skip extra empty lines.
- if line == "" {
- if previousLineEmpty {
- continue
+ // Skip extra empty lines.
+ if strings.TrimSpace(line) == "" {
+ if previousLineEmpty {
+ return nil
+ }
+ previousLineEmpty = true
+ } else {
+ previousLineEmpty = false
+ }
+
+ if _, err := io.WriteString(out, line); err != nil {
+ return err
}
- previousLineEmpty = true
- } else {
- previousLineEmpty = false
- }
- if _, err := io.WriteString(out, line); err != nil {
+ return nil
+ }(); err != nil {
return err
}
- if _, err := io.WriteString(out, "\n"); err != nil {
- return err
+
+ if readErr == io.EOF {
+ break
}
}
- if outScanner.Err() != nil {
- return outScanner.Err()
- }
var errBuilder strings.Builder
- for errScanner.Scan() {
- line := errScanner.Text()
- // Log the error, but return the first 1024 characters in the error to users.
- log.Warn(line)
- if errBuilder.Len() < 1024 {
- if _, err := errBuilder.WriteString(line); err != nil {
- return err
- }
- if _, err := errBuilder.WriteString("\n"); err != nil {
- return err
+ for {
+ line, readErr := errReader.ReadString('\n')
+ if readErr != nil && readErr != io.EOF {
+ return readErr
+ }
+
+ if err := func() error {
+ // Log the error, but return the first 1024 characters in the error to users.
+ log.Warn(line)
+ if errBuilder.Len() < 1024 {
+ if _, err := errBuilder.WriteString(line); err != nil {
+ return err
+ }
}
+ return nil
+ }(); err != nil {
+ return err
+ }
+
+ if readErr == io.EOF {
+ break
}
- }
- if errScanner.Err() != nil {
- return errScanner.Err()
}
err = cmd.Wait()
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 3595fa96d1..8d693f39cd 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -933,7 +933,8 @@ WHERE table_type = 'BASE TABLE'
a.Equal(`[["table_name"],["NAME"],[["projects"],["users"]]]`, result)
// Get migration history
- const initialSchema = `SET statement_timeout = 0;
+ const initialSchema = `
+SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
@@ -945,7 +946,8 @@ SET client_min_messages = warning;
SET row_security = off;
`
- const updatedSchema = `SET statement_timeout = 0;
+ const updatedSchema = `
+SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
@@ -1849,7 +1851,8 @@ func TestGetLatestSchema(t *testing.T) {
dbType: db.Postgres,
databaseName: "latestSchema",
ddl: `CREATE TABLE book(id INT, name TEXT);`,
- wantRawSchema: `SET statement_timeout = 0;
+ wantRawSchema: `
+SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';