-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Postgres: Add a check to determine if table already exists to elide CREATE query #526
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
dhui
merged 5 commits into
golang-migrate:master
from
testtest959:modify-ensure-version-table
Mar 19, 2021
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
Squash commits
- Loading branch information
commit 177d366f8123e58a9b0f024fe5306ef1ebc4137f
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
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 |
---|---|---|
|
@@ -6,11 +6,11 @@ import ( | |
"context" | ||
"database/sql" | ||
sqldriver "database/sql/driver" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"io" | ||
"log" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
@@ -310,6 +310,159 @@ func TestWithSchema(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestFailToCreateTableWithoutPermissions(t *testing.T) { | ||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.FirstPort() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
addr := pgConnectionString(ip, port) | ||
|
||
// Check that opening the postgres connection returns NilVersion | ||
p := &Postgres{} | ||
|
||
d, err := p.Open(addr) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer func() { | ||
if err := d.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
// create user who is not the owner. Although we're concatenating strings in an sql statement it should be fine | ||
// since this is a test environment and we're not expecting to the pgPassword to be malicious | ||
if err := d.Run(strings.NewReader("CREATE USER not_owner WITH ENCRYPTED PASSWORD '" + pgPassword + "'")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// create foobar schema | ||
if err := d.Run(strings.NewReader("CREATE SCHEMA barfoo AUTHORIZATION postgres")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// revoke privileges | ||
if err := d.Run(strings.NewReader("GRANT USAGE ON SCHEMA barfoo TO not_owner")); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := d.Run(strings.NewReader("REVOKE CREATE ON SCHEMA barfoo FROM PUBLIC")); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := d.Run(strings.NewReader("REVOKE CREATE ON SCHEMA barfoo FROM not_owner")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// re-connect using that schema | ||
d2, err := p.Open(fmt.Sprintf("postgres://not_owner:%s@%v:%v/postgres?sslmode=disable&search_path=barfoo", | ||
pgPassword, ip, port)) | ||
|
||
defer func() { | ||
if d2 == nil { | ||
return | ||
} | ||
if err := d2.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
var e *database.Error | ||
if !errors.As(err, &e) || err == nil { | ||
t.Fatal("Unexpected error, want permission denied error. Got: ", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the permission denied error exposed? e.g. a postgres error code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, but we have to parse the error string to do it. Made the change! |
||
} | ||
}) | ||
} | ||
|
||
func TestCheckBeforeCreateTable(t *testing.T) { | ||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.FirstPort() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
addr := pgConnectionString(ip, port) | ||
|
||
// Check that opening the postgres connection returns NilVersion | ||
p := &Postgres{} | ||
|
||
d, err := p.Open(addr) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
defer func() { | ||
if err := d.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
// create user who is not the owner. Although we're concatenating strings in an sql statement it should be fine | ||
// since this is a test environment and we're not expecting to the pgPassword to be malicious | ||
if err := d.Run(strings.NewReader("CREATE USER not_owner WITH ENCRYPTED PASSWORD '" + pgPassword + "'")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// create foobar schema | ||
if err := d.Run(strings.NewReader("CREATE SCHEMA barfoo AUTHORIZATION postgres")); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := d.Run(strings.NewReader("GRANT USAGE ON SCHEMA barfoo TO not_owner")); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := d.Run(strings.NewReader("GRANT CREATE ON SCHEMA barfoo TO not_owner")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// re-connect using that schema | ||
d2, err := p.Open(fmt.Sprintf("postgres://not_owner:%s@%v:%v/postgres?sslmode=disable&search_path=barfoo", | ||
pgPassword, ip, port)) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := d2.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// revoke privileges | ||
if err := d.Run(strings.NewReader("REVOKE CREATE ON SCHEMA barfoo FROM PUBLIC")); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := d.Run(strings.NewReader("REVOKE CREATE ON SCHEMA barfoo FROM not_owner")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// re-connect using that schema | ||
d3, err := p.Open(fmt.Sprintf("postgres://not_owner:%s@%v:%v/postgres?sslmode=disable&search_path=barfoo", | ||
pgPassword, ip, port)) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
version, _, err := d3.Version() | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if version != database.NilVersion { | ||
t.Fatal("Unexpected version, want database.NilVersion. Got: ", version) | ||
} | ||
|
||
defer func() { | ||
if err := d3.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
}) | ||
} | ||
|
||
func TestParallelSchema(t *testing.T) { | ||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.FirstPort() | ||
|
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.
Looks like there are a bunch of setup statements for these tests. Create a
mustRun(testing.T*, []string)
method and use that.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.
Awesome! That makes this a lot cleaner