Merge pull request #365 from sosedoff/cockroachdb

CockroachDB initial fixes
This commit is contained in:
Dan Sosedoff 2018-06-05 22:59:02 -05:00 committed by GitHub
commit 2ce5e9a1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 166 additions and 6 deletions

View File

@ -29,6 +29,7 @@ test:
test-all: test-all:
@./script/test_all.sh @./script/test_all.sh
@./script/test_cockroach.sh
assets: static/ assets: static/
go-bindata -o pkg/data/bindata.go -pkg data $(BINDATA_OPTS) $(BINDATA_IGNORE) -ignore=[.]gitignore -ignore=[.]gitkeep $<... go-bindata -o pkg/data/bindata.go -pkg data $(BINDATA_OPTS) $(BINDATA_IGNORE) -ignore=[.]gitignore -ignore=[.]gitkeep $<...

43
data/roach.sql Normal file
View File

@ -0,0 +1,43 @@
DROP DATABASE IF EXISTS "roach";
CREATE DATABASE "roach";
USE "roach";
CREATE TABLE product_information (
product_id INT PRIMARY KEY NOT NULL,
product_name STRING(50) UNIQUE NOT NULL,
product_description STRING(2000),
category_id STRING(1) NOT NULL CHECK (category_id IN ('A','B','C')),
weight_class INT,
warranty_period INT CONSTRAINT valid_warranty CHECK (warranty_period BETWEEN 0 AND 24),
supplier_id INT,
product_status STRING(20),
list_price DECIMAL(8,2),
min_price DECIMAL(8,2),
catalog_url STRING(50) UNIQUE,
date_added DATE DEFAULT CURRENT_DATE(),
misc JSONB,
CONSTRAINT price_check CHECK (list_price >= min_price),
INDEX date_added_idx (date_added),
INDEX supp_id_prod_status_idx (supplier_id, product_status),
INVERTED INDEX details (misc)
);
INSERT INTO product_information VALUES
(1, 'Product A', 'Text', 'A', NULL, 1),
(2, 'Product B', 'Text', 'B', NULL, 2),
(3, 'Product C', 'Text', 'C', NULL, 3);
CREATE TABLE customers (
id INT PRIMARY KEY,
name STRING
);
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT REFERENCES customers(id) ON DELETE CASCADE
);
INSERT INTO customers VALUES (1, 'Lauren');
INSERT INTO orders VALUES (1,1);
DELETE FROM customers WHERE id = 1;
SELECT * FROM orders;

View File

@ -81,7 +81,7 @@ func initClient() {
} }
if !command.Opts.Sessions { if !command.Opts.Sessions {
fmt.Printf("Server runs PostgreSQL v%s\n", cl.ServerVersion()) fmt.Printf("Conneced to %s\n", cl.ServerVersion())
} }
fmt.Println("Checking database objects...") fmt.Println("Checking database objects...")

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
neturl "net/url" neturl "net/url"
"reflect" "reflect"
"regexp"
"strings" "strings"
"time" "time"
@ -17,10 +18,19 @@ import (
"github.com/sosedoff/pgweb/pkg/statements" "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 { type Client struct {
db *sqlx.DB db *sqlx.DB
tunnel *Tunnel tunnel *Tunnel
serverVersion string serverVersion string
serverType string
lastQueryTime time.Time lastQueryTime time.Time
External bool External bool
History []history.Record `json:"history"` 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) 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) db, err := sqlx.Open("postgres", url)
if err != nil { if err != nil {
return nil, err return nil, err
@ -132,7 +147,22 @@ func (client *Client) setServerVersion() {
} }
version := res.Rows[0][0].(string) 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 { func (client *Client) Test() error {
@ -203,6 +233,9 @@ func (client *Client) TableRowsCount(table string, opts RowsOptions) (*Result, e
} }
func (client *Client) TableInfo(table string) (*Result, error) { func (client *Client) TableInfo(table string) (*Result, error) {
if client.serverType == cockroachType {
return client.query(statements.TableInfoCockroach)
}
return client.query(statements.TableInfo, table) return client.query(statements.TableInfo, table)
} }
@ -230,9 +263,11 @@ func (client *Client) TableConstraints(table string) (*Result, error) {
// Returns all active queriers on the server // Returns all active queriers on the server
func (client *Client) Activity() (*Result, error) { func (client *Client) Activity() (*Result, error) {
chunks := strings.Split(client.serverVersion, ".") if client.serverType == cockroachType {
version := strings.Join(chunks[0:2], ".") return client.query("SHOW QUERIES")
}
version := getMajorMinorVersion(client.serverVersion)
query := statements.Activity[version] query := statements.Activity[version]
if query == "" { if query == "" {
query = statements.Activity["default"] query = statements.Activity["default"]
@ -267,7 +302,7 @@ func (client *Client) SetReadOnlyMode() error {
} }
func (client *Client) ServerVersion() string { 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) { func (client *Client) query(query string, args ...interface{}) (*Result, error) {

15
pkg/client/util.go Normal file
View 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], ".")
}

View File

@ -73,6 +73,13 @@ SELECT
pg_size_pretty(pg_total_relation_size($1)) AS total_size, pg_size_pretty(pg_total_relation_size($1)) AS total_size,
(SELECT reltuples FROM pg_class WHERE oid = $1::regclass) AS rows_count` (SELECT reltuples FROM pg_class WHERE oid = $1::regclass) AS rows_count`
TableInfoCockroach = `
SELECT
'n/a' AS data_size,
'n/a' AS index_size,
'n/a' AS total_size,
'n/a' AS rows_count`
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
TableSchema = ` TableSchema = `

View File

@ -30,4 +30,5 @@ do
sleep 5 sleep 5
make test make test
echo "-------------------------------- END TEST --------------------------------" echo "-------------------------------- END TEST --------------------------------"
done done

58
script/test_cockroach.sh Executable file
View File

@ -0,0 +1,58 @@
#!/bin/bash
set -e
function killproc() {
if [[ $(lsof -i tcp:8888) ]]; then
lsof -i tcp:8888 | grep pgweb | awk '{print $2}' | xargs kill
fi
}
# Nuke the old container if exists.
docker rm -f cockroach || true
# Start cockroach on 26258 so we dont mess with local server.
docker run \
--name=cockroach \
-d \
-t \
-p 26258:26257 \
cockroachdb/cockroach \
start --insecure
sleep 3
# Load the demo database.
docker exec -i cockroach ./cockroach sql --insecure < ./data/roach.sql
# Find and destroy the existing pgweb process.
# Would be great if pgweb had --pid option.
killproc
# Start pgweb and connect to cockroach.
make build
./pgweb \
--url=postgres://root@localhost:26258/roach?sslmode=disable \
--listen=8888 \
--skip-open &
sleep 1
# Run smoke tests
base="-w \"\n\" -f http://localhost:8888/api"
table="product_information"
curl $base/info
curl $base/connection
curl $base/schemas
curl $base/objects
curl $base/query -F query='select * from product_information;'
curl $base/tables/$table
curl $base/tables/$table/rows
curl $base/tables/$table/info
curl $base/tables/$table/indexes
curl $base/tables/$table/constraints
# Cleanup
killproc