Skip to content

Commit

Permalink
start including database parameters in inspect results
Browse files Browse the repository at this point in the history
  • Loading branch information
sjansen committed Jun 19, 2019
1 parent c077f5b commit 952491a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/cli/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func registerInspect(p *ArgParser) {
c := &commands.InspectCmd{}
cmd := p.addCommand(c, "inspect", "Scan a datbase and output its configuration")
cmd := p.addCommand(c, "inspect", "Scan a database and output its configuration")

cmd.Flag("host", "server hostname or socket directory").Short('h').StringVar(&c.Host)
cmd.Flag("port", "server port number").Short('p').Uint16Var(&c.Port)
Expand Down
1 change: 1 addition & 0 deletions internal/ddl/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Database struct {
// Parameters describes database-level configuration options
type Parameters struct {
SearchPath []string `hcl:"search_path,optional"`
Timezone string `hcl:"timezone,optional"`
}

// A Schema is a database namespace
Expand Down
1 change: 1 addition & 0 deletions internal/ddl/testdata/expected.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

parameters {
search_path = ["$user", "public"]
timezone = ""
}

schema "public" {
Expand Down
19 changes: 16 additions & 3 deletions internal/pg/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func (c *Conn) InspectDatabase(o *InspectOptions) (db *ddl.Database, err error)
o = &InspectOptions{}
}

db.Parameters, err = c.ListParameters()
if err != nil {
return nil, err
}

db.Schemas, err = c.ListSchemas()
if err != nil {
return nil, err
Expand All @@ -29,14 +34,22 @@ func (c *Conn) InspectDatabase(o *InspectOptions) (db *ddl.Database, err error)
return nil, err
}

if err := c.inspectTables(o, db); err != nil {
return nil, err
}

return db, nil
}

func (c *Conn) inspectTables(o *InspectOptions, db *ddl.Database) (err error) {
db.Tables, err = c.ListTables()
if err != nil {
return nil, err
return err
}

for _, table := range db.Tables {
if err := c.inspectTable(o, db, table); err != nil {
return nil, err
return err
}
}

Expand All @@ -50,7 +63,7 @@ func (c *Conn) InspectDatabase(o *InspectOptions) (db *ddl.Database, err error)
})
}

return db, nil
return
}

func (c *Conn) inspectTable(o *InspectOptions, db *ddl.Database, table *ddl.Table) (err error) {
Expand Down
32 changes: 32 additions & 0 deletions internal/pg/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pg

import (
"strings"

"github.com/sjansen/pgutil/internal/ddl"
)

// ListParameters describes database configuration parameters
func (c *Conn) ListParameters() (*ddl.Parameters, error) {
c.log.Infow("listing parameters")

params := &ddl.Parameters{}

var tmp string
c.log.Debugw("SHOW search_path")
row := c.conn.QueryRow("SHOW search_path")
err := row.Scan(&tmp)
if err != nil {
return nil, err
}
params.SearchPath = strings.Split(tmp, ",")

c.log.Debugw("SHOW timezone")
row = c.conn.QueryRow("SHOW timezone")
err = row.Scan(&params.Timezone)
if err != nil {
return nil, err
}

return params, nil
}
5 changes: 5 additions & 0 deletions testdata/expected.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

parameters {
search_path = ["\"$user\"", " public"]
timezone = "UTC"
}

schema "public" {
comment = "standard public schema"
owner = "docker"
Expand Down

0 comments on commit 952491a

Please sign in to comment.