diff --git a/.golangci.yml b/.golangci.yml index c75c6206..f4ec69a3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 @@ -52,8 +52,6 @@ linters-settings: - (github.com/neilotoole/lg.Log).Errorf lll: line-length: 180 - maligned: - suggest-new: true misspell: locale: US @@ -82,7 +80,6 @@ linters: - gosimple - govet - ineffassign - - interfacer - lll # - misspell - nakedret @@ -100,7 +97,6 @@ linters: # - gochecknoglobals - gocognit # - godox - - maligned - prealloc issues: diff --git a/drivers/mysql/internal_test.go b/drivers/mysql/internal_test.go index 4aa63542..9026292f 100644 --- a/drivers/mysql/internal_test.go +++ b/drivers/mysql/internal_test.go @@ -88,5 +88,4 @@ func TestDSNFromLocation(t *testing.T) { require.Equal(t, tc.wantDSN, gotDSN) }) } - } diff --git a/drivers/mysql/metadata.go b/drivers/mysql/metadata.go index 6911b6fc..753c7207 100644 --- a/drivers/mysql/metadata.go +++ b/drivers/mysql/metadata.go @@ -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) } @@ -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 | @@ -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 @@ -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 }) diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index 5747fb2c..737d2200 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -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 { @@ -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 diff --git a/drivers/mysql/sqlbuilder.go b/drivers/mysql/sqlbuilder.go index d8571f55..27b43bcb 100644 --- a/drivers/mysql/sqlbuilder.go +++ b/drivers/mysql/sqlbuilder.go @@ -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)) @@ -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() @@ -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) {