Skip to content

Commit

Permalink
Merge pull request #68 from k1LoW/schema-driver-info
Browse files Browse the repository at this point in the history
Add tbls driver information to JSON
  • Loading branch information
k1LoW authored Nov 23, 2018
2 parents 7e499dc + dc99e84 commit 37b2bf3
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 3 deletions.
6 changes: 6 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// Driver is the common interface for database driver
type Driver interface {
Analyze(*sql.DB, *schema.Schema) error
Info(*sql.DB) (*schema.Driver, error)
}

// Analyze database
Expand Down Expand Up @@ -59,6 +60,11 @@ func Analyze(urlstr string) (*schema.Schema, error) {
default:
return s, errors.WithStack(fmt.Errorf("unsupported driver '%s'", u.Driver))
}
d, err := driver.Info(db)
if err != nil {
return s, err
}
s.Driver = d
err = driver.Analyze(db, s)
if err != nil {
return s, err
Expand Down
13 changes: 13 additions & 0 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position`, s.Name, ta
return nil
}

// Info return schema.Driver
func (m *Mysql) Info(db *sql.DB) (*schema.Driver, error) {
var v string
row := db.QueryRow(`SELECT version();`)
row.Scan(&v)

d := &schema.Driver{
Name: "mysql",
DatabaseVersion: v,
}
return d, nil
}

func convertColumnNullable(str string) bool {
if str == "NO" {
return false
Expand Down
14 changes: 14 additions & 0 deletions drivers/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ func TestAnalyzeView(t *testing.T) {
t.Errorf("actual not empty string.")
}
}

func TestInfo(t *testing.T) {
driver := new(Mysql)
d, err := driver.Info(db)
if err != nil {
t.Errorf("%v", err)
}
if d.Name != "mysql" {
t.Errorf("actual %v\nwant %v", d.Name, "mysql")
}
if d.DatabaseVersion == "" {
t.Errorf("actual not empty string.")
}
}
13 changes: 13 additions & 0 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ ORDER BY ordinal_position
return nil
}

// Info return schema.Driver
func (p *Postgres) Info(db *sql.DB) (*schema.Driver, error) {
var v string
row := db.QueryRow(`SELECT version();`)
row.Scan(&v)

d := &schema.Driver{
Name: "postgres",
DatabaseVersion: v,
}
return d, nil
}

func convertColmunType(t string, udtName string, characterMaximumLength sql.NullInt64) string {
switch t {
case "USER-DEFINED":
Expand Down
14 changes: 14 additions & 0 deletions drivers/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ func TestAnalyzeView(t *testing.T) {
t.Errorf("actual not empty string.")
}
}

func TestInfo(t *testing.T) {
driver := new(Postgres)
d, err := driver.Info(db)
if err != nil {
t.Errorf("%v", err)
}
if d.Name != "postgres" {
t.Errorf("actual %v\nwant %v", d.Name, "postgres")
}
if d.DatabaseVersion == "" {
t.Errorf("actual not empty string.")
}
}
16 changes: 15 additions & 1 deletion drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"sort"
"strings"

"regexp"

"github.com/k1LoW/tbls/schema"
"github.com/pkg/errors"
"regexp"
)

var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s]+)\s?\((.+)\)`)
Expand Down Expand Up @@ -359,6 +360,19 @@ SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ?;
return nil
}

// Info return schema.Driver
func (l *Sqlite) Info(db *sql.DB) (*schema.Driver, error) {
var v string
row := db.QueryRow(`SELECT sqlite_version();`)
row.Scan(&v)

d := &schema.Driver{
Name: "sqlite",
DatabaseVersion: v,
}
return d, nil
}

func convertColumnNullable(str string) bool {
if str == "1" {
return false
Expand Down
14 changes: 14 additions & 0 deletions drivers/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ func TestAnalyzeView(t *testing.T) {
}
}

func TestInfo(t *testing.T) {
driver := new(Sqlite)
d, err := driver.Info(db)
if err != nil {
t.Errorf("%v", err)
}
if d.Name != "sqlite" {
t.Errorf("actual %v\nwant %v", d.Name, "sqlite")
}
if d.DatabaseVersion == "" {
t.Errorf("actual not empty string.")
}
}

func TestParseCheckConstraints(t *testing.T) {
sql := `CREATE TABLE check_constraints (
id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down
4 changes: 4 additions & 0 deletions output/dot/dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func newTestSchema() *schema.Schema {
Relations: []*schema.Relation{
r,
},
Driver: &schema.Driver{
Name: "testdriver",
DatabaseVersion: "1.0.0",
},
}
return s
}
4 changes: 4 additions & 0 deletions output/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func newTestSchema() *schema.Schema {
Relations: []*schema.Relation{
r,
},
Driver: &schema.Driver{
Name: "testdriver",
DatabaseVersion: "1.0.0",
},
}
return s
}
7 changes: 7 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ type Relation struct {
IsAdditional bool `json:"is_additional"`
}

// Driver is the struct for tbls driver information
type Driver struct {
Name string `json:"name"`
DatabaseVersion string `json:"database_version"`
}

// Schema is the struct for database schema
type Schema struct {
Name string `json:"name"`
Tables []*Table `json:"tables"`
Relations []*Relation `json:"relations"`
Driver *Driver `json:"driver"`
}

// AdditionalData is the struct for table relations from yaml
Expand Down
4 changes: 4 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ func newTestSchema() *Schema {
Relations: []*Relation{
r,
},
Driver: &Driver{
Name: "testdriver",
DatabaseVersion: "1.0.0",
},
}
return s
}
6 changes: 5 additions & 1 deletion testdata/json_test_schema.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,9 @@
"def": "",
"is_additional": false
}
]
],
"driver": {
"name": "testdriver",
"database_version": "1.0.0"
}
}
6 changes: 5 additions & 1 deletion testdata/testdb.json
Original file line number Diff line number Diff line change
Expand Up @@ -2271,5 +2271,9 @@
"def": "Additional Relation",
"is_additional": true
}
]
],
"driver": {
"name": "mysql",
"database_version": "5.7.23"
}
}

0 comments on commit 37b2bf3

Please sign in to comment.