Skip to content

Commit

Permalink
drivers/mysql linting (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilotoole authored Mar 13, 2021
1 parent dff14d8 commit d8ecff4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 66 deletions.
8 changes: 2 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ linters-settings:
lines: 200
statements: 100
goconst:
min-len: 2
min-occurrences: 3
min-len: 3
min-occurrences: 4
gocritic:
enabled-tags:
- diagnostic
Expand Down Expand Up @@ -52,8 +52,6 @@ linters-settings:
- (github.com/neilotoole/lg.Log).Errorf
lll:
line-length: 180
maligned:
suggest-new: true
misspell:
locale: US

Expand Down Expand Up @@ -82,7 +80,6 @@ linters:
- gosimple
- govet
- ineffassign
- interfacer
- lll
# - misspell
- nakedret
Expand All @@ -100,7 +97,6 @@ linters:
# - gochecknoglobals
- gocognit
# - godox
- maligned
- prealloc

issues:
Expand Down
1 change: 0 additions & 1 deletion drivers/mysql/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,4 @@ func TestDSNFromLocation(t *testing.T) {
require.Equal(t, tc.wantDSN, gotDSN)
})
}

}
16 changes: 8 additions & 8 deletions drivers/mysql/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func recordMetaFromColumnTypes(log lg.Log, colTypes []*sql.ColumnType) sqlz.Reco
recMeta := make(sqlz.RecordMeta, len(colTypes))

for i, colType := range colTypes {
kind := kindFromDBTypeName(log, colType.Name(), colType.DatabaseTypeName())
colTypeData := sqlz.NewColumnTypeData(colType, kind)
knd := kindFromDBTypeName(log, colType.Name(), colType.DatabaseTypeName())
colTypeData := sqlz.NewColumnTypeData(colType, knd)
recMeta[i] = sqlz.NewFieldMeta(colTypeData)
}

Expand Down Expand Up @@ -312,6 +312,7 @@ FROM information_schema.TABLES t
WHERE t.TABLE_SCHEMA = DATABASE()
ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC`

// nolint:lll
// Query results look like:
// +------------+----------+----------+-------------+----------+-----------+----------------+----------+---------+--------------------+-----------+-----------------+--------------+---------------------------+
// |TABLE_SCHEMA|TABLE_NAME|TABLE_TYPE|TABLE_COMMENT|table_size|COLUMN_NAME|ORDINAL_POSITION|COLUMN_KEY|DATA_TYPE|COLUMN_TYPE |IS_NULLABLE|COLUMN_DEFAULT |COLUMN_COMMENT|EXTRA |
Expand Down Expand Up @@ -345,7 +346,6 @@ ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC`
default:
}

//var colNullable, colKey, colExtra string
var colName, colDefault, colNullable, colKey, colBaseType, colColumnType, colComment, colExtra sql.NullString
var colPosition sql.NullInt64

Expand Down Expand Up @@ -380,18 +380,18 @@ ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC`

rowCountTbl, rowCount, i := curTblName.String, &curTblMeta.RowCount, len(tblMetas)-1
gRowCount.Go(func() error {
err := db.QueryRowContext(gctx, "SELECT COUNT(*) FROM `"+rowCountTbl+"`").Scan(rowCount)
if err != nil {
if hasErrCode(err, errNumTableNotExist) {
gErr := db.QueryRowContext(gctx, "SELECT COUNT(*) FROM `"+rowCountTbl+"`").Scan(rowCount)
if gErr != nil {
if hasErrCode(gErr, errNumTableNotExist) {
// The table was probably dropped while we were collecting
// metadata, but that's ok. We set the element to nil
// and we'll filter it out later.
log.Debugf("Failed to get row count for %q: ignoring: %v", curTblName.String, err)
log.Debugf("Failed to get row count for %q: ignoring: %v", curTblName.String, gErr)
tblMetas[i] = nil
return nil
}

return errz.Err(err)
return errz.Err(gErr)
}
return nil
})
Expand Down
15 changes: 6 additions & 9 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,15 @@ func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (sqlz.RecordMeta, drive

// CreateTable implements driver.SQLDriver.
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error {
createStmt, err := buildCreateTableStmt(tblDef)
if err != nil {
return err
}
createStmt := buildCreateTableStmt(tblDef)

_, err = db.ExecContext(ctx, createStmt)
_, err := db.ExecContext(ctx, createStmt)
return errz.Err(err)
}

// AlterTableAddColumn implements driver.SQLDriver.
func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl string, col string, kind kind.Kind) error {
q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(kind)
func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl, col string, knd kind.Kind) error {
q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(knd)

_, err := db.ExecContext(ctx, q)
if err != nil {
Expand Down Expand Up @@ -285,13 +282,13 @@ func (d *driveri) ValidateSource(src *source.Source) (*source.Source, error) {

// Ping implements driver.Driver.
func (d *driveri) Ping(ctx context.Context, src *source.Source) error {
dbase, err := d.Open(context.TODO(), src)
dbase, err := d.Open(ctx, src)
if err != nil {
return err
}
defer d.log.WarnIfCloseError(dbase.DB())

return dbase.DB().Ping()
return dbase.DB().PingContext(ctx)
}

// Truncate implements driver.SQLDriver. Arg reset is
Expand Down
87 changes: 45 additions & 42 deletions drivers/mysql/sqlbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ var createTblKindDefaults = map[kind.Kind]string{
kind.Unknown: ``,
}

func buildCreateTableStmt(tblDef *sqlmodel.TableDef) (string, error) {
// nolint:funlen
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string {
buf := &bytes.Buffer{}

cols := make([]string, len(tblDef.Cols))
Expand Down Expand Up @@ -134,46 +135,48 @@ func buildCreateTableStmt(tblDef *sqlmodel.TableDef) (string, error) {
fk := ""
buf.Reset()
for _, col := range tblDef.Cols {
if col.ForeignKey != nil {
if buf.Len() > 0 {
buf.WriteString(",\n")
}
buf.WriteString("KEY `")
buf.WriteString(tblDef.Name)
buf.WriteRune('_')
buf.WriteString(col.Name)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("_key` (`")
buf.WriteString(col.Name)
buf.WriteString("`),\nCONSTRAINT `")
buf.WriteString(tblDef.Name)
buf.WriteRune('_')
buf.WriteString(col.Name)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("_fk` FOREIGN KEY (`")
buf.WriteString(col.Name)
buf.WriteString("`) REFERENCES `")
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteString("` (`")
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("`) ON DELETE ")
if col.ForeignKey.OnDelete == "" {
buf.WriteString("CASCADE")
} else {
buf.WriteString(col.ForeignKey.OnDelete)
}
buf.WriteString(" ON UPDATE ")
if col.ForeignKey.OnUpdate == "" {
buf.WriteString("CASCADE")
} else {
buf.WriteString(col.ForeignKey.OnUpdate)
}
if col.ForeignKey == nil {
continue
}

if buf.Len() > 0 {
buf.WriteString(",\n")
}
buf.WriteString("KEY `")
buf.WriteString(tblDef.Name)
buf.WriteRune('_')
buf.WriteString(col.Name)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("_key` (`")
buf.WriteString(col.Name)
buf.WriteString("`),\nCONSTRAINT `")
buf.WriteString(tblDef.Name)
buf.WriteRune('_')
buf.WriteString(col.Name)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteRune('_')
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("_fk` FOREIGN KEY (`")
buf.WriteString(col.Name)
buf.WriteString("`) REFERENCES `")
buf.WriteString(col.ForeignKey.RefTable)
buf.WriteString("` (`")
buf.WriteString(col.ForeignKey.RefCol)
buf.WriteString("`) ON DELETE ")
if col.ForeignKey.OnDelete == "" {
buf.WriteString("CASCADE")
} else {
buf.WriteString(col.ForeignKey.OnDelete)
}
buf.WriteString(" ON UPDATE ")
if col.ForeignKey.OnUpdate == "" {
buf.WriteString("CASCADE")
} else {
buf.WriteString(col.ForeignKey.OnUpdate)
}
}
fk = buf.String()
Expand Down Expand Up @@ -202,7 +205,7 @@ func buildCreateTableStmt(tblDef *sqlmodel.TableDef) (string, error) {
buf.WriteString(fk)
}
buf.WriteString("\n)")
return buf.String(), nil
return buf.String()
}

func buildUpdateStmt(tbl string, cols []string, where string) (string, error) {
Expand Down

0 comments on commit d8ecff4

Please sign in to comment.