Skip to content

Commit

Permalink
wrap errors for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh authored and marccampbell committed Apr 11, 2020
1 parent 8ffb7a9 commit 12a4ecd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
23 changes: 9 additions & 14 deletions pkg/generate/generate.go
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"
schemasv1alpha3 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha3"
"github.com/schemahero/schemahero/pkg/database/interfaces"
"github.com/schemahero/schemahero/pkg/database/mysql"
Expand All @@ -32,47 +33,42 @@ func (g *Generator) RunSync() error {
if g.Viper.GetString("driver") == "postgres" {
pgDb, err := postgres.Connect(g.Viper.GetString("uri"))
if err != nil {
return err
return errors.Wrap(err, "postgres connect")
}
db = pgDb
} else if g.Viper.GetString("driver") == "mysql" {
mysqlDb, err := mysql.Connect(g.Viper.GetString("uri"))
if err != nil {
return err
return errors.Wrap(err, "mysql connect")
}
db = mysqlDb
}

tableNames, err := db.ListTables()
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "list tables")
}

filesWritten := make([]string, 0, 0)
for _, tableName := range tableNames {
primaryKey, err := db.GetTablePrimaryKey(tableName)
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "get table primary key")
}

foreignKeys, err := db.ListTableForeignKeys(g.Viper.GetString("dbname"), tableName)
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "list table foreign keys")
}

indexes, err := db.ListTableIndexes(g.Viper.GetString("dbname"), tableName)
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "list table indexes")
}

columns, err := db.GetTableSchema(tableName)
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "get table schema")
}

var primaryKeyColumns []string
Expand All @@ -81,8 +77,7 @@ func (g *Generator) RunSync() error {
}
tableYAML, err := generateTableYAML(g.Viper.GetString("driver"), g.Viper.GetString("dbname"), tableName, primaryKeyColumns, foreignKeys, indexes, columns)
if err != nil {
fmt.Printf("%#v\n", err)
return err
return errors.Wrap(err, "generate table yaml")
}

// If there was a outputdir set, write it, else print it
Expand Down
52 changes: 52 additions & 0 deletions pkg/watcher/watcher.go
@@ -0,0 +1,52 @@
package watcher

import (
"fmt"
"time"

"github.com/pkg/errors"
"github.com/schemahero/schemahero/pkg/database/interfaces"
"github.com/schemahero/schemahero/pkg/database/mysql"
"github.com/schemahero/schemahero/pkg/database/postgres"
"github.com/spf13/viper"
)

type Watcher struct {
Viper *viper.Viper
}

func NewWatcher() *Watcher {
return &Watcher{
Viper: viper.GetViper(),
}
}

func (w *Watcher) RunSync() error {
fmt.Printf("connecting to %s\n", w.Viper.GetString("uri"))

var conn interfaces.SchemaHeroDatabaseConnection

if w.Viper.GetString("driver") == "postgres" {
c, err := postgres.Connect(w.Viper.GetString("uri"))
if err != nil {
return errors.Wrap(err, "postgres connect")
}

conn = c
} else if w.Viper.GetString("driver") == "mysql" {
c, err := mysql.Connect(w.Viper.GetString("uri"))
if err != nil {
return errors.Wrap(err, "mysql connect")
}

conn = c
}

for {
if _, err := conn.CheckAlive(w.Viper.GetString("namespace"), w.Viper.GetString("instance")); err != nil {
return errors.Wrap(err, "check alive")
}

time.Sleep(time.Second * 10)
}
}

0 comments on commit 12a4ecd

Please sign in to comment.