func NewDB(ctx context.Context, t testing.TB) (*DB, error) {
	// if we find an environment variable use this inplace of the passed in DSN.
	// this will mostly be used in CI/CD settings to point to pipeline databases
	var dsn string
	dsnFromEnv := os.Getenv(EnvPGConnString)
	if dsnFromEnv != "" {
		dsn = dsnFromEnv
	} else {
		dsn = DefaultDSN
	}

	cfg, err := pgxpool.ParseConfig(dsn)
	if err != nil {
		return nil, err
	}
	cfg.ConnConfig.Logger = testingadapter.NewLogger(t)

	dbid, roleid := mkIDs()
	database := fmt.Sprintf("db%x", dbid)
	role := fmt.Sprintf("role%x", roleid)

	conn, err := pgx.ConnectConfig(ctx, cfg.ConnConfig)
	if err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, fmt.Sprintf(createRole, role)); err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, fmt.Sprintf(createDatabase, role, database)); err != nil {
		return nil, err
	}
	if err := conn.Close(ctx); err != nil {
		return nil, err
	}

	cfg.ConnConfig.Database = database
	conn, err = pgx.ConnectConfig(ctx, cfg.ConnConfig)
	if err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, loadUUID); err != nil {
		return nil, err
	}
	if err := conn.Close(ctx); err != nil {
		return nil, err
	}

	cfg.ConnConfig.User = role
	t.Logf("config: %+v", struct {
		Host     string
		Port     uint16
		Database string
		User     string
		Password string
	}{
		Host:     cfg.ConnConfig.Host,
		Port:     cfg.ConnConfig.Port,
		Database: cfg.ConnConfig.Database,
		User:     cfg.ConnConfig.User,
		Password: cfg.ConnConfig.Password,
	})
	cfg.ConnConfig.Logger = nil

	return &DB{
		dsn: dsn,
		cfg: cfg,
	}, nil
}
// NewDB generates a unique database instance for use in concurrent integration tests.
//
// An environment variable maybe use to provide the root connection string used to
// generate a test specific database.
//
// If no environment variable is specified the root connection string defaults to our local
// development db connection string DefaultDSN.
NewDB generates a unique database instance for use in concurrent integration tests.

An environment variable maybe use to provide the root connection string used to
generate a test specific database.

If no environment variable is specified the root connection string defaults to our local
development db connection string DefaultDSN.
// NewDB generates a unique database instance for use in concurrent integration tests . // // An environment variable maybe use to provide the root connection string used to // generate a test specific database . // // If no environment variable is specified the root connection string defaults to our local // development db connection string DefaultDSN .
func NewDB(ctx context.Context, t testing.TB) (*DB, error) {
	var dsn string
	dsnFromEnv := os.Getenv(EnvPGConnString)
	if dsnFromEnv != "" {
		dsn = dsnFromEnv
	} else {
		dsn = DefaultDSN
	}
	cfg, err := pgxpool.ParseConfig(dsn)
	if err != nil {
		return nil, err
	}
	cfg.ConnConfig.Logger = testingadapter.NewLogger(t)
	dbid, roleid := mkIDs()
	database := fmt.Sprintf("db%x", dbid)
	role := fmt.Sprintf("role%x", roleid)
	conn, err := pgx.ConnectConfig(ctx, cfg.ConnConfig)
	if err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, fmt.Sprintf(createRole, role)); err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, fmt.Sprintf(createDatabase, role, database)); err != nil {
		return nil, err
	}
	if err := conn.Close(ctx); err != nil {
		return nil, err
	}
	cfg.ConnConfig.Database = database
	conn, err = pgx.ConnectConfig(ctx, cfg.ConnConfig)
	if err != nil {
		return nil, err
	}
	if _, err := conn.Exec(ctx, loadUUID); err != nil {
		return nil, err
	}
	if err := conn.Close(ctx); err != nil {
		return nil, err
	}
	cfg.ConnConfig.User = role
	t.Logf("config: %+v", struct {
		Host     string
		Port     uint16
		Database string
		User     string
		Password string
	}{
		Host:     cfg.ConnConfig.Host,
		Port:     cfg.ConnConfig.Port,
		Database: cfg.ConnConfig.Database,
		User:     cfg.ConnConfig.User,
		Password: cfg.ConnConfig.Password,
	})
	cfg.ConnConfig.Logger = nil
	return &DB{
		dsn: dsn,
		cfg: cfg,
	}, nil
}
func NewDB ( ctx context . Context , t testing . TB ) ( * DB , error ) { var dsn string dsnFromEnv := os . Getenv ( EnvPGConnString ) if dsnFromEnv != " " { dsn = dsnFromEnv } else { dsn = DefaultDSN } cfg , err := pgxpool . ParseConfig ( dsn ) if err != nil { return nil , err } cfg . ConnConfig . Logger = testingadapter . NewLogger ( t ) dbid , roleid := mkIDs ( ) database := fmt . Sprintf ( " " , dbid ) role := fmt . Sprintf ( " " , roleid ) conn , err := pgx . ConnectConfig ( ctx , cfg . ConnConfig ) if err != nil { return nil , err } if _ , err := conn . Exec ( ctx , fmt . Sprintf ( createRole , role ) ) ; err != nil { return nil , err } if _ , err := conn . Exec ( ctx , fmt . Sprintf ( createDatabase , role , database ) ) ; err != nil { return nil , err } if err := conn . Close ( ctx ) ; err != nil { return nil , err } cfg . ConnConfig . Database = database conn , err = pgx . ConnectConfig ( ctx , cfg . ConnConfig ) if err != nil { return nil , err } if _ , err := conn . Exec ( ctx , loadUUID ) ; err != nil { return nil , err } if err := conn . Close ( ctx ) ; err != nil { return nil , err } cfg . ConnConfig . User = role t . Logf ( " " , struct { Host string Port uint16 Database string User string Password string } { Host : cfg . ConnConfig . Host , Port : cfg . ConnConfig . Port , Database : cfg . ConnConfig . Database , User : cfg . ConnConfig . User , Password : cfg . ConnConfig . Password , } ) cfg . ConnConfig . Logger = nil return & DB { dsn : dsn , cfg : cfg , } , nil }
NewDB generates a unique database instance for use in concurrent integration tests.
NewDB generates a unique database instance for use in concurrent integration tests .
// if we find an environment variable use this inplace of the passed in DSN. // this will mostly be used in CI/CD settings to point to pipeline databases