Skip to content
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

Postgres: Add a check to determine if table already exists to elide CREATE query #526

Merged
merged 5 commits into from
Mar 19, 2021

Conversation

testtest959
Copy link
Contributor

This PR adds a check in ensureVersionTable() in order to prevent a CREATE query which may fail for read only users. As a result, read only users will be able to query the schema version.

Closes #266

@coveralls
Copy link

coveralls commented Mar 16, 2021

Pull Request Test Coverage Report for Build 247

  • 20 of 24 (83.33%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 56.846%

Changes Missing Coverage Covered Lines Changed/Added Lines %
database/pgx/pgx.go 10 12 83.33%
database/postgres/postgres.go 10 12 83.33%
Totals Coverage Status
Change from base Build 226: 0.3%
Covered Lines: 3338
Relevant Lines: 5872

💛 - Coveralls

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and tests!!


// 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 {
Copy link
Member

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.

Copy link
Contributor Author

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


var e *database.Error
if !errors.As(err, &e) || err == nil {
t.Fatal("Unexpected error, want permission denied error. Got: ", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the permission denied error exposed? e.g. a postgres error code

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing my feedback. The test setup code looks much cleaner now!

Also, thanks for also updating the pgx driver!

t.Fatal("Unexpected error, want permission denied error. Got: ", err)
}

if strings.Contains(e.Err, "permission denied for schema barfoo") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be !strings.Contains()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, the assert was broken before then

t.Fatal("Unexpected error, want permission denied error. Got: ", err)
}

if strings.Contains(e.Err, "permission denied for schema barfoo") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto with !strings.Contains()

@dhui dhui merged commit 511ae9f into golang-migrate:master Mar 19, 2021
stephane-klein pushed a commit to spacefill/migrate that referenced this pull request Apr 4, 2021
…REATE query (golang-migrate#526)

* Squash commits

* Format

* Minor refactoring

* Address PR feedback; Add mustRun

* Fix a test assert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

postgres: ensureVersionTable() is a little more brittle with BDR than mattes/migrate
3 participants