-
Notifications
You must be signed in to change notification settings - Fork 70
snapshot: trim extension statements from pgdump when uploading #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package pgdump | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "io" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
| ) | ||
|
|
||
| // PartialCopyWithoutExtensions will perform a partial copy of a SQL database dump from | ||
| // src to dst while commenting out EXTENSIONs-related statements. When it determines there | ||
| // are no more EXTENSIONs-related statements, it will return, resetting src to the position | ||
| // of the last contents written to dst. | ||
| // | ||
| // This is needed for import to Google Cloud Storage, which does not like many EXTENSION | ||
| // statements. For more details, see https://cloud.google.com/sql/docs/postgres/import-export/import-export-dmp | ||
| // | ||
| // Filtering requires reading entire lines into memory - this can be a very expensive | ||
| // operation, so when filtering is complete the more efficient io.Copy should be used | ||
| // to perform the remainder of the copy from src to dst. | ||
| func PartialCopyWithoutExtensions(dst io.Writer, src io.ReadSeeker, progressFn func(int64)) (int64, error) { | ||
| var ( | ||
| reader = bufio.NewReader(src) | ||
| // position we have consumed up to, track separately because bufio.Reader may have | ||
| // read ahead on src. This allows us to reset src later. | ||
| consumed int64 | ||
| // number of bytes we have actually written to dst - it should always be returned. | ||
| written int64 | ||
| // set to true when we have done all our filtering | ||
| noMoreExtensions bool | ||
| ) | ||
|
|
||
| for !noMoreExtensions { | ||
| // Read up to a line, keeping track of our position in src | ||
| line, err := reader.ReadBytes('\n') | ||
| consumed += int64(len(line)) | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
|
|
||
| // Once we start seeing table creations, we are definitely done with extensions, | ||
| // so we can hand off the rest to the superior io.Copy implementation. | ||
| if bytes.HasPrefix(line, []byte("CREATE TABLE")) { | ||
| // we are done with extensions | ||
| noMoreExtensions = true | ||
| } else if bytes.HasPrefix(line, []byte("COMMENT ON EXTENSION")) { | ||
| // comment out this line | ||
| line = append([]byte("-- "), line...) | ||
| } | ||
|
|
||
| // Write this line and update our progress before returning on error | ||
| lineWritten, err := dst.Write(line) | ||
| written += int64(lineWritten) | ||
| progressFn(written) | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| // No more extensions - reset src to the last actual consumed position | ||
| _, err := src.Seek(consumed, io.SeekStart) | ||
| if err != nil { | ||
| return written, errors.Wrap(err, "reset src position") | ||
| } | ||
| return written, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package pgdump | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/hexops/autogold" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPartialCopyWithoutExtensions(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("Test doesn't work on Windows of weirdness with t.TempDir() handling") | ||
| } | ||
|
|
||
| // Create test data - there is no stdlib in-memory io.ReadSeeker implementation | ||
| src, err := os.Create(filepath.Join(t.TempDir(), t.Name())) | ||
| require.NoError(t, err) | ||
| _, err = src.WriteString(`-- Some comment | ||
|
|
||
| CREATE EXTENSION foobar | ||
|
|
||
| COMMENT ON EXTENSION barbaz | ||
|
|
||
| CREATE TYPE asdf | ||
|
|
||
| CREATE TABLE robert ( | ||
| ... | ||
| ) | ||
|
|
||
| CREATE TABLE bobhead ( | ||
| ... | ||
| )`) | ||
| require.NoError(t, err) | ||
| _, err = src.Seek(0, io.SeekStart) | ||
| require.NoError(t, err) | ||
|
|
||
| // Set up target to assert against | ||
| var dst bytes.Buffer | ||
|
|
||
| // Perform partial copy | ||
| _, err = PartialCopyWithoutExtensions(&dst, src, func(i int64) {}) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Copy rest of contents | ||
| _, err = io.Copy(&dst, src) | ||
| assert.NoError(t, err) | ||
|
|
||
| // Assert contents (update with -update) | ||
| autogold.Want("partial-copy-without-extensions", `-- Some comment | ||
|
|
||
| CREATE EXTENSION foobar | ||
|
|
||
| -- COMMENT ON EXTENSION barbaz | ||
|
|
||
| CREATE TYPE asdf | ||
|
|
||
| CREATE TABLE robert ( | ||
| ... | ||
| ) | ||
|
|
||
| CREATE TABLE bobhead ( | ||
| ... | ||
| )`).Equal(t, dst.String()) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure about this? I'm lacking some context if this is being used by end users or not but this is a large assumption.
The official postgres docs use things like
splithttps://www.postgresql.org/docs/12/backup-dump.html#BACKUP-DUMP-LARGEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're referring to table creations being before extensions, then yes - extensions are typically a prerequisite
As for handing off to io.Copy, yes - the io.Copy implementation is more robust and efficient, and the GCS bucket handler will handle chunking for us. I don't think Cloud SQL will be happy with split dumps, and to piece it together we need to download it somewhere, which I'd like to avoid (right now the process can just have us import directly from GCS)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can revisit things like
splitif size becomes a blocker, but I think the streaming nature of io.Copy should mitigate the effects of size