From b9d70d93826f439a60732a497228779520b42e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B3pez?= Date: Fri, 4 Mar 2022 09:34:30 +0100 Subject: [PATCH] *: bump versions, use embed - Update go version to 1.17 - Update golangci-lint version to 1.44.2 - Use //go:embed instead of an external embedding tool - Migrate from DoneCI to Github actions --- .drone.yml | 51 -------- .github/workflows/main.yml | 34 ++++++ .golangci.yml | 2 +- Dockerfile.build | 5 +- Makefile | 14 +-- README.md | 4 +- docker-compose.yaml | 11 +- go.mod | 15 ++- go.sum | 5 - migrator_test.go | 12 +- sql.go | 235 ------------------------------------- 11 files changed, 69 insertions(+), 319 deletions(-) delete mode 100644 .drone.yml create mode 100644 .github/workflows/main.yml delete mode 100644 sql.go diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index d735f43..0000000 --- a/.drone.yml +++ /dev/null @@ -1,51 +0,0 @@ -kind: pipeline -name: migrator - -workspace: - base: /go - path: src/github.com/lopezator/migrator - -steps: - - name: prepare - image: golang:1.15.2 - commands: - - make prepare - - - name: sanity-check - image: golang:1.15.2 - commands: - - make sanity-check - - - name: test - image: golang:1.15.2 - environment: - POSTGRES_URL: postgres://postgres@postgres:5432/migrator?sslmode=disable - MYSQL_URL: root:mysql@tcp(mysql:3306)/migrator - commands: - - sleep 30 - - make test - - - name: coverage - image: plugins/codecov - settings: - files: - - ./coverage.txt - environment: - CODECOV_TOKEN: - from_secret: codecov_token - -services: - - name: postgres - image: postgres:11.2 - ports: - - 5432 - environment: - POSTGRES_DB: migrator - - - name: mysql - image: mysql:8.0.15 - ports: - - 3306 - environment: - MYSQL_DATABASE: migrator - MYSQL_ROOT_PASSWORD: mysql diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..74a0604 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,34 @@ +name: "migrator build" + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: prepare + run: | + docker compose up -d --build + docker compose exec -T migrator make prepare + - name: sanity-check + run: docker compose exec -T migrator make sanity-check + - name: test + env: + POSTGRES_URL: postgres://postgres:migrator@postgres:5432/migrator?sslmode=disable + MYSQL_URL: root:migrator@tcp(mysql:3306)/migrator + run: | + docker compose exec -T migrator make test POSTGRES_URL="${POSTGRES_URL}" MYSQL_URL="${MYSQL_URL}" + docker compose cp migrator:/go/src/github.com/lopezator/migrator/coverage.txt . + - name: coverage + uses: codecov/codecov-action@v2 + with: + files: ./coverage.txt + - name: clean + run: docker compose down -v diff --git a/.golangci.yml b/.golangci.yml index 37ae27b..783381a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,7 +3,7 @@ linters: - errcheck - gocritic - goimports - - golint + - revive - govet - megacheck diff --git a/Dockerfile.build b/Dockerfile.build index 6a45cc9..de9228d 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -1,12 +1,11 @@ -FROM golang:1.11.5-stretch +FROM golang:1.17 ENV OS linux -ENV GO111MODULE on ENV PKGPATH github.com/lopezator/migrator ENV CGO_ENABLED 0 # golangci-lint -RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.15.0 +RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.44.2 # copy current workspace WORKDIR ${GOPATH}/src/${PKGPATH} diff --git a/Makefile b/Makefile index 381de54..ab8cfdb 100644 --- a/Makefile +++ b/Makefile @@ -2,18 +2,12 @@ SHELL = /bin/bash -GOPROXY = https://proxy.golang.org -POSTGRES_URL = postgres://postgres@postgres:5432/migrator?sslmode=disable -MYSQL_URL = root:mysql@tcp(mysql:3306)/migrator +POSTGRES_URL = postgres://postgres:migrator@postgres:5432/migrator?sslmode=disable +MYSQL_URL = root:migrator@tcp(mysql:3306)/migrator .PHONY: setup-env setup-env: - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b . v1.31.0 - GO111MODULE=off go get -u github.com/mjibson/esc - -.PHONY: esc-gen -esc-gen: - esc -pkg migrator -private -include=".*.sql$$" -modtime="0" testdata > sql.go + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b . v1.44.2 .PHONY: prepare prepare: setup-env mod-download @@ -21,7 +15,7 @@ prepare: setup-env mod-download .PHONY: mod-download mod-download: @echo "Running download..." - GOPROXY="$(GOPROXY)" go mod download + go mod download .PHONY: sanity-check sanity-check: golangci-lint diff --git a/README.md b/README.md index 373a7db..d83d827 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Dead simple Go database migration library. * Usage as a library, embeddable and extensible on your behalf * Support of any database supported by `database/sql` * Go code migrations, either transactional or transaction-less, using `*sql.Tx` (`migrator.Migration`) or `*sql.DB` (`migrator.MigrationNoTx`) -* No need to use `packr`, `gobin` or others, since all migrations are just Go code +* No need to use `//go:embed` or others, since all migrations are just Go code # Compatibility @@ -106,7 +106,7 @@ Just examine the [migrator_test.go](migrator_test.go) file. You still can use your favorite embedding tool to write your migrations inside `.sql` files and load them into migrator! -I provide a simple example using [esc](https://github.com/mjibson/esc) on the `Using tx, one embedded query` test here: [migrator_test](https://github.com/lopezator/migrator/blob/master/migrator_test.go) +I provide a simple example using `//go:embed` on the `Using tx, one embedded query` test here: [migrator_test](https://github.com/lopezator/migrator/blob/master/migrator_test.go) ### Erm... Where are the ID's of the migrations to know their order? 🤔 diff --git a/docker-compose.yaml b/docker-compose.yaml index f4fa045..1b24ae3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,16 +12,17 @@ services: tty: true postgres: - image: postgres:11.2 + image: postgres:14.2 environment: POSTGRES_DB: migrator + POSTGRES_PASSWORD: migrator ports: - - 2345:5432 + - "2345:5432" mysql: - image: mysql:8.0.15 + image: mysql:8.0.28 environment: MYSQL_DATABASE: migrator - MYSQL_ROOT_PASSWORD: mysql + MYSQL_ROOT_PASSWORD: migrator ports: - - 6033:3306 + - "6033:3306" diff --git a/go.mod b/go.mod index c42a596..3854a38 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,22 @@ module github.com/lopezator/migrator -go 1.15 +go 1.17 require ( github.com/go-sql-driver/mysql v1.4.1 github.com/jackc/pgx/v4 v4.9.0 +) + +require ( + github.com/jackc/chunkreader/v2 v2.0.1 // indirect + github.com/jackc/pgconn v1.7.0 // indirect + github.com/jackc/pgio v1.0.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgproto3/v2 v2.0.5 // indirect + github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect + github.com/jackc/pgtype v1.5.0 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect + golang.org/x/text v0.3.3 // indirect + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect google.golang.org/appengine v1.4.0 // indirect ) diff --git a/go.sum b/go.sum index 8563fdc..f341727 100644 --- a/go.sum +++ b/go.sum @@ -69,13 +69,10 @@ github.com/jackc/puddle v1.1.2/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 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/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= -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.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -146,7 +143,6 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= @@ -166,7 +162,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= diff --git a/migrator_test.go b/migrator_test.go index ab51a42..e312275 100644 --- a/migrator_test.go +++ b/migrator_test.go @@ -1,9 +1,10 @@ -// +build integration +//go:build integration package migrator import ( "database/sql" + _ "embed" "fmt" "log" "os" @@ -13,6 +14,9 @@ import ( _ "github.com/jackc/pgx/v4/stdlib" // postgres driver ) +//go:embed testdata/0_bar.sql +var mig0bar string + var migrations = []interface{}{ &Migration{ Name: "Using tx, encapsulate two queries", @@ -38,11 +42,7 @@ var migrations = []interface{}{ &Migration{ Name: "Using tx, one embedded query", Func: func(tx *sql.Tx) error { - query, err := _escFSString(false, "/testdata/0_bar.sql") - if err != nil { - return err - } - if _, err := tx.Exec(query); err != nil { + if _, err := tx.Exec(mig0bar); err != nil { return err } return nil diff --git a/sql.go b/sql.go deleted file mode 100644 index 0ae10db..0000000 --- a/sql.go +++ /dev/null @@ -1,235 +0,0 @@ -// Code generated by "esc -pkg migrator -private -include=.*.sql$ -modtime=0 testdata"; DO NOT EDIT. - -package migrator - -import ( - "bytes" - "compress/gzip" - "encoding/base64" - "fmt" - "io" - "io/ioutil" - "net/http" - "os" - "path" - "sync" - "time" -) - -type _escLocalFS struct{} - -var _escLocal _escLocalFS - -type _escStaticFS struct{} - -var _escStatic _escStaticFS - -type _escDirectory struct { - fs http.FileSystem - name string -} - -type _escFile struct { - compressed string - size int64 - modtime int64 - local string - isDir bool - - once sync.Once - data []byte - name string -} - -func (_escLocalFS) Open(name string) (http.File, error) { - f, present := _escData[path.Clean(name)] - if !present { - return nil, os.ErrNotExist - } - return os.Open(f.local) -} - -func (_escStaticFS) prepare(name string) (*_escFile, error) { - f, present := _escData[path.Clean(name)] - if !present { - return nil, os.ErrNotExist - } - var err error - f.once.Do(func() { - f.name = path.Base(name) - if f.size == 0 { - return - } - var gr *gzip.Reader - b64 := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(f.compressed)) - gr, err = gzip.NewReader(b64) - if err != nil { - return - } - f.data, err = ioutil.ReadAll(gr) - }) - if err != nil { - return nil, err - } - return f, nil -} - -func (fs _escStaticFS) Open(name string) (http.File, error) { - f, err := fs.prepare(name) - if err != nil { - return nil, err - } - return f.File() -} - -func (dir _escDirectory) Open(name string) (http.File, error) { - return dir.fs.Open(dir.name + name) -} - -func (f *_escFile) File() (http.File, error) { - type httpFile struct { - *bytes.Reader - *_escFile - } - return &httpFile{ - Reader: bytes.NewReader(f.data), - _escFile: f, - }, nil -} - -func (f *_escFile) Close() error { - return nil -} - -func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) { - if !f.isDir { - return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name) - } - - fis, ok := _escDirs[f.local] - if !ok { - return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local) - } - limit := count - if count <= 0 || limit > len(fis) { - limit = len(fis) - } - - if len(fis) == 0 && count > 0 { - return nil, io.EOF - } - - return fis[0:limit], nil -} - -func (f *_escFile) Stat() (os.FileInfo, error) { - return f, nil -} - -func (f *_escFile) Name() string { - return f.name -} - -func (f *_escFile) Size() int64 { - return f.size -} - -func (f *_escFile) Mode() os.FileMode { - return 0 -} - -func (f *_escFile) ModTime() time.Time { - return time.Unix(f.modtime, 0) -} - -func (f *_escFile) IsDir() bool { - return f.isDir -} - -func (f *_escFile) Sys() interface{} { - return f -} - -// _escFS returns a http.Filesystem for the embedded assets. If useLocal is true, -// the filesystem's contents are instead used. -func _escFS(useLocal bool) http.FileSystem { - if useLocal { - return _escLocal - } - return _escStatic -} - -// _escDir returns a http.Filesystem for the embedded assets on a given prefix dir. -// If useLocal is true, the filesystem's contents are instead used. -func _escDir(useLocal bool, name string) http.FileSystem { - if useLocal { - return _escDirectory{fs: _escLocal, name: name} - } - return _escDirectory{fs: _escStatic, name: name} -} - -// _escFSByte returns the named file from the embedded assets. If useLocal is -// true, the filesystem's contents are instead used. -func _escFSByte(useLocal bool, name string) ([]byte, error) { - if useLocal { - f, err := _escLocal.Open(name) - if err != nil { - return nil, err - } - b, err := ioutil.ReadAll(f) - _ = f.Close() - return b, err - } - f, err := _escStatic.prepare(name) - if err != nil { - return nil, err - } - return f.data, nil -} - -// _escFSMustByte is the same as _escFSByte, but panics if name is not present. -func _escFSMustByte(useLocal bool, name string) []byte { - b, err := _escFSByte(useLocal, name) - if err != nil { - panic(err) - } - return b -} - -// _escFSString is the string version of _escFSByte. -func _escFSString(useLocal bool, name string) (string, error) { - b, err := _escFSByte(useLocal, name) - return string(b), err -} - -// _escFSMustString is the string version of _escFSMustByte. -func _escFSMustString(useLocal bool, name string) string { - return string(_escFSMustByte(useLocal, name)) -} - -var _escData = map[string]*_escFile{ - - "/testdata/0_bar.sql": { - name: "0_bar.sql", - local: "testdata/0_bar.sql", - size: 37, - modtime: 0, - compressed: ` -H4sIAAAAAAAC/3IOcnUMcVUIcXTycVVISixS0MhMUfD0C1EICPL0dQyKVPB2jdQEBAAA//9fvWH7JQAA -AA== -`, - }, - - "/testdata": { - name: "testdata", - local: `testdata`, - isDir: true, - }, -} - -var _escDirs = map[string][]os.FileInfo{ - - "testdata": { - _escData["/testdata/0_bar.sql"], - }, -}