Skip to content

Commit

Permalink
Add golangci-lint and fix errors (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Jan 27, 2023
1 parent bf26560 commit 32ad603
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 30 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: golangci
on:
push:
branches:
- master
- main
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19.x
check-latest: true
cache: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.1

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ start-postgres:
.PHONY: clean
clean:
@find . -type f -name '*.FAIL' -delete

.PHONY: lint
lint: tools
@golangci-lint run ./... --fix

.PHONY: tools
tools:
@go install github.com/golangci/golangci-lint/cmd/[email protected]
5 changes: 4 additions & 1 deletion cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ var (

func main() {
flags.Usage = usage
flags.Parse(os.Args[1:])
if err := flags.Parse(os.Args[1:]); err != nil {
log.Fatalf("failed to parse args: %v", err)
return
}

if *version {
if buildInfo, ok := debug.ReadBuildInfo(); ok && buildInfo != nil && gooseVersion == "" {
Expand Down
3 changes: 2 additions & 1 deletion goose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"github.com/pressly/goose/v3/internal/check"
_ "modernc.org/sqlite"
)

Expand Down Expand Up @@ -172,7 +173,7 @@ func TestEmbeddedMigrations(t *testing.T) {
}

SetBaseFS(fsys)
SetDialect("sqlite3")
check.NoError(t, SetDialect("sqlite3"))
t.Cleanup(func() { SetBaseFS(nil) })

t.Run("Migration cycle", func(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions internal/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,3 @@ func reflectToInt64(v interface{}) (int64, error) {
}
return 0, fmt.Errorf("invalid number: must be int64 type: got:%T", v)
}

func reflectToStr(v interface{}) (string, error) {
switch typ := v.(type) {
case string:
return reflect.ValueOf(typ).String(), nil
}
return "", fmt.Errorf("invalid string: got:%T", v)
}
4 changes: 2 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ func createVersionTable(db *sql.DB) error {
d := GetDialect()

if _, err := txn.Exec(d.createVersionTableSQL()); err != nil {
txn.Rollback()
_ = txn.Rollback()
return err
}

version := 0
applied := true
if _, err := txn.Exec(d.insertVersionSQL(), version, applied); err != nil {
txn.Rollback()
_ = txn.Rollback()
return err
}

Expand Down
4 changes: 2 additions & 2 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ func runGoMigration(
if fn != nil {
// Run go migration function.
if err := fn(tx); err != nil {
tx.Rollback()
_ = tx.Rollback()
return fmt.Errorf("failed to run go migration: %w", err)
}
}
if recordVersion {
if err := insertOrDeleteVersion(tx, version, direction); err != nil {
tx.Rollback()
_ = tx.Rollback()
return fmt.Errorf("failed to update version: %w", err)
}
}
Expand Down
11 changes: 6 additions & 5 deletions migration_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
verboseInfo("Executing statement: %s\n", clearStatement(query))
if err = execQuery(tx.Exec, query); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
_ = tx.Rollback()
return fmt.Errorf("failed to execute SQL query %q: %w", clearStatement(query), err)
}
}
Expand All @@ -39,13 +39,13 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
if direction {
if err := execQuery(tx.Exec, GetDialect().insertVersionSQL(), v, direction); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
_ = tx.Rollback()
return fmt.Errorf("failed to insert new goose version: %w", err)
}
} else {
if err := execQuery(tx.Exec, GetDialect().deleteVersionSQL(), v); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
_ = tx.Rollback()
return fmt.Errorf("failed to delete goose version: %w", err)
}
}
Expand Down Expand Up @@ -95,12 +95,13 @@ func execQuery(fn func(string, ...interface{}) (sql.Result, error), query string
}()

t := time.Now()

ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case err := <-ch:
return err
case <-time.Tick(time.Minute):
case <-ticker.C:
verboseInfo("Executing statement still in progress for %v", time.Since(t).Round(time.Second))
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestClickUpDownAll(t *testing.T) {
check.NoError(t, err)
t.Cleanup(cleanup)

goose.SetDialect("clickhouse")
check.NoError(t, goose.SetDialect("clickhouse"))

retryCheckTableMutation := func(table string) func() error {
return func() error {
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestClickHouseFirstThree(t *testing.T) {
check.NoError(t, err)
t.Cleanup(cleanup)

goose.SetDialect("clickhouse")
check.NoError(t, goose.SetDialect("clickhouse"))

err = goose.Up(db, migrationDir)
check.NoError(t, err)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestRemoteImportMigration(t *testing.T) {
check.NoError(t, err)
t.Cleanup(cleanup)

goose.SetDialect("clickhouse")
check.NoError(t, goose.SetDialect("clickhouse"))

err = goose.Up(db, migrationDir)
check.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/allow_missing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func setupTestDB(t *testing.T, version int64) *sql.DB {
db, err := newDockerDB(t)
check.NoError(t, err)

goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))

// Create goose table.
current, err := goose.EnsureDBVersion(db)
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestMigrateUpWithReset(t *testing.T) {

db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))
migrations, err := goose.CollectMigrations(migrationsDir, 0, goose.MaxVersion)
check.NoError(t, err)
check.NumberNotZero(t, len(migrations))
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestMigrateUpWithRedo(t *testing.T) {

db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))
migrations, err := goose.CollectMigrations(migrationsDir, 0, goose.MaxVersion)
check.NoError(t, err)

Expand Down Expand Up @@ -84,7 +84,7 @@ func TestMigrateUpTo(t *testing.T) {
)
db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))
migrations, err := goose.CollectMigrations(migrationsDir, 0, goose.MaxVersion)
check.NoError(t, err)
check.NumberNotZero(t, len(migrations))
Expand All @@ -106,7 +106,7 @@ func TestMigrateUpByOne(t *testing.T) {

db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))
migrations, err := goose.CollectMigrations(migrationsDir, 0, goose.MaxVersion)
check.NoError(t, err)
check.NumberNotZero(t, len(migrations))
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestMigrateFull(t *testing.T) {

db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))
migrations, err := goose.CollectMigrations(migrationsDir, 0, goose.MaxVersion)
check.NoError(t, err)
check.NumberNotZero(t, len(migrations))
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/no_versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNoVersioning(t *testing.T) {
)
db, err := newDockerDB(t)
check.NoError(t, err)
goose.SetDialect(*dialect)
check.NoError(t, goose.SetDialect(*dialect))

err = goose.Up(db, migrationsDir)
check.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/vertica/vertica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestVerticaUpDownAll(t *testing.T) {
check.NoError(t, err)
t.Cleanup(cleanup)

goose.SetDialect("vertica")
check.NoError(t, goose.SetDialect("vertica"))

goose.SetTableName("goose_db_version")

Expand Down

0 comments on commit 32ad603

Please sign in to comment.