func WithInstance(ctx context.Context, db *sql.DB) (*Postgres, error) {
	conn, err := db.Conn(ctx)
	if err != nil {
		return nil, err
	}

	p := Postgres{conn: conn}

	if err := conn.QueryRowContext(ctx, `SELECT CURRENT_DATABASE()`).Scan(&p.DatabaseName); err != nil {
		return nil, err
	}

	if len(p.DatabaseName) == 0 {
		return nil, ErrNoDatabaseName
	}

	if err := conn.QueryRowContext(ctx, `SELECT CURRENT_SCHEMA()`).Scan(&p.SchemaName); err != nil {
		return nil, err
	}

	if len(p.SchemaName) == 0 {
		return nil, ErrNoSchema
	}

	if len(p.EventStoreTable) == 0 {
		p.EventStoreTable = DefaultEventStoreTable
	}

	if err := p.ensureTable(ctx); err != nil {
		return nil, err
	}

	return &p, nil
}
// WithInstance creates a postgres data store with an existing db connection
WithInstance creates a postgres data store with an existing db connection
// WithInstance creates a postgres data store with an existing db connection
func WithInstance(ctx context.Context, db *sql.DB) (*Postgres, error) {
	conn, err := db.Conn(ctx)
	if err != nil {
		return nil, err
	}
	p := Postgres{conn: conn}
	if err := conn.QueryRowContext(ctx, `SELECT CURRENT_DATABASE()`).Scan(&p.DatabaseName); err != nil {
		return nil, err
	}
	if len(p.DatabaseName) == 0 {
		return nil, ErrNoDatabaseName
	}
	if err := conn.QueryRowContext(ctx, `SELECT CURRENT_SCHEMA()`).Scan(&p.SchemaName); err != nil {
		return nil, err
	}
	if len(p.SchemaName) == 0 {
		return nil, ErrNoSchema
	}
	if len(p.EventStoreTable) == 0 {
		p.EventStoreTable = DefaultEventStoreTable
	}
	if err := p.ensureTable(ctx); err != nil {
		return nil, err
	}
	return &p, nil
}
func WithInstance ( ctx context . Context , db * sql . DB ) ( * Postgres , error ) { conn , err := db . Conn ( ctx ) if err != nil { return nil , err } p := Postgres { conn : conn } if err := conn . QueryRowContext ( ctx , `SELECT CURRENT_DATABASE()` ) . Scan ( & p . DatabaseName ) ; err != nil { return nil , err } if len ( p . DatabaseName ) == 0 { return nil , ErrNoDatabaseName } if err := conn . QueryRowContext ( ctx , `SELECT CURRENT_SCHEMA()` ) . Scan ( & p . SchemaName ) ; err != nil { return nil , err } if len ( p . SchemaName ) == 0 { return nil , ErrNoSchema } if len ( p . EventStoreTable ) == 0 { p . EventStoreTable = DefaultEventStoreTable } if err := p . ensureTable ( ctx ) ; err != nil { return nil , err } return & p , nil }
WithInstance creates a postgres data store with an existing db connection
WithInstance creates a postgres data store with an existing db connection