drivers/mysql linting (#92)

This commit is contained in:
Neil O'Toole 2021-03-13 10:41:15 -07:00 committed by GitHub
parent dff14d86b6
commit d8ecff4234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 66 deletions

View File

@ -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:

View File

@ -88,5 +88,4 @@ func TestDSNFromLocation(t *testing.T) {
require.Equal(t, tc.wantDSN, gotDSN)
})
}
}

View File

@ -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
})

View File

@ -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

View File

@ -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,7 +135,10 @@ func buildCreateTableStmt(tblDef *sqlmodel.TableDef) (string, error) {
fk := ""
buf.Reset()
for _, col := range tblDef.Cols {
if col.ForeignKey != nil {
if col.ForeignKey == nil {
continue
}
if buf.Len() > 0 {
buf.WriteString(",\n")
}
@ -175,7 +179,6 @@ func buildCreateTableStmt(tblDef *sqlmodel.TableDef) (string, error) {
buf.WriteString(col.ForeignKey.OnUpdate)
}
}
}
fk = buf.String()
buf.Reset()
@ -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) {