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

pgx not following intended import behaviour #1035

Open
krishi-saripalli opened this issue Jan 23, 2024 · 1 comment
Open

pgx not following intended import behaviour #1035

krishi-saripalli opened this issue Jan 23, 2024 · 1 comment

Comments

@krishi-saripalli
Copy link

Describe the Bug
This is a follow up to issue #563

Despite following the guidlines about how to import drivers from database, I seem to be hitting the same problem with an unknown import error:

database driver: unknown driver pgx (forgotten import?)

I've also run the neccesary go get command with the pgx tag
go install -tags 'pgx' github.com/golang-migrate/migrate/v4/cmd/migrate@latest

"github.com/golang-migrate/migrate/v4"
	_ "github.com/golang-migrate/migrate/v4/database/pgx/v5"
	_ "github.com/golang-migrate/migrate/v4/source/file"
	
//more imports	
)


func main() {
	ctx := context.Background()
	port := ":8080" // Define the port as a variable for reuse
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("Failed to listen: %v", err)
	}
	//open db connection
	connStr := GetDbString()
	conn, err := pgx.Connect(ctx, connStr)
	if err != nil {
		log.Fatalf("Failed to connect to Postgres DB: %v", err)
	}
	log.Print("Successfully connected to %f", connStr)
	defer conn.Close(ctx)
	queries := db.New(conn)

	//Create server and register services
	grpcServer := grpc.NewServer()
	
	// Run migrations
	m, err := migrate.New(
		"file:///postgres/migrations",
		"pgx://test_db_user:test_db_password@db:5432/my_test_db")
	if err != nil {
		log.Fatalf("Migration creation failed: %v", err)  <------ failing here
	}
	if err := m.Up(); err != nil {
		if err != migrate.ErrNoChange {
			log.Fatalf("Migration failed: %v", err)
		}
	}
	log.Println("Migration completed successfully")

Steps to Reproduce
Steps to reproduce the behavior:

  1. My migrations look like '...'
  2. I ran migrate with the following options '....'
  3. See error

Expected Behavior
I would expect a succesful migration

Loaded Database Drivers
pgx

Go Version
go 1.21 linux/amd64

@lao-tseu-is-alive
Copy link

I did have the same issue...
I found that because of --> https://github.com/golang-migrate/migrate/blob/master/database/pgx/v5/pgx.go#L30

You need to use pgx5 in your dsn like this :

m, err := migrate.New(
		"file:///postgres/migrations",
		"pgx5://test_db_user:test_db_password@db:5432/my_test_db")

hope it can help you or someone else

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

No branches or pull requests

2 participants