Skip to content

Commit 86c7d37

Browse files
committed
Fix all linter errors.
1 parent b6ed53c commit 86c7d37

16 files changed

Lines changed: 126 additions & 147 deletions

bindata_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func bindata_read(data []byte, name string) ([]byte, error) {
1212
gz, err := gzip.NewReader(bytes.NewBuffer(data))
1313
if err != nil {
14-
return nil, fmt.Errorf("Read %q: %v", name, err)
14+
return nil, fmt.Errorf("Read %q: %w", name, err)
1515
}
1616

1717
var buf bytes.Buffer
@@ -21,7 +21,7 @@ func bindata_read(data []byte, name string) ([]byte, error) {
2121
}
2222

2323
if err != nil {
24-
return nil, fmt.Errorf("Read %q: %v", name, err)
24+
return nil, fmt.Errorf("Read %q: %w", name, err)
2525
}
2626

2727
return buf.Bytes(), nil
@@ -68,7 +68,7 @@ func test_migrations_2_record_sql() ([]byte, error) {
6868
// It returns an error if the asset could not be found or
6969
// could not be loaded.
7070
func Asset(name string) ([]byte, error) {
71-
canonicalName := strings.Replace(name, "\\", "/", -1)
71+
canonicalName := strings.ReplaceAll(name, "\\", "/")
7272
if f, ok := _bindata[canonicalName]; ok {
7373
return f()
7474
}
@@ -94,19 +94,21 @@ var _bindata = map[string]func() ([]byte, error){
9494
// directory embedded in the file by go-bindata.
9595
// For example if you run go-bindata on data/... and data contains the
9696
// following hierarchy:
97-
// data/
98-
// foo.txt
99-
// img/
100-
// a.png
101-
// b.png
97+
//
98+
// data/
99+
// foo.txt
100+
// img/
101+
// a.png
102+
// b.png
103+
//
102104
// then AssetDir("data") would return []string{"foo.txt", "img"}
103105
// AssetDir("data/img") would return []string{"a.png", "b.png"}
104106
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
105107
// AssetDir("") will return []string{"data"}.
106108
func AssetDir(name string) ([]string, error) {
107109
node := _bintree
108110
if len(name) != 0 {
109-
canonicalName := strings.Replace(name, "\\", "/", -1)
111+
canonicalName := strings.ReplaceAll(name, "\\", "/")
110112
pathList := strings.Split(canonicalName, "/")
111113
for _, p := range pathList {
112114
node = node.Children[p]
@@ -131,8 +133,8 @@ type _bintree_t struct {
131133
}
132134

133135
var _bintree = &_bintree_t{nil, map[string]*_bintree_t{
134-
"test-migrations": &_bintree_t{nil, map[string]*_bintree_t{
135-
"1_initial.sql": &_bintree_t{test_migrations_1_initial_sql, map[string]*_bintree_t{}},
136-
"2_record.sql": &_bintree_t{test_migrations_2_record_sql, map[string]*_bintree_t{}},
136+
"test-migrations": {nil, map[string]*_bintree_t{
137+
"1_initial.sql": {test_migrations_1_initial_sql, map[string]*_bintree_t{}},
138+
"2_record.sql": {test_migrations_2_record_sql, map[string]*_bintree_t{}},
137139
}},
138140
}}

doc.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
/*
2-
32
SQL Schema migration tool for Go.
43
54
Key features:
65
7-
* Usable as a CLI tool or as a library
8-
* Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through gorp)
9-
* Can embed migrations into your application
10-
* Migrations are defined with SQL for full flexibility
11-
* Atomic migrations
12-
* Up/down migrations to allow rollback
13-
* Supports multiple database types in one project
6+
- Usable as a CLI tool or as a library
7+
- Supports SQLite, PostgreSQL, MySQL, MSSQL and Oracle databases (through gorp)
8+
- Can embed migrations into your application
9+
- Migrations are defined with SQL for full flexibility
10+
- Atomic migrations
11+
- Up/down migrations to allow rollback
12+
- Supports multiple database types in one project
1413
15-
Installation
14+
# Installation
1615
1716
To install the library and command line program, use the following:
1817
1918
go get -v github.com/rubenv/sql-migrate/...
2019
21-
Command-line tool
20+
# Command-line tool
2221
2322
The main command is called sql-migrate.
2423
@@ -77,7 +76,7 @@ Use the status command to see the state of the applied migrations:
7776
| 2_record.sql | no |
7877
+---------------+-----------------------------------------+
7978
80-
MySQL Caveat
79+
# MySQL Caveat
8180
8281
If you are using MySQL, you must append ?parseTime=true to the datasource configuration. For example:
8382
@@ -89,7 +88,7 @@ If you are using MySQL, you must append ?parseTime=true to the datasource config
8988
9089
See https://github.com/go-sql-driver/mysql#parsetime for more information.
9190
92-
Library
91+
# Library
9392
9493
Import sql-migrate into your application:
9594
@@ -137,7 +136,7 @@ Note that n can be greater than 0 even if there is an error: any migration that
137136
138137
The full set of capabilities can be found in the API docs below.
139138
140-
Writing migrations
139+
# Writing migrations
141140
142141
Migrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations.
143142
@@ -183,7 +182,7 @@ Normally each migration is run within a transaction in order to guarantee that i
183182
-- +migrate Down
184183
DROP INDEX people_unique_id_idx;
185184
186-
Embedding migrations with packr
185+
# Embedding migrations with packr
187186
188187
If you like your Go applications self-contained (that is: a single binary): use packr (https://github.com/gobuffalo/packr) to embed the migration files.
189188
@@ -202,7 +201,7 @@ If you already have a box and would like to use a subdirectory:
202201
Dir: "./migrations",
203202
}
204203
205-
Embedding migrations with bindata
204+
# Embedding migrations with bindata
206205
207206
As an alternative, but slightly less maintained, you can use bindata (https://github.com/shuLhan/go-bindata) to embed the migration files.
208207
@@ -226,7 +225,7 @@ Both Asset and AssetDir are functions provided by bindata.
226225
227226
Then proceed as usual.
228227
229-
Extending
228+
# Extending
230229
231230
Adding a new migration source means implementing MigrationSource.
232231

migrate.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ type OracleDialect struct {
183183
gorp.OracleDialect
184184
}
185185

186-
func (d OracleDialect) IfTableNotExists(command, schema, table string) string {
186+
func (OracleDialect) IfTableNotExists(command, _, _ string) string {
187187
return command
188188
}
189189

190-
func (d OracleDialect) IfSchemaNotExists(command, schema string) string {
190+
func (OracleDialect) IfSchemaNotExists(command, _ string) string {
191191
return command
192192
}
193193

194-
func (d OracleDialect) IfTableExists(command, schema, table string) string {
194+
func (OracleDialect) IfTableExists(command, _, _ string) string {
195195
return command
196196
}
197197

@@ -287,13 +287,13 @@ func migrationFromFile(dir http.FileSystem, root string, info os.FileInfo) (*Mig
287287
path := path.Join(root, info.Name())
288288
file, err := dir.Open(path)
289289
if err != nil {
290-
return nil, fmt.Errorf("Error while opening %s: %s", info.Name(), err)
290+
return nil, fmt.Errorf("Error while opening %s: %w", info.Name(), err)
291291
}
292292
defer func() { _ = file.Close() }()
293293

294294
migration, err := ParseMigration(info.Name(), file)
295295
if err != nil {
296-
return nil, fmt.Errorf("Error while parsing %s: %s", info.Name(), err)
296+
return nil, fmt.Errorf("Error while parsing %s: %w", info.Name(), err)
297297
}
298298
return migration, nil
299299
}
@@ -407,7 +407,7 @@ func ParseMigration(id string, r io.ReadSeeker) (*Migration, error) {
407407

408408
parsed, err := sqlparse.ParseMigration(r)
409409
if err != nil {
410-
return nil, fmt.Errorf("Error parsing migration (%s): %s", id, err)
410+
return nil, fmt.Errorf("Error parsing migration (%s): %w", id, err)
411411
}
412412

413413
m.Up = parsed.UpStatements
@@ -477,7 +477,7 @@ func (ms MigrationSet) ExecVersion(db *sql.DB, dialect string, m MigrationSource
477477
}
478478

479479
// Applies the planned migrations and returns the number of applied migrations.
480-
func (ms MigrationSet) applyMigrations(dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap) (int, error) {
480+
func (MigrationSet) applyMigrations(dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap) (int, error) {
481481
applied := 0
482482
for _, migration := range migrations {
483483
var executor SqlExecutor
@@ -645,7 +645,6 @@ func (ms MigrationSet) planMigrationCommon(db *sql.DB, dialect string, m Migrati
645645
toApplyCount = max
646646
}
647647
for _, v := range toApply[0:toApplyCount] {
648-
649648
if dir == Up {
650649
result = append(result, &PlannedMigration{
651650
Migration: v,
@@ -715,7 +714,7 @@ func SkipMax(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirecti
715714

716715
// Filter a slice of migrations into ones that should be applied.
717716
func ToApply(migrations []*Migration, current string, direction MigrationDirection) []*Migration {
718-
var index = -1
717+
index := -1
719718
if current != "" {
720719
for index < len(migrations)-1 {
721720
index++
@@ -804,16 +803,14 @@ func (ms MigrationSet) getMigrationDbMap(db *sql.DB, dialect string) (*gorp.DbMa
804803
805804
Make sure that the parseTime option is supplied to your database connection.
806805
Check https://github.com/go-sql-driver/mysql#parsetime for more info.`)
807-
} else {
808-
return nil, err
809806
}
807+
return nil, err
810808
}
811809
}
812810

813811
// Create migration database map
814812
dbMap := &gorp.DbMap{Db: db, Dialect: d}
815813
table := dbMap.AddTableWithNameAndSchema(MigrationRecord{}, ms.SchemaName, ms.getTableName()).SetKeys(false, "Id")
816-
//dbMap.TraceOn("", log.New(os.Stdout, "migrate: ", log.Lmicroseconds))
817814

818815
if dialect == "oci8" || dialect == "godror" {
819816
table.ColMap("Id").SetMaxSize(4000)

migrate_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
502502
c.Assert(plannedMigrations[2].Queries[0], Equals, down)
503503
}
504504

505-
func (s *SqliteMigrateSuite) TestLess(c *C) {
505+
func (*SqliteMigrateSuite) TestLess(c *C) {
506506
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2
507507
c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1
508508
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "a"}), Equals, true) // 1 less than a
@@ -520,7 +520,6 @@ func (s *SqliteMigrateSuite) TestLess(c *C) {
520520
// 20160126_1200 not less than 20160126_1100
521521
c.Assert((Migration{Id: "20160126_1200"}).
522522
Less(&Migration{Id: "20160126_1100"}), Equals, false)
523-
524523
}
525524

526525
func (s *SqliteMigrateSuite) TestPlanMigrationWithUnknownDatabaseMigrationApplied(c *C) {
@@ -663,7 +662,7 @@ func (s *SqliteMigrateSuite) TestExecWithUnknownMigrationInDatabase(c *C) {
663662
c.Assert(n, Equals, 2)
664663

665664
// Then create a new migration source with one of the migrations missing
666-
var newSqliteMigrations = []*Migration{
665+
newSqliteMigrations := []*Migration{
667666
{
668667
Id: "124_other",
669668
Up: []string{"ALTER TABLE people ADD COLUMN middle_name text"},
@@ -742,7 +741,7 @@ func (s *SqliteMigrateSuite) TestRunMigrationObjOtherTable(c *C) {
742741
c.Assert(n, Equals, 0)
743742
}
744743

745-
func (s *SqliteMigrateSuite) TestSetDisableCreateTable(c *C) {
744+
func (*SqliteMigrateSuite) TestSetDisableCreateTable(c *C) {
746745
c.Assert(migSet.DisableCreateTable, Equals, false)
747746

748747
SetDisableCreateTable(true)

sort_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ type SortSuite struct{}
1010

1111
var _ = Suite(&SortSuite{})
1212

13-
func (s *SortSuite) TestSortMigrations(c *C) {
14-
var migrations = byId([]*Migration{
15-
&Migration{Id: "10_abc", Up: nil, Down: nil},
16-
&Migration{Id: "120_cde", Up: nil, Down: nil},
17-
&Migration{Id: "1_abc", Up: nil, Down: nil},
18-
&Migration{Id: "efg", Up: nil, Down: nil},
19-
&Migration{Id: "2_cde", Up: nil, Down: nil},
20-
&Migration{Id: "35_cde", Up: nil, Down: nil},
21-
&Migration{Id: "3_efg", Up: nil, Down: nil},
22-
&Migration{Id: "4_abc", Up: nil, Down: nil},
13+
func (*SortSuite) TestSortMigrations(c *C) {
14+
migrations := byId([]*Migration{
15+
{Id: "10_abc", Up: nil, Down: nil},
16+
{Id: "120_cde", Up: nil, Down: nil},
17+
{Id: "1_abc", Up: nil, Down: nil},
18+
{Id: "efg", Up: nil, Down: nil},
19+
{Id: "2_cde", Up: nil, Down: nil},
20+
{Id: "35_cde", Up: nil, Down: nil},
21+
{Id: "3_efg", Up: nil, Down: nil},
22+
{Id: "4_abc", Up: nil, Down: nil},
2323
})
2424

2525
sort.Sort(migrations)

sql-migrate/command_common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, version int64) error {
1010
env, err := GetEnvironment()
1111
if err != nil {
12-
return fmt.Errorf("Could not parse config: %s", err)
12+
return fmt.Errorf("Could not parse config: %w", err)
1313
}
1414

1515
db, dialect, err := GetConnection(env)
@@ -32,7 +32,7 @@ func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, ver
3232
}
3333

3434
if err != nil {
35-
return fmt.Errorf("Cannot plan migration: %s", err)
35+
return fmt.Errorf("Cannot plan migration: %w", err)
3636
}
3737

3838
for _, m := range migrations {
@@ -48,7 +48,7 @@ func ApplyMigrations(dir migrate.MigrationDirection, dryrun bool, limit int, ver
4848
}
4949

5050
if err != nil {
51-
return fmt.Errorf("Migration failed: %s", err)
51+
return fmt.Errorf("Migration failed: %w", err)
5252
}
5353

5454
if n == 1 {

sql-migrate/command_down.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
migrate "github.com/rubenv/sql-migrate"
88
)
99

10-
type DownCommand struct {
11-
}
10+
type DownCommand struct{}
1211

13-
func (c *DownCommand) Help() string {
12+
func (*DownCommand) Help() string {
1413
helpText := `
1514
Usage: sql-migrate down [options] ...
1615
@@ -28,7 +27,7 @@ Options:
2827
return strings.TrimSpace(helpText)
2928
}
3029

31-
func (c *DownCommand) Synopsis() string {
30+
func (*DownCommand) Synopsis() string {
3231
return "Undo a database migration"
3332
}
3433

sql-migrate/command_new.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ var templateContent = `
1616
1717
-- +migrate Down
1818
`
19-
var tpl *template.Template
19+
var tpl = template.Must(template.New("new_migration").Parse(templateContent))
2020

21-
func init() {
22-
tpl = template.Must(template.New("new_migration").Parse(templateContent))
23-
}
24-
25-
type NewCommand struct {
26-
}
21+
type NewCommand struct{}
2722

28-
func (c *NewCommand) Help() string {
23+
func (*NewCommand) Help() string {
2924
helpText := `
3025
Usage: sql-migrate new [options] name
3126
@@ -40,7 +35,7 @@ Options:
4035
return strings.TrimSpace(helpText)
4136
}
4237

43-
func (c *NewCommand) Synopsis() string {
38+
func (*NewCommand) Synopsis() string {
4439
return "Create a new migration"
4540
}
4641

@@ -79,7 +74,6 @@ func CreateMigration(name string) error {
7974
fileName := fmt.Sprintf("%s-%s.sql", time.Now().Format("20060102150405"), strings.TrimSpace(name))
8075
pathName := path.Join(env.Dir, fileName)
8176
f, err := os.Create(pathName)
82-
8377
if err != nil {
8478
return err
8579
}

0 commit comments

Comments
 (0)