Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

panic: sql: Register called twice for driver postgres #322

Open
jlpellicer opened this issue Dec 14, 2017 · 1 comment
Open

panic: sql: Register called twice for driver postgres #322

jlpellicer opened this issue Dec 14, 2017 · 1 comment

Comments

@jlpellicer
Copy link

I've seen this question asked before, but I could not find a solution.

My code before the migration works just fine (DB Ready to Rock), but when i add the migrate packages and code it doesn't.

The error:

panic: sql: Register called twice for driver postgres

goroutine 1 [running]:
database/sql.Register(0x1457227, 0x8, 0x163c460, 0x1698f30)
	/usr/local/go/src/database/sql/sql.go:50 +0x1ad
github.com/lib/pq.init.0()
	/Users/transistor/Documents/Go/src/github.com/lib/pq/conn.go:49 +0x5c
github.com/lib/pq.init()
	<autogenerated>:1 +0x782
github.com/mattes/migrate/database/postgres.init()
	<autogenerated>:1 +0x64
asc-api-go/db1.init()
	<autogenerated>:1 +0x5d
asc-api-go/controllers/roles.init()
	<autogenerated>:1 +0x50
asc-api-go/ascMiddleware.init()
	<autogenerated>:1 +0x5a
asc-api-go/server.init()
	<autogenerated>:1 +0x44
main.init()

This is my db file:

package db1

import (
	"database/sql"
	"log"
	"os"
	_ "github.com/lib/pq"
	"github.com/mattes/migrate"
	"github.com/mattes/migrate/database/postgres"
	_ "github.com/mattes/migrate/source/file"
)

var Db *sql.DB

func Connect() {
	var error error
	Db1ConnectString := os.Getenv("DB1_CONNECT_STRING")
	if Db1ConnectString == "" {
		log.Fatal("$DB1_CONNECT_STRING must be set")
	}

	Db, error = sql.Open("postgres", Db1ConnectString)
	if error != nil {
		log.Fatal(error)
	}

	error = Db.Ping()
	if error != nil {
		log.Fatal(error)
	}

	log.Println("DB1 Ready to Rock 🤘")

	driver, err := postgres.WithInstance(Db, &postgres.Config{})
	if err != nil {
		log.Fatal(error)
	}
	log.Print(driver)

	m, err := migrate.NewWithDatabaseInstance(
		"file:///Users/transistor/Documents/Go/src/asc-api-go/db1/migrations", 
		"myDB", 
		driver)
	if err != nil {
		log.Fatal(error)
	}

	err = m.Up()
	if err != nil {
		log.Fatal(err)
	}
}

I also read that there might be a "confilct" while using vendoring (I'm using Godep), and I do in fact have two pq libraries, but I don't know if this is a problem and therefore no idea how to solve it:

/Users/transistor/Documents/Go/src/asc-api-go/vendor/github.com/lib/pq

/Users/transistor/Documents/Go/src/github.com/lib/pq

Can you help?
Thank you.

@eddieroger
Copy link

eddieroger commented Jan 20, 2018

I was having this problem, and like other assorted comments mention, it has to do with loading lib/pg twice. The way I fixed it was moving the migrate in to my vendor directory and out of $GOPATH/src/github.com/.... It worked after that. The moral of that story is that if you use vendor, everything should be there, don't use it, but some packages get confused when loaded in both places.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants