Skip to content

Commit

Permalink
fixes linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tgallant committed Jul 19, 2021
1 parent 5da9831 commit d6552cc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
7 changes: 6 additions & 1 deletion cmd/db2jsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ var (

func HandleGenerate(cmd *cobra.Command, args []string) {
if len(driver) == 0 || len(dburl) == 0 {
cmd.Help()
err := cmd.Help()
if err != nil {
log.Error(err)
os.Exit(1)
return
}
return
}
req := &db2jsonschema.Request{
Expand Down
43 changes: 23 additions & 20 deletions database/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ type SQLiteTable struct {
}

type SQLiteCreateTable struct {
TableName string `"CREATE" "TABLE" @Ident`
FieldExpressions []*SQLiteFieldExpression `( "(" @@ ( "," @@ )* ( "," )?`
PrimaryKeys []string `( "PRIMARY" "KEY" "(" @Ident ( "," @Ident )* ")" )* ( "," )? )?`
Constraints []*SQLiteConstraint `( "CONSTRAINT" @@ ( "," "CONSTRAINT" @@ )* )? ")"`
TableName string `parser:"'CREATE' 'TABLE' @Ident"`
FieldExpressions []*SQLiteFieldExpression `parser:"( '(' @@ ( ',' @@ )* ( ',' )?"`
PrimaryKeys []string `parser:"( 'PRIMARY' 'KEY' '(' @Ident ( ',' @Ident )* ')' )* ( ',' )? )?"`
Constraints []*SQLiteConstraint `parser:"( 'CONSTRAINT' @@ ( ',' 'CONSTRAINT' @@ )* )? ')'"`
}

type SQLiteFieldExpression struct {
Name string `@Ident`
Type string `@Ident ( "(" Number ")" )*`
NotNull bool `( @"NOT_NULL"`
AutoIncrement bool `| @"AUTO_INCREMENT" )*`
Name string `parser:"@Ident"`
Type string `parser:"@Ident ( '(' Number ')' )*"`
NotNull bool `parser:"( @'NOT_NULL'"`
AutoIncrement bool `parser:"| @'AUTO_INCREMENT' )*"`
}

type SQLiteConstraint struct {
Name string `@Ident`
ForeignKey string `"FOREIGN" "KEY" "(" @Ident ")"`
ReferencedTable string `"REFERENCES" @Ident`
ReferencedField string `"(" @Ident ")"`
Name string `parser:"@Ident"`
ForeignKey string `parser:"'FOREIGN' 'KEY' '(' @Ident ')'"`
ReferencedTable string `parser:"'REFERENCES' @Ident"`
ReferencedField string `parser:"'(' @Ident ')'"`
}

var (
Expand Down Expand Up @@ -66,13 +66,13 @@ var (
}

sqlLexer = lexer.Must(stateful.NewSimple([]stateful.Rule{
{`Keyword`, `(?i)\b(CREATE|TABLE|PRIMARY|FOREIGN|KEY|CONSTRAINT|REFERENCE)\b`, nil},
{`Ident`, `[a-zA-Z_][a-zA-Z0-9_]*`, nil},
{`Number`, `[-+]?\d*\.?\d+([eE][-+]?\d+)?`, nil},
{`String`, `'[^']*'|"[^"]*"`, nil},
{`Operators`, `<>|!=|<=|>=|[-+*/%,.()=<>]`, nil},
{"whitespace", `\s+`, nil},
{"backtick", "`", nil},
{Name: `Keyword`, Pattern: `(?i)\b(CREATE|TABLE|PRIMARY|FOREIGN|KEY|CONSTRAINT|REFERENCE)\b`, Action: nil},
{Name: `Ident`, Pattern: `[a-zA-Z_][a-zA-Z0-9_]*`, Action: nil},
{Name: `Number`, Pattern: `[-+]?\d*\.?\d+([eE][-+]?\d+)?`, Action: nil},
{Name: `String`, Pattern: `'[^']*'|"[^"]*"`, Action: nil},
{Name: `Operators`, Pattern: `<>|!=|<=|>=|[-+*/%,.()=<>]`, Action: nil},
{Name: "whitespace", Pattern: `\s+`, Action: nil},
{Name: "backtick", Pattern: "`", Action: nil},
}))

parser = participle.MustBuild(
Expand Down Expand Up @@ -100,7 +100,10 @@ func SelectTables(conn *sql.DB) ([]*SQLiteTable, error) {
for row.Next() {
var name string
var sql string
row.Scan(&name, &sql)
err = row.Scan(&name, &sql)
if err != nil {
return nil, err
}
table := SQLiteTable{name, sql}
tables = append(tables, &table)
}
Expand Down
7 changes: 3 additions & 4 deletions test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Track struct {
Genre Genre
}

func MigrateTables(db *gorm.DB) {
db.AutoMigrate(
func MigrateTables(db *gorm.DB) error {
return db.AutoMigrate(
&Genre{},
&Artist{},
&Album{},
Expand Down Expand Up @@ -71,6 +71,5 @@ func (t *TestDB) Setup() error {
if err != nil {
return err
}
MigrateTables(db)
return nil
return MigrateTables(db)
}
6 changes: 6 additions & 0 deletions test/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ func TestMain(m *testing.M) {
fmt.Println(tempDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
tempDir = newTestDir
tempFile, err := os.CreateTemp(tempDir, "test.*.db")
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
testDB.DataSource = tempFile.Name()
err = testDB.Setup()
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
m.Run()
err = os.RemoveAll(tempDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
os.Exit(0)
}

func TestYAMLOutput(t *testing.T) {
Expand Down

0 comments on commit d6552cc

Please sign in to comment.