Merge remote-tracking branch 'upstream/master' into itn/tables-quotes-2
# Conflicts: # pkg/client/client.go
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
neturl "net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -17,10 +18,19 @@ import (
|
||||
"github.com/sosedoff/pgweb/pkg/statements"
|
||||
)
|
||||
|
||||
var (
|
||||
postgresSignature = regexp.MustCompile(`(?i)postgresql ([\d\.]+)\s`)
|
||||
postgresType = "PostgreSQL"
|
||||
|
||||
cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s`)
|
||||
cockroachType = "CockroachDB"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
db *sqlx.DB
|
||||
tunnel *Tunnel
|
||||
serverVersion string
|
||||
serverType string
|
||||
lastQueryTime time.Time
|
||||
External bool
|
||||
History []history.Record `json:"history"`
|
||||
@@ -109,6 +119,11 @@ func NewFromUrl(url string, sshInfo *shared.SSHInfo) (*Client, error) {
|
||||
fmt.Println("Creating a new client for:", url)
|
||||
}
|
||||
|
||||
uri, err := neturl.Parse(url)
|
||||
if err == nil && uri.Path == "" {
|
||||
return nil, fmt.Errorf("Database name is not provided")
|
||||
}
|
||||
|
||||
db, err := sqlx.Open("postgres", url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -132,7 +147,22 @@ func (client *Client) setServerVersion() {
|
||||
}
|
||||
|
||||
version := res.Rows[0][0].(string)
|
||||
client.serverVersion = strings.Split(version, " ")[1]
|
||||
|
||||
// Detect postgresql
|
||||
matches := postgresSignature.FindAllStringSubmatch(version, 1)
|
||||
if len(matches) > 0 {
|
||||
client.serverType = postgresType
|
||||
client.serverVersion = matches[0][1]
|
||||
return
|
||||
}
|
||||
|
||||
// Detect cockroachdb
|
||||
matches = cockroachSignature.FindAllStringSubmatch(version, 1)
|
||||
if len(matches) > 0 {
|
||||
client.serverType = cockroachType
|
||||
client.serverVersion = matches[0][1]
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) Test() error {
|
||||
@@ -191,18 +221,45 @@ func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error)
|
||||
return client.query(sql)
|
||||
}
|
||||
|
||||
func (client *Client) EstimatedTableRowsCount(table string, opts RowsOptions) (*Result, error) {
|
||||
schema, table := getSchemaAndTable(table)
|
||||
sql := fmt.Sprintf(`SELECT reltuples FROM pg_class WHERE oid = '%s.%s'::regclass;`, schema, table)
|
||||
|
||||
result, err := client.query(sql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// float64 to int64 conversion
|
||||
estimatedRowsCount := result.Rows[0][0].(float64)
|
||||
result.Rows[0] = Row{int64(estimatedRowsCount)}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (client *Client) TableRowsCount(table string, opts RowsOptions) (*Result, error) {
|
||||
schema, table := getSchemaAndTable(table)
|
||||
sql := fmt.Sprintf(`SELECT COUNT(1) FROM "%s"."%s"`, schema, table)
|
||||
|
||||
if opts.Where != "" {
|
||||
sql += fmt.Sprintf(" WHERE %s", opts.Where)
|
||||
} else if client.serverType == postgresType {
|
||||
tableInfo, err := client.TableInfo(table)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
estimatedRowsCount := tableInfo.Rows[0][3].(float64)
|
||||
if estimatedRowsCount > 100000 {
|
||||
return client.EstimatedTableRowsCount(table, opts)
|
||||
}
|
||||
}
|
||||
|
||||
return client.query(sql)
|
||||
}
|
||||
|
||||
func (client *Client) TableInfo(table string) (*Result, error) {
|
||||
if client.serverType == cockroachType {
|
||||
return client.query(statements.TableInfoCockroach)
|
||||
}
|
||||
schema, table := getSchemaAndTable(table)
|
||||
return client.query(statements.TableInfo, fmt.Sprintf(`"%s"."%s"`, schema, table))
|
||||
}
|
||||
@@ -231,9 +288,11 @@ func (client *Client) TableConstraints(table string) (*Result, error) {
|
||||
|
||||
// Returns all active queriers on the server
|
||||
func (client *Client) Activity() (*Result, error) {
|
||||
chunks := strings.Split(client.serverVersion, ".")
|
||||
version := strings.Join(chunks[0:2], ".")
|
||||
if client.serverType == cockroachType {
|
||||
return client.query("SHOW QUERIES")
|
||||
}
|
||||
|
||||
version := getMajorMinorVersion(client.serverVersion)
|
||||
query := statements.Activity[version]
|
||||
if query == "" {
|
||||
query = statements.Activity["default"]
|
||||
@@ -268,7 +327,7 @@ func (client *Client) SetReadOnlyMode() error {
|
||||
}
|
||||
|
||||
func (client *Client) ServerVersion() string {
|
||||
return client.serverVersion
|
||||
return fmt.Sprintf("%s %s", client.serverType, client.serverVersion)
|
||||
}
|
||||
|
||||
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
|
||||
|
||||
@@ -278,6 +278,35 @@ func test_TableInfo(t *testing.T) {
|
||||
assert.Equal(t, 1, len(res.Rows))
|
||||
}
|
||||
|
||||
func test_EstimatedTableRowsCount(t *testing.T) {
|
||||
var count int64 = 15
|
||||
res, err := testClient.EstimatedTableRowsCount("books", RowsOptions{})
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, []string{"reltuples"}, res.Columns)
|
||||
assert.Equal(t, []Row{Row{count}}, res.Rows)
|
||||
}
|
||||
|
||||
func test_TableRowsCount(t *testing.T) {
|
||||
var count int64 = 15
|
||||
res, err := testClient.TableRowsCount("books", RowsOptions{})
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, []string{"count"}, res.Columns)
|
||||
assert.Equal(t, []Row{Row{count}}, res.Rows)
|
||||
}
|
||||
|
||||
func test_TableRowsCountWithLargeTable(t *testing.T) {
|
||||
var count int64 = 100010
|
||||
testClient.db.MustExec(`create table large_table as select s from generate_Series(1,100010) s;`)
|
||||
testClient.db.MustExec(`VACUUM large_table;`)
|
||||
res, err := testClient.TableRowsCount("large_table", RowsOptions{})
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, []string{"reltuples"}, res.Columns)
|
||||
assert.Equal(t, []Row{Row{count}}, res.Rows)
|
||||
}
|
||||
|
||||
func test_TableIndexes(t *testing.T) {
|
||||
res, err := testClient.TableIndexes("books")
|
||||
|
||||
@@ -400,6 +429,9 @@ func TestAll(t *testing.T) {
|
||||
test_Table(t)
|
||||
test_TableRows(t)
|
||||
test_TableInfo(t)
|
||||
test_EstimatedTableRowsCount(t)
|
||||
test_TableRowsCount(t)
|
||||
test_TableRowsCountWithLargeTable(t)
|
||||
test_TableIndexes(t)
|
||||
test_TableConstraints(t)
|
||||
test_Query(t)
|
||||
|
||||
@@ -11,6 +11,11 @@ type Dump struct {
|
||||
Table string
|
||||
}
|
||||
|
||||
func (d *Dump) CanExport() bool {
|
||||
err := exec.Command("pg_dump", "--version").Run()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (d *Dump) Export(url string, writer io.Writer) error {
|
||||
errOutput := bytes.NewBuffer(nil)
|
||||
|
||||
|
||||
@@ -24,8 +24,12 @@ func test_DumpExport(t *testing.T) {
|
||||
os.Remove(savePath)
|
||||
}()
|
||||
|
||||
// Test full db dump
|
||||
dump := Dump{}
|
||||
|
||||
// Test for pg_dump presence
|
||||
assert.True(t, dump.CanExport())
|
||||
|
||||
// Test full db dump
|
||||
err = dump.Export(url, saveFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
||||
15
pkg/client/util.go
Normal file
15
pkg/client/util.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Get short version from the string
|
||||
// Example: 10.2.3.1 -> 10.2
|
||||
func getMajorMinorVersion(str string) string {
|
||||
chunks := strings.Split(str, ".")
|
||||
if len(chunks) == 0 {
|
||||
return str
|
||||
}
|
||||
return strings.Join(chunks[0:2], ".")
|
||||
}
|
||||
Reference in New Issue
Block a user