storage icon indicating copy to clipboard operation
storage copied to clipboard

Updated Postgre module to use pgx instead of pq

Open Technerder opened this issue 2 years ago • 28 comments

Removed Postgre Close test since the pgx implementation of Close() doesn't return an error, Removed Storage.Close()'s return method since the pgx implementation of Close() doesn't return an error, Removed Config.maxIdleConns since pgx implementation doesn't support setting a maximum number of idle connections

Technerder avatar Jul 02 '21 22:07 Technerder

Thanks for the PR! There are quiet a few breaking changes here, so we'll need to release a new major version of the postgres implementation.

hi019 avatar Jul 05 '21 02:07 hi019

Is there anything I could change or do to help?

Technerder avatar Jul 05 '21 03:07 Technerder

@technerder

I asked the developer of pgx as to whether or not there was a way to find out if pgxpool.Pool.Close() fails and they said that it simply blocks until it succeeds like I originally thought. I only removed the Test_Postgres_Close test because I thought it was redundant since the close method can't seem to fail. I also only modified the signature for the storage's Close method for the same reason

please add the test again to test the error free process

I'd be more than happy to change the signature back and make it return nil for the error but I'm not sure if that'd be considered best practice.

https://github.com/gofiber/fiber/blob/master/app.go#L66 we have several storages, it is important that they all use the same interface, otherwise you cannot use them as intended, please just change the return type

ReneWerner87 avatar Jul 09 '21 06:07 ReneWerner87

we have several storages, it is important that they all use the same interface, otherwise you cannot use them as intended, please just change the return type

So I assume adding something like the following would be frowned down upon unless I also added it to all other storage implementations?

func (s *Storage) DB() *pgxpool.Pool {
	return s.db
}

Technerder avatar Jul 09 '21 17:07 Technerder

No would be okay

ReneWerner87 avatar Jul 09 '21 19:07 ReneWerner87

Thanks for all the work you've done here. If we're going to release a new major version, what would you think of rewriting the config to just accept a pgx connection, as you wrote in your issue? We would be able to remove a lot of code.

hi019 avatar Jul 11 '21 23:07 hi019

I'll start working on changing the module to accept a pgx object instead.

Technerder avatar Jul 12 '21 00:07 Technerder

Thanks for all the work you've done here. If we're going to release a new major version, what would you think of rewriting the config to just accept a pgx connection, as you wrote in your issue? We would be able to remove a lot of code.

Should I remove the postgres config struct altogether? I think it would make sense to do so.

Technerder avatar Jul 12 '21 00:07 Technerder

Yeah, we should remove the config keys that correspond to the connection configuration.

hi019 avatar Jul 12 '21 00:07 hi019

But before you make changes, @ReneWerner87 what do you think of the idea?

hi019 avatar Jul 12 '21 00:07 hi019

passing the connection would be okay for me

ReneWerner87 avatar Jul 12 '21 06:07 ReneWerner87

I'll start implementing this right now. I'm thinking of modifying the Config to look somewhat like this

// Config defines the config for storage.
type Config struct {
	// DBPool pgxpool.Pool object
	//
	// Required
	DBPool pgxpool.Pool

	// Table name
	//
	// Optional. Default is "fiber_storage"
	Table string

	// Reset clears any existing keys in existing Table
	//
	// Optional. Default is false
	Reset bool

	// Time before deleting expired keys
	//
	// Optional. Default is 10 * time.Second
	GCInterval time.Duration
}

But I'm unsure of how to go about modifying the defaultConfig, should it just panic out if no DBPool object is passed in or should I remove the defaultConfig altogether?

Technerder avatar Jul 12 '21 21:07 Technerder

@Technerder Yes, panicking should be fine here

hi019 avatar Jul 15 '21 15:07 hi019

Latest commit should allow people to directly pass in a pgxpool.Pool object as opposed to the database host, port, etc

Technerder avatar Jul 15 '21 23:07 Technerder

@Technerder please check image and image

ReneWerner87 avatar Jul 16 '21 18:07 ReneWerner87

@ReneWerner87 I asked the developer of pgx about the dependency (related issue) and this is what they told me

My first though was: why does a database driver have a dependency on a JWT library? As it turns out, there isn't any production dependency. It is there because of how modules include all the dependencies including tests in go.sum. It appears it is coming from go-kit. 48f3934 trimmed the go-kit dependency so I think this pseudo-dependency should drop entirely at some point.

Its only being used for some test, not in the actual library code.

Technerder avatar Jul 18 '21 16:07 Technerder

@Technerder image

and the snyk check failed image

ReneWerner87 avatar Jul 26 '21 06:07 ReneWerner87

One more thing, can you put the new code in a v2 folder (so the path will be postgres/v2/)? That way we can issue a new major version with a /v2 import

hi019 avatar Jul 26 '21 13:07 hi019

@Technerder image

and the snyk check failed image

Would you be able to confirm if this is related to CVE-2021-3538?

Technerder avatar Jul 26 '21 19:07 Technerder

One more thing, can you put the new code in a v2 folder (so the path will be postgres/v2/)? That way we can issue a new major version with a /v2 import

Should I keep the v1 code there in the base folder?

Technerder avatar Jul 26 '21 19:07 Technerder

Yep

hi019 avatar Jul 27 '21 00:07 hi019

Verification for "Tech hashtag 0067" in joeyxyz's dms

Technerder avatar Dec 09 '21 05:12 Technerder

Hey folks, checking back on this PR. PQ is in maintenance mode now and its maintainers even recommend pgx. https://github.com/lib/pq#status

If this PR is ready, it will be good to merge it as soon as possible, so that we can start using a postgres driver which is in active development.

rishavs avatar Aug 08 '22 20:08 rishavs

@Technerder pgx v5 seems to be ready for production. Can you test it? The problem should be fixed

efectn avatar Oct 08 '22 07:10 efectn

@efectn I can test the code usage and functionality by tomorrow but I believe I lack the perms to run a Snyk security check/audit (unless I'm mistaken) and had to ask @ReneWerner87 the last time.

Technerder avatar Oct 09 '22 21:10 Technerder

Screenshot_20221010-002347_Chrome Screenshot_20221010-002326_Chrome

Satori uuid seems to be removed. Maybe we need to open an issue for these alerts @Technerder

efectn avatar Oct 09 '22 21:10 efectn

@efectn would you be able to provide a link to that page? I'm also a little confused about why yaml.v3 is being flagged since the go.mod on the main pgx repo shows the current version being used to be v3.0.1 as shown here which should remove the vulnerability according to the screenshot.

Technerder avatar Oct 10 '22 05:10 Technerder

@efectn would you be able to provide a link to that page? I'm also a little confused about why yaml.v3 is being flagged since the go.mod on the main pgx repo shows the current version being used to be v3.0.1 as shown here which should remove the vulnerability according to the screenshot.

🤔🤔

efectn avatar Oct 10 '22 06:10 efectn

Created an issue --> https://github.com/jackc/pgx/issues/1352

efectn avatar Oct 25 '22 17:10 efectn

From a dependancy CVE standpoint I think we should move forward with this PR review.

Per my comments here https://github.com/jackc/pgx/issues/1352 Snyk go.mod scanning issues are false positives. Checked with govulncheck.

~/Documents/GitHub/storage/postgres/v2 (pr/153) » govulncheck .                                                                                            1 ↵ sixcolors@Jasons-MacBook-Pro
govulncheck is an experimental tool. Share feedback at https://go.dev/s/govulncheck-feedback.

Scanning for dependencies with known vulnerabilities...
No vulnerabilities found.

And checking which packages are acutally used in v2

~/Documents/GitHub/storage/postgres/v2 (pr/153) » go list -deps -f '{{define "M"}}{{.Path}}@{{.Version}}{{end}}{{with .Module}}{{if not .Main}}{{if .Replace}}{{template "M" .Replace}}{{else}}{{template "M" .}}{{end}}{{end}}{{end}}' | sort -u
github.com/jackc/[email protected]
github.com/jackc/[email protected]
github.com/jackc/pgx/[email protected]
github.com/jackc/puddle/[email protected]
golang.org/x/[email protected]
golang.org/x/[email protected]

sixcolors avatar Nov 03 '22 22:11 sixcolors