diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index e4d73cf..bb8c6e9 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -9,7 +9,7 @@ jobs: - name: Setup Go for use with actions uses: actions/setup-go@v2 with: - go-version: 1.17 + go-version: 1.19 - uses: actions/checkout@v2 - uses: actions/cache@v2 with: diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index e7077fa..41a44c6 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -10,4 +10,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.43.0 + version: v1.50.0 diff --git a/.golangci.yml b/.golangci.yml index c907a38..19569e8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,3 +36,4 @@ issues: - path: _test\.go linters: - exhaustivestruct + - exhaustruct diff --git a/examples/1_create_users_table.go b/examples/1_create_users_table.go index dbf95e7..3f8064e 100644 --- a/examples/1_create_users_table.go +++ b/examples/1_create_users_table.go @@ -7,12 +7,17 @@ import ( ) func init() { + const ( + up = `CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)` + down = `DROP TABLE users` + ) + migrate.AddMigration( &migrate.Migration{ Name: "Create Users Table", Number: 1, Up: func(tx migrate.Tx) error { - _, err := tx.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)") + _, err := tx.Exec(up) if err != nil { return fmt.Errorf("failed to create users table: %w", err) } @@ -20,7 +25,7 @@ func init() { return nil }, Down: func(tx migrate.Tx) error { - _, err := tx.Exec("DROP TABLE users") + _, err := tx.Exec(down) if err != nil { return fmt.Errorf("failed to drop users table: %w", err) } diff --git a/examples/2_add_email_for_users.go b/examples/2_add_email_for_users.go index d33ccb7..f8339a4 100644 --- a/examples/2_add_email_for_users.go +++ b/examples/2_add_email_for_users.go @@ -7,12 +7,17 @@ import ( ) func init() { + const ( + up = `ALTER TABLE users ADD COLUMN email TEXT` + down = `ALTER TABLE users DROP COLUMN email` + ) + migrate.AddMigration( &migrate.Migration{ Name: "Add Email For Users", Number: 2, Up: func(tx migrate.Tx) error { - _, err := tx.Exec("ALTER TABLE users ADD COLUMN email TEXT") + _, err := tx.Exec(up) if err != nil { return fmt.Errorf("failed to alter users table to add email: %w", err) } @@ -20,7 +25,7 @@ func init() { return nil }, Down: func(tx migrate.Tx) error { - _, err := tx.Exec("ALTER TABLE users DROP COLUMN email") + _, err := tx.Exec(down) if err != nil { return fmt.Errorf("failed to drop email column for users table: %w", err) } diff --git a/examples/3_add_address_for_users.go b/examples/3_add_address_for_users.go index 82e6b85..c01bd35 100644 --- a/examples/3_add_address_for_users.go +++ b/examples/3_add_address_for_users.go @@ -7,12 +7,17 @@ import ( ) func init() { + const ( + up = `ALTER TABLE users ADD COLUMN address TEXT` + down = `ALTER TABLE users DROP COLUMN address` + ) + migrate.AddMigration( &migrate.Migration{ Name: "Add Address For Users", Number: 3, Up: func(tx migrate.Tx) error { - _, err := tx.Exec("ALTER TABLE users ADD COLUMN address TEXT") + _, err := tx.Exec(up) if err != nil { return fmt.Errorf("failed to alter users table to add address: %w", err) } @@ -20,7 +25,7 @@ func init() { return nil }, Down: func(tx migrate.Tx) error { - _, err := tx.Exec("ALTER TABLE users DROP COLUMN address") + _, err := tx.Exec(down) if err != nil { return fmt.Errorf("failed to drop address column for users table: %w", err) } diff --git a/go.mod b/go.mod index 674f7db..bb25634 100644 --- a/go.mod +++ b/go.mod @@ -1,28 +1,23 @@ module github.com/lawzava/go-pg-migrate/v2 -go 1.17 +go 1.19 require ( - github.com/fergusstrange/embedded-postgres v1.2.0 - github.com/golang/snappy v0.0.2 // indirect - github.com/lib/pq v1.10.4 - github.com/nwaples/rardecode v1.1.0 // indirect - github.com/pierrec/lz4 v2.6.0+incompatible // indirect - github.com/rs/zerolog v1.26.0 + github.com/fergusstrange/embedded-postgres v1.19.0 + github.com/lib/pq v1.10.7 + github.com/rs/zerolog v1.28.0 github.com/stretchr/testify v1.7.0 - github.com/ulikunitz/xz v0.5.9 // indirect ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/dsnet/compress v0.0.1 // indirect - github.com/frankban/quicktest v1.11.3 // indirect - github.com/google/go-cmp v0.5.6 // indirect - github.com/mholt/archiver v3.1.1+incompatible // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.0 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect + golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 59121bf..5e8d940 100644 --- a/go.sum +++ b/go.sum @@ -1,85 +1,73 @@ -github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= -github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= -github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= -github.com/fergusstrange/embedded-postgres v1.2.0 h1:qigL6V+YhtDBaUwoffpbyKPRg6XU5kOe4UrdJ19nrWc= -github.com/fergusstrange/embedded-postgres v1.2.0/go.mod h1:/puCGX7H8ORxgQGQd2BGfcGoS4LBtkGavfXid+5ZSs0= -github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY= -github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= +github.com/fergusstrange/embedded-postgres v1.19.0 h1:NqDufJHeA03U7biULlPHZ0pZ10/mDOMKPILEpT50Fyk= +github.com/fergusstrange/embedded-postgres v1.19.0/go.mod h1:0B+3bPsMvcNgR9nN+bdM2x9YaNYDnf3ksUqYp1OAub0= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw= -github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= -github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1wBi7mF09cbNkU= -github.com/mholt/archiver v3.1.1+incompatible/go.mod h1:Dh2dOXnSdiLxRiPoVfIr/fI1TwETms9B8CTWfeh7ROU= +github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= +github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/nwaples/rardecode v1.0.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= -github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ= -github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= -github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= -github.com/pierrec/lz4 v2.6.0+incompatible h1:Ix9yFKn1nSPBLFl/yZknTp8TU5G4Ps0JDmguYK6iH1A= -github.com/pierrec/lz4 v2.6.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE= -github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo= +github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= +github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= -github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I= -github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= -github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/migrate.go b/migrate.go index f44d3eb..ffd79f4 100644 --- a/migrate.go +++ b/migrate.go @@ -44,23 +44,19 @@ type migrationTask struct { opt Options } -// Migrate describes migration tasks. -type Migrate interface { - Migrate() error -} - -type migrate struct { +type Migrate struct { task *migrationTask } // Migrate executes actual migrations based on the specified options. -func (m migrate) Migrate() error { +func (m Migrate) Migrate() error { return m.task.migrate() } // New creates new migration instance. -// nolint:ireturn // allow returning interface -func New(opt Options) (Migrate, error) { +// + +func New(opt Options) (*Migrate, error) { if err := validateMigrations(migrations); err != nil { return nil, err } @@ -76,7 +72,7 @@ func New(opt Options) (Migrate, error) { return nil, err } - return &migrate{ + return &Migrate{ task: &migrationTask{ migrations: mapMigrations(migrations), repo: repo, @@ -85,7 +81,7 @@ func New(opt Options) (Migrate, error) { }, nil } -// migrate applies actual migrations based on the specified options. +// Migrate applies actual migrations based on the specified options. func (m *migrationTask) migrate() error { if err := m.performPreMigrationTask(); err != nil { return fmt.Errorf("failed to perform pre-migration task: %w", err) @@ -128,7 +124,7 @@ func (m *migrationTask) performPreMigrationTask() error { default: err := m.repo.EnsureMigrationTable() if err != nil { - return fmt.Errorf("failed to automatically migrate migrations table: %w", err) + return fmt.Errorf("failed to automatically Migrate migrations table: %w", err) } } @@ -167,7 +163,7 @@ func (m *migrationTask) refreshSchema(schemaName string) error { err = m.repo.EnsureMigrationTable() if err != nil { - return fmt.Errorf("failed to automatically migrate migrations table: %w", err) + return fmt.Errorf("failed to automatically Migrate migrations table: %w", err) } return nil diff --git a/migrate_test.go b/migrate_test.go index 6cb8c86..623ff7f 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -1,4 +1,4 @@ -package migrate // nolint:testpackage // allow direct tests +package migrate //nolint:testpackage // allow direct tests import ( "errors" @@ -90,13 +90,13 @@ func performMigrate(t *testing.T, options Options) error { return nil } -// nolint:funlen // allow longer function +//nolint:funlen // allow longer function func TestMigrateErrors(t *testing.T) { t.Parallel() repo := new(mockRepository) - someErr := errors.New("test-err") // nolint:goerr113 // used for tests only + someErr := errors.New("test-err") //nolint:goerr113 // used for tests only repo.On("EnsureMigrationTable").Return(someErr).Once() err := performMigrateTaskWithMigrations(t, repo, Options{}) @@ -164,7 +164,7 @@ func performMigrateTask(t *testing.T, repo repository, options Options, migratio t.Helper() options.LogInfo = func(format string, args ...interface{}) { - // nolint:forbidigo // allow in tests + //nolint:forbidigo // allow in tests fmt.Printf(format+"\n", args...) } @@ -231,11 +231,11 @@ func prepareDB() *embeddedpostgres.EmbeddedPostgres { Username("migrate"). Password("migrate"). Database("migrate"). - Version(embeddedpostgres.V13). + Version(embeddedpostgres.V14). Port(54320)) } -// nolint:funlen // allow longer function +//nolint:funlen // allow longer function func prepareMigrations() []*Migration { migrations := []*Migration{ { diff --git a/migration.go b/migration.go index 23ae959..a7c1621 100644 --- a/migration.go +++ b/migration.go @@ -20,7 +20,7 @@ type Migration struct { Down func(tx Tx) error } -// nolint:gochecknoglobals // allow global var as it's short-lived +//nolint:gochecknoglobals // allow global var as it's short-lived var migrations []*Migration type migration struct { @@ -77,7 +77,7 @@ func mapMigrations(rawMigrations []*Migration) []*migration { migrations := make([]*migration, len(rawMigrations)) for migrationIdx := range rawMigrations { - // nolint:exhaustivestruct // ID & created_at are not used + //nolint:exhaustivestruct,exhaustruct // ID & created_at are not used migrations[migrationIdx] = &migration{ Name: rawMigrations[migrationIdx].Name, Number: rawMigrations[migrationIdx].Number, diff --git a/repository.go b/repository.go index 631f0c9..f641f95 100644 --- a/repository.go +++ b/repository.go @@ -19,8 +19,7 @@ type repository interface { } type repo struct { - ctx context.Context - db *sql.DB + db *sql.DB } func newRepo(databaseURI string) (*repo, error) { @@ -29,14 +28,16 @@ func newRepo(databaseURI string) (*repo, error) { return nil, fmt.Errorf("failed to open database: %w", err) } - return &repo{context.Background(), db}, nil + return &repo{db}, nil } // GetLatestMigrationNumber returns 0,nil if not found. func (r *repo) GetLatestMigrationNumber() (uint, error) { var latestMigrationNumber uint - err := r.db.QueryRowContext(r.ctx, "SELECT number FROM migrations ORDER BY number DESC LIMIT 1"). + const query = "SELECT number FROM migrations ORDER BY number DESC LIMIT 1" + + err := r.db.QueryRowContext(context.TODO(), query). Scan(&latestMigrationNumber) if err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -75,9 +76,9 @@ func (r *repo) ApplyMigration(txFunc func(Tx) error) error { } func (r *repo) InsertMigration(m *migration) error { - _, err := r.db.ExecContext(r.ctx, - "INSERT INTO migrations (number, name) VALUES ($1, $2)", - m.Number, m.Name) + const query = "INSERT INTO migrations (number, name) VALUES ($1, $2)" + + _, err := r.db.ExecContext(context.TODO(), query, m.Number, m.Name) if err != nil { return fmt.Errorf("failed to create migration record: %w", err) } @@ -86,10 +87,9 @@ func (r *repo) InsertMigration(m *migration) error { } func (r *repo) RemoveMigrationsAfter(number uint) error { - _, err := r.db.ExecContext(r.ctx, - "DELETE FROM migrations WHERE number >= $1", - number, - ) + const query = "DELETE FROM migrations WHERE number >= $1" + + _, err := r.db.ExecContext(context.TODO(), query, number) if err != nil { return fmt.Errorf("failed to delete migrations: %w", err) } @@ -98,14 +98,16 @@ func (r *repo) RemoveMigrationsAfter(number uint) error { } func (r *repo) EnsureMigrationTable() error { - _, err := r.db.ExecContext(r.ctx, ` + const query = ` CREATE TABLE IF NOT EXISTS migrations ( id SERIAL PRIMARY KEY, created_at TIMESTAMP NOT NULL DEFAULT NOW(), number INTEGER NOT NULL UNIQUE, name VARCHAR(255) NOT NULL ) - `) + ` + + _, err := r.db.ExecContext(context.TODO(), query) if err != nil { return fmt.Errorf("failed to ensure migration table: %w", err) } @@ -114,7 +116,7 @@ func (r *repo) EnsureMigrationTable() error { } func (r *repo) DropSchema(schemaName string) error { - _, err := r.db.ExecContext(r.ctx, + _, err := r.db.ExecContext(context.TODO(), fmt.Sprintf(`DROP SCHEMA IF EXISTS %q CASCADE; CREATE SCHEMA IF NOT EXISTS %q;`, schemaName, schemaName)) if err != nil {