diff --git a/.golangci.yml b/.golangci.yml index 913bddb87..cb70629ab 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,6 +3,7 @@ linters: enable: - goconst - misspell + - modernize - nakedret - prealloc - revive diff --git a/database/driver_test.go b/database/driver_test.go index 7880f3208..5c3340ec7 100644 --- a/database/driver_test.go +++ b/database/driver_test.go @@ -55,7 +55,7 @@ func (m *mockDriver) Drop() error { func TestRegisterTwice(t *testing.T) { Register("mock", &mockDriver{}) - var err interface{} + var err any func() { defer func() { err = recover() diff --git a/database/neo4j/neo4j.go b/database/neo4j/neo4j.go index f6ab07a87..17f18ff89 100644 --- a/database/neo4j/neo4j.go +++ b/database/neo4j/neo4j.go @@ -146,7 +146,7 @@ func (n *Neo4j) Run(migration io.Reader) (err error) { }() if n.config.MultiStatement { - _, err = session.WriteTransaction(func(transaction neo4j.Transaction) (interface{}, error) { + _, err = session.WriteTransaction(func(transaction neo4j.Transaction) (any, error) { var stmtRunErr error if err := multistmt.Parse(migration, StatementSeparator, n.config.MultiStatementMaxSize, func(stmt []byte) bool { trimStmt := bytes.TrimSpace(stmt) @@ -194,7 +194,7 @@ func (n *Neo4j) SetVersion(version int, dirty bool) (err error) { query := fmt.Sprintf("MERGE (sm:%s {version: $version}) SET sm.dirty = $dirty, sm.ts = datetime()", n.config.MigrationsLabel) - _, err = neo4j.Collect(session.Run(query, map[string]interface{}{"version": version, "dirty": dirty})) + _, err = neo4j.Collect(session.Run(query, map[string]any{"version": version, "dirty": dirty})) if err != nil { return err } @@ -220,7 +220,7 @@ func (n *Neo4j) Version() (version int, dirty bool, err error) { query := fmt.Sprintf(`MATCH (sm:%s) RETURN sm.version AS version, sm.dirty AS dirty ORDER BY COALESCE(sm.ts, datetime({year: 0})) DESC, sm.version DESC LIMIT 1`, n.config.MigrationsLabel) - result, err := session.ReadTransaction(func(transaction neo4j.Transaction) (interface{}, error) { + result, err := session.ReadTransaction(func(transaction neo4j.Transaction) (any, error) { result, err := transaction.Run(query, nil) if err != nil { return nil, err diff --git a/database/pgx/pgx_test.go b/database/pgx/pgx_test.go index d86caeb36..1fc46cf3d 100644 --- a/database/pgx/pgx_test.go +++ b/database/pgx/pgx_test.go @@ -694,7 +694,7 @@ func TestWithInstance_Concurrent(t *testing.T) { defer wg.Wait() wg.Add(concurrency) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go func(i int) { defer wg.Done() _, err := WithInstance(db, &Config{}) diff --git a/database/pgx/v5/pgx_test.go b/database/pgx/v5/pgx_test.go index 9a8652768..548b49fb7 100644 --- a/database/pgx/v5/pgx_test.go +++ b/database/pgx/v5/pgx_test.go @@ -669,7 +669,7 @@ func TestWithInstance_Concurrent(t *testing.T) { defer wg.Wait() wg.Add(concurrency) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go func(i int) { defer wg.Done() _, err := WithInstance(db, &Config{}) diff --git a/database/postgres/postgres_test.go b/database/postgres/postgres_test.go index 3a49c50ab..060a7d14e 100644 --- a/database/postgres/postgres_test.go +++ b/database/postgres/postgres_test.go @@ -699,7 +699,7 @@ func testWithInstanceConcurrent(t *testing.T) { defer wg.Wait() wg.Add(concurrency) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go func(i int) { defer wg.Done() _, err := WithInstance(db, &Config{}) diff --git a/database/rqlite/rqlite.go b/database/rqlite/rqlite.go index fcab3f8ab..5c7c26a2d 100644 --- a/database/rqlite/rqlite.go +++ b/database/rqlite/rqlite.go @@ -186,7 +186,7 @@ func (r *Rqlite) SetVersion(version int, dirty bool) error { if version >= 0 || (version == database.NilVersion && dirty) { statements = append(statements, gorqlite.ParameterizedStatement{ Query: insertQuery, - Arguments: []interface{}{ + Arguments: []any{ version, dirty, }, diff --git a/database/spanner/spanner.go b/database/spanner/spanner.go index 914c79532..bc3dc1774 100644 --- a/database/spanner/spanner.go +++ b/database/spanner/spanner.go @@ -202,7 +202,7 @@ func (s *Spanner) SetVersion(version int, dirty bool) error { spanner.Delete(s.config.MigrationsTable, spanner.AllKeys()), spanner.Insert(s.config.MigrationsTable, []string{"Version", "Dirty"}, - []interface{}{version, dirty}, + []any{version, dirty}, )} return txn.BufferWrite(m) }) diff --git a/database/stub/stub.go b/database/stub/stub.go index 39edadea2..3c6ce8749 100644 --- a/database/stub/stub.go +++ b/database/stub/stub.go @@ -14,7 +14,7 @@ func init() { type Stub struct { Url string - Instance interface{} + Instance any CurrentVersion int MigrationSequence []string LastRunMigration []byte // todo: make []string @@ -35,7 +35,7 @@ func (s *Stub) Open(url string) (database.Driver, error) { type Config struct{} -func WithInstance(instance interface{}, config *Config) (database.Driver, error) { +func WithInstance(instance any, config *Config) (database.Driver, error) { return &Stub{ Instance: instance, CurrentVersion: database.NilVersion, diff --git a/dktesting/dktesting.go b/dktesting/dktesting.go index 6e94fc646..093a49fdb 100644 --- a/dktesting/dktesting.go +++ b/dktesting/dktesting.go @@ -45,7 +45,6 @@ func ParallelTest(t *testing.T, specs []ContainerSpec, testFunc func(*testing.T, dktest.ContainerInfo)) { for i, spec := range specs { - spec := spec // capture range variable, see https://goo.gl/60w3p2 // Only test against one version in short mode // TODO: order is random, maybe always pick first version instead? diff --git a/internal/cli/log.go b/internal/cli/log.go index b17754197..7c7174342 100644 --- a/internal/cli/log.go +++ b/internal/cli/log.go @@ -12,7 +12,7 @@ type Log struct { } // Printf prints out formatted string into a log -func (l *Log) Printf(format string, v ...interface{}) { +func (l *Log) Printf(format string, v ...any) { if l.verbose { logpkg.Printf(format, v...) } else { @@ -21,7 +21,7 @@ func (l *Log) Printf(format string, v ...interface{}) { } // Println prints out args into a log -func (l *Log) Println(args ...interface{}) { +func (l *Log) Println(args ...any) { if l.verbose { logpkg.Println(args...) } else { @@ -34,7 +34,7 @@ func (l *Log) Verbose() bool { return l.verbose } -func (l *Log) fatal(args ...interface{}) { +func (l *Log) fatal(args ...any) { l.Println(args...) os.Exit(1) } diff --git a/log.go b/log.go index cb00b7798..946d32805 100644 --- a/log.go +++ b/log.go @@ -5,7 +5,7 @@ package migrate type Logger interface { // Printf is like fmt.Printf - Printf(format string, v ...interface{}) + Printf(format string, v ...any) // Verbose should return true when verbose logging output is wanted Verbose() bool diff --git a/migrate.go b/migrate.go index 7cac0ba1b..3d6ef7ec4 100644 --- a/migrate.go +++ b/migrate.go @@ -223,7 +223,7 @@ func (m *Migrate) Migrate(version uint) error { return m.unlockErr(ErrDirty{curVersion}) } - ret := make(chan interface{}, m.PrefetchMigrations) + ret := make(chan any, m.PrefetchMigrations) go m.read(curVersion, int(version), ret) return m.unlockErr(m.runMigrations(ret)) @@ -249,7 +249,7 @@ func (m *Migrate) Steps(n int) error { return m.unlockErr(ErrDirty{curVersion}) } - ret := make(chan interface{}, m.PrefetchMigrations) + ret := make(chan any, m.PrefetchMigrations) if n > 0 { go m.readUp(curVersion, n, ret) @@ -276,7 +276,7 @@ func (m *Migrate) Up() error { return m.unlockErr(ErrDirty{curVersion}) } - ret := make(chan interface{}, m.PrefetchMigrations) + ret := make(chan any, m.PrefetchMigrations) go m.readUp(curVersion, -1, ret) return m.unlockErr(m.runMigrations(ret)) @@ -298,7 +298,7 @@ func (m *Migrate) Down() error { return m.unlockErr(ErrDirty{curVersion}) } - ret := make(chan interface{}, m.PrefetchMigrations) + ret := make(chan any, m.PrefetchMigrations) go m.readDown(curVersion, -1, ret) return m.unlockErr(m.runMigrations(ret)) } @@ -336,7 +336,7 @@ func (m *Migrate) Run(migration ...*Migration) error { return m.unlockErr(ErrDirty{curVersion}) } - ret := make(chan interface{}, m.PrefetchMigrations) + ret := make(chan any, m.PrefetchMigrations) go func() { defer close(ret) @@ -397,7 +397,7 @@ func (m *Migrate) Version() (version uint, dirty bool, err error) { // Each migration is then written to the ret channel. // If an error occurs during reading, that error is written to the ret channel, too. // Once read is done reading it will close the ret channel. -func (m *Migrate) read(from int, to int, ret chan<- interface{}) { +func (m *Migrate) read(from int, to int, ret chan<- any) { defer close(ret) // check if from version exists @@ -529,7 +529,7 @@ func (m *Migrate) read(from int, to int, ret chan<- interface{}) { // Each migration is then written to the ret channel. // If an error occurs during reading, that error is written to the ret channel, too. // Once readUp is done reading it will close the ret channel. -func (m *Migrate) readUp(from int, limit int, ret chan<- interface{}) { +func (m *Migrate) readUp(from int, limit int, ret chan<- any) { defer close(ret) // check if from version exists @@ -629,7 +629,7 @@ func (m *Migrate) readUp(from int, limit int, ret chan<- interface{}) { // Each migration is then written to the ret channel. // If an error occurs during reading, that error is written to the ret channel, too. // Once readDown is done reading it will close the ret channel. -func (m *Migrate) readDown(from int, limit int, ret chan<- interface{}) { +func (m *Migrate) readDown(from int, limit int, ret chan<- any) { defer close(ret) // check if from version exists @@ -720,7 +720,7 @@ func (m *Migrate) readDown(from int, limit int, ret chan<- interface{}) { // Before running a newly received migration it will check if it's supposed // to stop execution because it might have received a stop signal on the // GracefulStop channel. -func (m *Migrate) runMigrations(ret <-chan interface{}) error { +func (m *Migrate) runMigrations(ret <-chan any) error { for r := range ret { if m.stop() { @@ -958,14 +958,14 @@ func (m *Migrate) unlockErr(prevErr error) error { } // logPrintf writes to m.Log if not nil -func (m *Migrate) logPrintf(format string, v ...interface{}) { +func (m *Migrate) logPrintf(format string, v ...any) { if m.Log != nil { m.Log.Printf(format, v...) } } // logVerbosePrintf writes to m.Log if not nil. Use for verbose logging output. -func (m *Migrate) logVerbosePrintf(format string, v ...interface{}) { +func (m *Migrate) logVerbosePrintf(format string, v ...any) { if m.Log != nil && m.Log.Verbose() { m.Log.Printf(format, v...) } diff --git a/migrate_test.go b/migrate_test.go index 19a6d8820..992acb7b2 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -1139,7 +1139,7 @@ func TestRead(t *testing.T) { } for i, v := range tt { - ret := make(chan interface{}) + ret := make(chan any) go m.read(v.from, v.to, ret) migrations, err := migrationsFromChannel(ret) @@ -1216,7 +1216,7 @@ func TestReadUp(t *testing.T) { } for i, v := range tt { - ret := make(chan interface{}) + ret := make(chan any) go m.readUp(v.from, v.limit, ret) migrations, err := migrationsFromChannel(ret) @@ -1293,7 +1293,7 @@ func TestReadDown(t *testing.T) { } for i, v := range tt { - ret := make(chan interface{}) + ret := make(chan any) go m.readDown(v.from, v.limit, ret) migrations, err := migrationsFromChannel(ret) @@ -1319,7 +1319,7 @@ func TestLock(t *testing.T) { } } -func migrationsFromChannel(ret chan interface{}) ([]*Migration, error) { +func migrationsFromChannel(ret chan any) ([]*Migration, error) { slice := make([]*Migration, 0) for r := range ret { switch t := r.(type) { @@ -1391,7 +1391,7 @@ func equalMigSeq(t *testing.T, i int, expected, got migrationSequence) { t.Errorf("expected migrations %v, got %v, in %v", expected, got, i) } else { - for ii := 0; ii < len(expected); ii++ { + for ii := range expected { if expected[ii].Version != got[ii].Version { t.Errorf("expected version %v, got %v, in %v", expected[ii].Version, got[ii].Version, i) } diff --git a/source/file/file_test.go b/source/file/file_test.go index 5680aa2a3..b860ef189 100644 --- a/source/file/file_test.go +++ b/source/file/file_test.go @@ -156,7 +156,7 @@ func mustWriteFile(t testing.TB, dir, file string, body string) { func mustCreateBenchmarkDir(t *testing.B) (dir string) { tmpDir := t.TempDir() - for i := 0; i < 1000; i++ { + for i := range 1000 { mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.up.sql", i), "") mustWriteFile(t, tmpDir, fmt.Sprintf("%v_foobar.down.sql", i), "") } @@ -171,8 +171,8 @@ func BenchmarkOpen(b *testing.B) { b.Error(err) } }() - b.ResetTimer() - for n := 0; n < b.N; n++ { + + for b.Loop() { f := &File{} _, err := f.Open(scheme + dir) if err != nil { @@ -191,9 +191,9 @@ func BenchmarkNext(b *testing.B) { }() f := &File{} d, _ := f.Open(scheme + dir) - b.ResetTimer() + v, err := d.First() - for n := 0; n < b.N; n++ { + for b.Loop() { for !errors.Is(err, os.ErrNotExist) { v, err = d.Next(v) } diff --git a/source/go_bindata/go-bindata.go b/source/go_bindata/go-bindata.go index d0d42f5af..4fbc01d59 100644 --- a/source/go_bindata/go-bindata.go +++ b/source/go_bindata/go-bindata.go @@ -41,7 +41,7 @@ var ( ErrNoAssetSource = fmt.Errorf("expects *AssetSource") ) -func WithInstance(instance interface{}) (source.Driver, error) { +func WithInstance(instance any) (source.Driver, error) { if _, ok := instance.(*AssetSource); !ok { return nil, ErrNoAssetSource } diff --git a/source/migration.go b/source/migration.go index 74f6523cb..5ce82ff28 100644 --- a/source/migration.go +++ b/source/migration.go @@ -1,6 +1,7 @@ package source import ( + "slices" "sort" ) @@ -70,9 +71,7 @@ func (i *Migrations) buildIndex() { for version := range i.migrations { i.index = append(i.index, version) } - sort.Slice(i.index, func(x, y int) bool { - return i.index[x] < i.index[y] - }) + slices.Sort(i.index) } func (i *Migrations) First() (version uint, ok bool) { diff --git a/source/stub/stub.go b/source/stub/stub.go index ad2620ff7..cea9f6a9c 100644 --- a/source/stub/stub.go +++ b/source/stub/stub.go @@ -20,7 +20,7 @@ type Config struct{} type Stub struct { Url string - Instance interface{} + Instance any Migrations *source.Migrations Config *Config } @@ -33,7 +33,7 @@ func (s *Stub) Open(url string) (source.Driver, error) { }, nil } -func WithInstance(instance interface{}, config *Config) (source.Driver, error) { +func WithInstance(instance any, config *Config) (source.Driver, error) { return &Stub{ Instance: instance, Migrations: source.NewMigrations(), diff --git a/testing/testing.go b/testing/testing.go index 164d9ef6c..8e8a4fe3f 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -27,7 +27,6 @@ func ParallelTest(t *testing.T, versions []Version, readyFn IsReadyFunc, testFn } for i, version := range versions { - version := version // capture range variable, see https://goo.gl/60w3p2 // Only test against one version in short mode // TODO: order is random, maybe always pick first version instead?