Merge with master

This commit is contained in:
Dan Sosedoff
2016-01-17 15:22:33 -06:00
10 changed files with 187 additions and 58 deletions

View File

@@ -158,7 +158,15 @@ func GetSchemas(c *gin.Context) {
}
func GetTable(c *gin.Context) {
res, err := DB(c).Table(c.Params.ByName("table"))
var res *client.Result
var err error
if c.Request.FormValue("type") == "materialized_view" {
res, err = DB(c).MaterializedView(c.Params.ByName("table"))
} else {
res, err = DB(c).Table(c.Params.ByName("table"))
}
serveResult(res, err, c)
}

View File

@@ -141,6 +141,10 @@ func (client *Client) Table(table string) (*Result, error) {
return client.query(statements.PG_TABLE_SCHEMA, schema, table)
}
func (client *Client) MaterializedView(name string) (*Result, error) {
return client.query(statements.PG_MATERIALIZED_VIEW_SCHEMA, name)
}
func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) {
schema, table := getSchemaAndTable(table)
sql := fmt.Sprintf(`SELECT * FROM "%s"."%s"`, schema, table)
@@ -235,7 +239,10 @@ func (client *Client) query(query string, args ...interface{}) (*Result, error)
return nil, err
}
result := Result{Columns: cols}
result := Result{
Columns: cols,
Rows: []Row{},
}
for rows.Next() {
obj, err := rows.SliceScan()

View File

@@ -11,8 +11,13 @@ import (
)
var (
testClient *Client
testCommands map[string]string
testClient *Client
testCommands map[string]string
serverHost string
serverPort string
serverUser string
serverPassword string
serverDatabase string
)
func mapKeys(data map[string]*Objects) []string {
@@ -23,6 +28,28 @@ func mapKeys(data map[string]*Objects) []string {
return result
}
func pgVersion() (int, int) {
var major, minor int
fmt.Sscanf(os.Getenv("PGVERSION"), "%d.%d", &major, &minor)
return major, minor
}
func getVar(name, def string) string {
val := os.Getenv(name)
if val == "" {
return def
}
return val
}
func initVars() {
serverHost = getVar("PGHOST", "localhost")
serverPort = getVar("PGPORT", "5432")
serverUser = getVar("PGUSER", "postgres")
serverPassword = getVar("PGPASSWORD", "postgres")
serverDatabase = getVar("PGDATABASE", "booktown")
}
func setupCommands() {
testCommands = map[string]string{
"createdb": "createdb",
@@ -42,7 +69,13 @@ func onWindows() bool {
}
func setup() {
out, err := exec.Command(testCommands["createdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput()
out, err := exec.Command(
testCommands["createdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
if err != nil {
fmt.Println("Database creation failed:", string(out))
@@ -50,7 +83,14 @@ func setup() {
os.Exit(1)
}
out, err = exec.Command(testCommands["psql"], "-U", "postgres", "-h", "localhost", "-f", "../../data/booktown.sql", "booktown").CombinedOutput()
out, err = exec.Command(
testCommands["psql"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
"-f", "../../data/booktown.sql",
serverDatabase,
).CombinedOutput()
if err != nil {
fmt.Println("Database import failed:", string(out))
@@ -60,7 +100,8 @@ func setup() {
}
func setupClient() {
testClient, _ = NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable", nil)
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
testClient, _ = NewFromUrl(url, nil)
}
func teardownClient() {
@@ -70,7 +111,13 @@ func teardownClient() {
}
func teardown() {
_, err := exec.Command(testCommands["dropdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput()
_, err := exec.Command(
testCommands["dropdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
if err != nil {
fmt.Println("Teardown error:", err)
@@ -78,7 +125,7 @@ func teardown() {
}
func test_NewClientFromUrl(t *testing.T) {
url := "postgres://postgres@localhost/booktown?sslmode=disable"
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url, nil)
if err != nil {
@@ -90,7 +137,7 @@ func test_NewClientFromUrl(t *testing.T) {
}
func test_NewClientFromUrl2(t *testing.T) {
url := "postgresql://postgres@localhost/booktown?sslmode=disable"
url := fmt.Sprintf("postgresql://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url, nil)
if err != nil {
@@ -156,6 +203,13 @@ func test_Objects(t *testing.T) {
assert.Equal(t, tables, objects["public"].Tables)
assert.Equal(t, []string{"recent_shipments", "stock_view"}, objects["public"].Views)
assert.Equal(t, []string{"author_ids", "book_ids", "shipments_ship_id_seq", "subject_ids"}, objects["public"].Sequences)
major, minor := pgVersion()
if minor == 0 || minor >= 3 {
assert.Equal(t, []string{"m_stock_view"}, objects["public"].MaterializedViews)
} else {
t.Logf("Skipping materialized view on %d.%d\n", major, minor)
}
}
func test_Table(t *testing.T) {
@@ -257,7 +311,8 @@ func test_HistoryError(t *testing.T) {
}
func test_HistoryUniqueness(t *testing.T) {
client, _ := NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable", nil)
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, _ := NewFromUrl(url, nil)
client.Query("SELECT * FROM books WHERE id = 1")
client.Query("SELECT * FROM books WHERE id = 1")
@@ -272,6 +327,7 @@ func TestAll(t *testing.T) {
return
}
initVars()
setupCommands()
teardown()
setup()

View File

@@ -25,9 +25,10 @@ type Result struct {
}
type Objects struct {
Tables []string `json:"tables"`
Views []string `json:"views"`
Sequences []string `json:"sequences"`
Tables []string `json:"table"`
Views []string `json:"view"`
MaterializedViews []string `json:"materialized_view"`
Sequences []string `json:"sequence"`
}
// Due to big int number limitations in javascript, numbers should be encoded
@@ -115,9 +116,10 @@ func ObjectsFromResult(res *Result) map[string]*Objects {
if objects[schema] == nil {
objects[schema] = &Objects{
Tables: []string{},
Views: []string{},
Sequences: []string{},
Tables: []string{},
Views: []string{},
MaterializedViews: []string{},
Sequences: []string{},
}
}
@@ -126,6 +128,8 @@ func ObjectsFromResult(res *Result) map[string]*Objects {
objects[schema].Tables = append(objects[schema].Tables, name)
case "view":
objects[schema].Views = append(objects[schema].Views, name)
case "materialized_view":
objects[schema].MaterializedViews = append(objects[schema].MaterializedViews, name)
case "sequence":
objects[schema].Sequences = append(objects[schema].Sequences, name)
}

File diff suppressed because one or more lines are too long

View File

@@ -92,6 +92,23 @@ WHERE
// ---------------------------------------------------------------------------
PG_MATERIALIZED_VIEW_SCHEMA = `
SELECT
attname as column_name,
atttypid::regtype AS data_type,
(case when attnotnull IS TRUE then 'NO' else 'YES' end) as is_nullable,
null as character_maximum_length,
null as character_set_catalog,
null as column_default
FROM
pg_attribute
WHERE
attrelid = $1::regclass AND
attnum > 0 AND
NOT attisdropped`
// ---------------------------------------------------------------------------
PG_ACTIVITY = `
SELECT
datname,
@@ -130,7 +147,7 @@ FROM
LEFT JOIN
pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind IN ('r','v','S','s','') AND
c.relkind IN ('r','v','m','S','s','') AND
n.nspname !~ '^pg_toast' AND
n.nspname NOT IN ('information_schema', 'pg_catalog')
ORDER BY 1, 2`