golang-migrate / migrate

Database migrations. CLI and Golang library.
Other
14.63k stars 1.35k forks source link

pgx not following intended import behaviour #1035

Open krishi-saripalli opened 6 months ago

krishi-saripalli commented 6 months ago

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 commented 3 months ago

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