Example #1
0
func TestOpenFromConnPool(t *testing.T) {
	connConfig := pgx.ConnConfig{
		Host:     "127.0.0.1",
		User:     "******",
		Password: "******",
		Database: "pgx_test",
	}

	config := pgx.ConnPoolConfig{ConnConfig: connConfig}
	pool, err := pgx.NewConnPool(config)
	if err != nil {
		t.Fatalf("Unable to create connection pool: %v", err)
	}
	defer pool.Close()

	db, err := stdlib.OpenFromConnPool(pool)
	if err != nil {
		t.Fatalf("Unable to create connection pool: %v", err)
	}
	defer closeDB(t, db)

	// Can get pgx.ConnPool from driver
	driver := db.Driver().(*stdlib.Driver)
	if driver.Pool == nil {
		t.Fatal("Expected driver opened through OpenFromConnPool to have Pool, but it did not")
	}

	// Normal sql/database still works
	var n int64
	err = db.QueryRow("select 1").Scan(&n)
	if err != nil {
		t.Fatalf("db.QueryRow unexpectedly failed: %v", err)
	}
}
Example #2
0
// Connect connects to the database using env vars.
// After connect, it creates tables if missing.
func (p *DB) Connect() (err error) {
	cfg, err := pgx.ParseEnvLibpq()
	if err != nil {
		return
	}

	pool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
		ConnConfig:     cfg,
		MaxConnections: 25,
	})
	if err != nil {
		return
	}

	c, err := pgx_stdlib.OpenFromConnPool(pool)
	if err != nil {
		return
	}

	p.conn, err = gorm.Open("postgres", c)
	if err != nil {
		return
	}

	d := p.conn.AutoMigrate(&secrets.Secret{}, &secrets.Key{})

	return d.Error
}
Example #3
0
File: db.go Project: jonasi/pg
func (d *DB) Open() error {
	conf := pgx.ConnPoolConfig{
		ConnConfig: pgx.ConnConfig{
			Host:     d.config.Host,
			Port:     uint16(d.config.Port),
			User:     d.config.Username,
			Password: d.config.Password,
			Database: d.config.Database,
			Logger:   d.config.Logger,
		},
		MaxConnections: d.config.MaxConnections,
	}

	p, err := pgx.NewConnPool(conf)

	if err != nil {
		return err
	}

	db, err := stdlib.OpenFromConnPool(p)

	if err != nil {
		return err
	}

	dbx := sqlx.NewDb(db, "pgx")
	d.queryer = queryer{impl: dbx}

	return nil
}
Example #4
0
func openPgxStdlib(config pgx.ConnPoolConfig) (*sql.DB, error) {
	connPool, err := pgx.NewConnPool(config)
	if err != nil {
		return nil, err
	}

	return stdlib.OpenFromConnPool(connPool)
}
Example #5
0
func TestConnQueryLog(t *testing.T) {
	logger := &testLogger{}

	connConfig := pgx.ConnConfig{
		Host:     "127.0.0.1",
		User:     "******",
		Password: "******",
		Database: "pgx_test",
		Logger:   logger,
	}

	config := pgx.ConnPoolConfig{ConnConfig: connConfig}
	pool, err := pgx.NewConnPool(config)
	if err != nil {
		t.Fatalf("Unable to create connection pool: %v", err)
	}
	defer pool.Close()

	db, err := stdlib.OpenFromConnPool(pool)
	if err != nil {
		t.Fatalf("Unable to create connection pool: %v", err)
	}
	defer closeDB(t, db)

	// clear logs from initial connection
	logger.logs = []testLog{}

	var n int64
	err = db.QueryRow("select 1").Scan(&n)
	if err != nil {
		t.Fatalf("db.QueryRow unexpectedly failed: %v", err)
	}

	l := logger.logs[0]
	if l.msg != "Query" {
		t.Errorf("Expected to log Query, but got %v", l)
	}

	if !(l.ctx[0] == "sql" && l.ctx[1] == "select 1") {
		t.Errorf("Expected to log Query with sql 'select 1', but got %v", l)
	}
}
Example #6
0
func getPgHandler(c *cli.Context) (*sql.DB, error) {
	var db *sql.DB
	config := pgx.ConnPoolConfig{
		ConnConfig: pgx.ConnConfig{
			Host:      c.String("host"),
			User:      c.String("user"),
			Password:  c.String("password"),
			Database:  c.String("database"),
			Port:      uint16(c.Int("port")),
			TLSConfig: nil,
		}}
	pool, err := pgx.NewConnPool(config)
	if err != nil {
		return db, err
	}
	db, err = stdlib.OpenFromConnPool(pool)
	if err != nil {
		return db, err
	}
	return db, nil
}