From ed91eff65f0e48aea4d2fe012017014371f0abe2 Mon Sep 17 00:00:00 2001 From: Nikita Bolotov Date: Wed, 16 Apr 2025 21:33:49 +0300 Subject: [PATCH 1/2] updated golanglint ci version --- .github/workflows/test.yml | 4 +- .golangci.yaml | 214 ++++++++++++++++++---------------- migrate.go | 12 +- migrate_test.go | 16 +-- sql-migrate/command_common.go | 7 +- 5 files changed, 135 insertions(+), 118 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cc0a3295..7624377f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,9 +36,9 @@ jobs: cache: true cache-dependency-path: go.sum - name: golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v7 with: - version: v1.64 + version: v2 - name: go mod tidy run: go mod tidy - name: check for any changes diff --git a/.golangci.yaml b/.golangci.yaml index fdf5e2a5..f5818416 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,117 +1,133 @@ -linters-settings: - gocritic: - disabled-checks: - - ifElseChain - - goimports: - local-prefixes: github.com/rubenv/sql-migrate - - govet: - enable-all: true - disable: - - fieldalignment - - depguard: - rules: - main: - allow: - - $gostd - - github.com/denisenkom/go-mssqldb - - github.com/go-sql-driver/mysql - - github.com/go-gorp/gorp/v3 - - github.com/lib/pq - - github.com/mattn/go-sqlite3 - - github.com/mitchellh/cli - - github.com/olekukonko/tablewriter - - github.com/rubenv/sql-migrate - - gopkg.in/check.v1 - - gopkg.in/yaml.v2 - - exhaustive: - default-signifies-exhaustive: true - - nolintlint: - allow-unused: false - allow-no-explanation: - - depguard - require-explanation: true - require-specific: true - - revive: - enable-all-rules: false - rules: - - name: atomic - - name: blank-imports - - name: bool-literal-in-expr - - name: call-to-gc - - name: constant-logical-expr - - name: context-as-argument - - name: context-keys-type - - name: dot-imports - - name: duplicated-imports - - name: empty-block - - name: empty-lines - - name: error-naming - - name: error-return - - name: error-strings - - name: errorf - - name: exported - - name: identical-branches - - name: imports-blocklist - - name: increment-decrement - - name: indent-error-flow - - name: modifies-parameter - - name: modifies-value-receiver - - name: package-comments - - name: range - - name: range-val-address - - name: range-val-in-closure - - name: receiver-naming - - name: string-format - - name: string-of-int - - name: struct-tag - - name: time-naming - - name: unconditional-recursion - - name: unexported-naming - - name: unexported-return - - name: superfluous-else - - name: unreachable-code - - name: var-declaration - - name: waitgroup-by-value - - name: unused-receiver - - name: unnecessary-stmt - - name: unused-parameter - +version: "2" run: tests: true - timeout: 1m - linters: - disable-all: true + default: none enable: - asciicheck - depguard - errcheck + - errorlint - exhaustive - gocritic - - gofmt - - gofumpt - - goimports - govet - ineffassign - nolintlint - revive - staticcheck - - typecheck + - unparam - unused - whitespace - - errorlint - - gosimple - - unparam - + settings: + depguard: + rules: + main: + allow: + - $gostd + - github.com/denisenkom/go-mssqldb + - github.com/go-sql-driver/mysql + - github.com/go-gorp/gorp/v3 + - github.com/lib/pq + - github.com/mattn/go-sqlite3 + - github.com/mitchellh/cli + - github.com/olekukonko/tablewriter + - github.com/rubenv/sql-migrate + - gopkg.in/check.v1 + - gopkg.in/yaml.v2 + exhaustive: + default-signifies-exhaustive: true + gocritic: + disabled-checks: + - ifElseChain + govet: + disable: + - fieldalignment + enable-all: true + nolintlint: + require-explanation: true + require-specific: true + allow-no-explanation: + - depguard + allow-unused: false + revive: + enable-all-rules: false + rules: + - name: atomic + - name: blank-imports + - name: bool-literal-in-expr + - name: call-to-gc + - name: constant-logical-expr + - name: context-as-argument + - name: context-keys-type + - name: dot-imports + - name: duplicated-imports + - name: empty-block + - name: empty-lines + - name: error-naming + - name: error-return + - name: error-strings + - name: errorf + - name: exported + - name: identical-branches + - name: imports-blocklist + - name: increment-decrement + - name: indent-error-flow + - name: modifies-parameter + - name: modifies-value-receiver + - name: package-comments + - name: range + - name: range-val-address + - name: range-val-in-closure + - name: receiver-naming + - name: string-format + - name: string-of-int + - name: struct-tag + - name: time-naming + - name: unconditional-recursion + - name: unexported-naming + - name: unexported-return + - name: superfluous-else + - name: unreachable-code + - name: var-declaration + - name: waitgroup-by-value + - name: unused-receiver + - name: unnecessary-stmt + - name: unused-parameter + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - path: (.+)\.go$ + text: declaration of "err" shadows declaration at + - path: (.+)\.go$ + text: 'error-strings: error strings should not be capitalized or end with punctuation or a newline' + - path: (.+)\.go$ + text: 'ST1005: error strings should not end with punctuation or newline' + - path: (.+)\.go$ + text: 'ST1005: error strings should not be capitalized' + paths: + - third_party$ + - builtin$ + - examples$ issues: - exclude: - - 'declaration of "err" shadows declaration at' # Allow shadowing of `err` because it's so common - - 'error-strings: error strings should not be capitalized or end with punctuation or a newline' - max-same-issues: 10000 max-issues-per-linter: 10000 + max-same-issues: 10000 +formatters: + enable: + - gofmt + - gofumpt + - goimports + settings: + goimports: + local-prefixes: + - github.com/rubenv/sql-migrate + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/migrate.go b/migrate.go index 7fb56f1a..c9cb4a48 100644 --- a/migrate.go +++ b/migrate.go @@ -700,13 +700,14 @@ func (ms MigrationSet) planMigrationCommon(db *sql.DB, dialect string, m Migrati toApplyCount = max } for _, v := range toApply[0:toApplyCount] { - if dir == Up { + switch dir { + case Up: result = append(result, &PlannedMigration{ Migration: v, Queries: v.Up, DisableTransaction: v.DisableTransactionUp, }) - } else if dir == Down { + case Down: result = append(result, &PlannedMigration{ Migration: v, Queries: v.Down, @@ -779,14 +780,13 @@ func ToApply(migrations []*Migration, current string, direction MigrationDirecti } } - if direction == Up { + switch direction { + case Up: return migrations[index+1:] - } else if direction == Down { + case Down: if index == -1 { return []*Migration{} } - - // Add in reverse order toApply := make([]*Migration, index+1) for i := 0; i < index+1; i++ { toApply[index-i] = migrations[i] diff --git a/migrate_test.go b/migrate_test.go index f1d66d6f..3d6e80e4 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -445,31 +445,31 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) { plannedMigrations, _, err := PlanMigration(s.Db, "sqlite3", migrations, Up, 0) c.Assert(err, IsNil) c.Assert(plannedMigrations, HasLen, 3) - c.Assert(plannedMigrations[0].Migration.Id, Equals, "2") + c.Assert(plannedMigrations[0].Id, Equals, "2") c.Assert(plannedMigrations[0].Queries[0], Equals, up) - c.Assert(plannedMigrations[1].Migration.Id, Equals, "4") + c.Assert(plannedMigrations[1].Id, Equals, "4") c.Assert(plannedMigrations[1].Queries[0], Equals, up) - c.Assert(plannedMigrations[2].Migration.Id, Equals, "5") + c.Assert(plannedMigrations[2].Id, Equals, "5") c.Assert(plannedMigrations[2].Queries[0], Equals, up) // first catch up to current target state 123, then migrate down 1 step to 12 plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 1) c.Assert(err, IsNil) c.Assert(plannedMigrations, HasLen, 2) - c.Assert(plannedMigrations[0].Migration.Id, Equals, "2") + c.Assert(plannedMigrations[0].Id, Equals, "2") c.Assert(plannedMigrations[0].Queries[0], Equals, up) - c.Assert(plannedMigrations[1].Migration.Id, Equals, "3") + c.Assert(plannedMigrations[1].Id, Equals, "3") c.Assert(plannedMigrations[1].Queries[0], Equals, down) // first catch up to current target state 123, then migrate down 2 steps to 1 plannedMigrations, _, err = PlanMigration(s.Db, "sqlite3", migrations, Down, 2) c.Assert(err, IsNil) c.Assert(plannedMigrations, HasLen, 3) - c.Assert(plannedMigrations[0].Migration.Id, Equals, "2") + c.Assert(plannedMigrations[0].Id, Equals, "2") c.Assert(plannedMigrations[0].Queries[0], Equals, up) - c.Assert(plannedMigrations[1].Migration.Id, Equals, "3") + c.Assert(plannedMigrations[1].Id, Equals, "3") c.Assert(plannedMigrations[1].Queries[0], Equals, down) - c.Assert(plannedMigrations[2].Migration.Id, Equals, "2") + c.Assert(plannedMigrations[2].Id, Equals, "2") c.Assert(plannedMigrations[2].Queries[0], Equals, down) } diff --git a/sql-migrate/command_common.go b/sql-migrate/command_common.go index 30a976a5..e517c24b 100644 --- a/sql-migrate/command_common.go +++ b/sql-migrate/command_common.go @@ -62,17 +62,18 @@ func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, ver } func PrintMigration(m *migrate.PlannedMigration, dir migrate.MigrationDirection) { - if dir == migrate.Up { + switch dir { + case migrate.Up: ui.Output(fmt.Sprintf("==> Would apply migration %s (up)", m.Id)) for _, q := range m.Up { ui.Output(q) } - } else if dir == migrate.Down { + case migrate.Down: ui.Output(fmt.Sprintf("==> Would apply migration %s (down)", m.Id)) for _, q := range m.Down { ui.Output(q) } - } else { + default: panic("Not reached") } } From eef511de74e775210e74d45f59108d3bcd8b02d2 Mon Sep 17 00:00:00 2001 From: Nikita Bolotov Date: Wed, 16 Apr 2025 21:35:49 +0300 Subject: [PATCH 2/2] updated golanglint ci version --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7624377f..a6042b9c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,7 +38,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v7 with: - version: v2 + version: v2.1 - name: go mod tidy run: go mod tidy - name: check for any changes