Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golangci-lint #251

Merged
merged 11 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ jobs:

services:
oracle:
image: deepdiver/docker-oracle-xe-11g
image: deepdiver/docker-oracle-xe-11g:2.0
ports:
- 1521
env:
Expand Down Expand Up @@ -219,7 +219,7 @@ jobs:
POSTGRES_PASSWORD: postgres

oracle:
image: deepdiver/docker-oracle-xe-11g
image: deepdiver/docker-oracle-xe-11g:2.0
ports:
- 1521
env:
Expand Down Expand Up @@ -307,14 +307,10 @@ jobs:
env:
RUNNER_OS: ${{ runner.os }}

- name: Run lint
run: |
set -xe
go install golang.org/x/lint/golint@latest

make fmtci
make lint
make vet
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.58

- name: Slack Notification (not success)
uses: act10ns/slack@v2
Expand Down
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
linters-settings:
gofmt:
revive:
testifylint:
wrapcheck:

linters:
enable:
- gofmt
- revive
- testifylint
- wrapcheck
14 changes: 1 addition & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,9 @@ testrace:
fmt:
go fmt ./...

.PHONY: fmtci
fmtci:
! gofmt -d . | grep '^'

.PHONY: lint
lint:
golint -set_exit_status ./...

.PHONY: vet
vet:
go vet ./...

.PHONY: integration_test
integration_test: build build-oracle
go test _integration/check_readme_test.go

.PHONY: test_all
test_all: test testrace fmt lint vet integration_test
test_all: test testrace fmt integration_test
19 changes: 10 additions & 9 deletions adapter/mysql/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql

import (
"fmt"
"github.com/cockroachdb/errors"
"sort"
"strings"

Expand All @@ -23,7 +24,7 @@ func NewAdapter(config *mysql.Config) (*Adapter, Close, error) {
db, err := sqlx.Connect("mysql", config.FormatDSN())

if err != nil {
return nil, nil, err
return nil, nil, errors.WithStack(err)
}

return &Adapter{db: db}, db.Close, nil
Expand All @@ -43,7 +44,7 @@ func (a *Adapter) GetAllTableNames() ([]string, error) {
err := a.db.Select(&rows, "SELECT table_name AS table_name FROM information_schema.tables WHERE table_schema=database() AND table_type = 'BASE TABLE' ORDER BY table_name")

if err != nil {
return []string{}, err
return []string{}, errors.WithStack(err)
}

var tables []string
Expand All @@ -63,15 +64,15 @@ func (a *Adapter) GetTable(tableName string) (*db.Table, error) {
rows, err := a.db.Queryx(fmt.Sprintf("SHOW COLUMNS FROM %s", tableName))

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

for rows.Next() {
row := map[string]interface{}{}
err := rows.MapScan(row)

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

column := &db.Column{
Expand All @@ -86,14 +87,14 @@ func (a *Adapter) GetTable(tableName string) (*db.Table, error) {

foreignKeys, err := a.getForeignKeys(tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

table.ForeignKeys = foreignKeys

indexes, err := a.getIndexes(tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

table.Indexes = indexes
Expand All @@ -118,7 +119,7 @@ func (a *Adapter) getForeignKeys(tableName string) ([]*db.ForeignKey, error) {

err := a.db.Select(&rows, sql, tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var foreignKeys []*db.ForeignKey
Expand Down Expand Up @@ -155,7 +156,7 @@ func (a *Adapter) getIndexes(tableName string) ([]*db.Index, error) {
rows, err := a.db.Queryx(fmt.Sprintf("SHOW INDEX FROM %s WHERE Key_name != 'PRIMARY'", tableName))

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var indexes []*db.Index
Expand All @@ -166,7 +167,7 @@ func (a *Adapter) getIndexes(tableName string) ([]*db.Index, error) {
err := rows.MapScan(row)

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

keyName := rowString(row, "Key_name")
Expand Down
4 changes: 2 additions & 2 deletions adapter/mysql/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func withDatabase(callback func(*Adapter)) {
config.User = os.Getenv("MYSQL_USER")
config.Passwd = os.Getenv("MYSQL_PASSWORD")
config.DBName = os.Getenv("MYSQL_DATABASE")
adapter, close, err := NewAdapter(config)
adapter, closeDatabase, err := NewAdapter(config)

if err != nil {
panic(err)
}

defer close()
defer closeDatabase() //nolint:errcheck

adapter.db.MustExec("DROP TABLE IF EXISTS followers;")
adapter.db.MustExec("DROP TABLE IF EXISTS articles;")
Expand Down
33 changes: 17 additions & 16 deletions adapter/oracle/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oracle

import (
"github.com/cockroachdb/errors"
mapset "github.com/deckarep/golang-set"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-oci8" // for sql
Expand All @@ -20,7 +21,7 @@ func NewAdapter(config *Config) (*Adapter, Close, error) {
db, err := sqlx.Connect("oci8", config.FormatDSN())

if err != nil {
return nil, nil, err
return nil, nil, errors.WithStack(err)
}

return &Adapter{db: db}, db.Close, nil
Expand All @@ -42,7 +43,7 @@ func (a *Adapter) GetAllTableNames() ([]string, error) {
`)

if err != nil {
return []string{}, err
return []string{}, errors.WithStack(err)
}

var tables []string
Expand All @@ -61,7 +62,7 @@ func (a *Adapter) GetTable(tableName string) (*db.Table, error) {

primaryKeyColumns, err := a.getPrimaryKeyColumns(tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

sql := `
Expand All @@ -72,15 +73,15 @@ func (a *Adapter) GetTable(tableName string) (*db.Table, error) {
`
stmt, err := a.db.Preparex(a.db.Rebind(sql))
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var rows []allTabColumns
err = stmt.Select(&rows, tableName)
defer stmt.Close()

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

for _, row := range rows {
Expand All @@ -95,13 +96,13 @@ func (a *Adapter) GetTable(tableName string) (*db.Table, error) {

foreignKeys, err := a.getForeignKeys(tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
table.ForeignKeys = foreignKeys

indexes, err := a.getIndexes(tableName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
table.Indexes = indexes

Expand All @@ -123,15 +124,15 @@ func (a *Adapter) getPrimaryKeyColumns(tableName string) (mapset.Set, error) {

stmt, err := a.db.Preparex(a.db.Rebind(sql))
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var rows []primaryKeys
err = stmt.Select(&rows, tableName)
defer stmt.Close()

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

columns := mapset.NewSet()
Expand Down Expand Up @@ -165,15 +166,15 @@ func (a *Adapter) getForeignKeys(tableName string) ([]*db.ForeignKey, error) {

stmt, err := a.db.Preparex(a.db.Rebind(sql))
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var rows []foreignKey
err = stmt.Select(&rows, tableName)
defer stmt.Close()

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var foreignKeys []*db.ForeignKey
Expand Down Expand Up @@ -208,21 +209,21 @@ func (a *Adapter) getIndexes(tableName string) ([]*db.Index, error) {

stmt, err := a.db.Preparex(a.db.Rebind(sql))
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var rows []allIndexes
err = stmt.Select(&rows, tableName)
defer stmt.Close()

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
var indexes []*db.Index
for _, row := range rows {
columns, err := a.getIndexColumns(row.IndexName)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

index := &db.Index{
Expand All @@ -242,15 +243,15 @@ func (a *Adapter) getIndexColumns(indexName string) ([]string, error) {

stmt, err := a.db.Preparex(a.db.Rebind(sql))
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var rows []allIndColumns
err = stmt.Select(&rows, indexName)
defer stmt.Close()

if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var columns []string
Expand Down
4 changes: 2 additions & 2 deletions adapter/oracle/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func withDatabase(callback func(*Adapter)) {
config.Username = os.Getenv("ORACLE_USER")
config.Password = os.Getenv("ORACLE_PASSWORD")
config.ServiceName = os.Getenv("ORACLE_SERVICE")
adapter, close, err := NewAdapter(config)
adapter, closeDatabase, err := NewAdapter(config)

if err != nil {
panic(err)
}

defer close()
defer closeDatabase() //nolint:errcheck

// adapter.db.MustExec("DROP TABLE followers")
// adapter.db.MustExec("DROP TABLE articles")
Expand Down
Loading
Loading