Simplify Makefile and docker usage (#24)

This commit is contained in:
Adrian Macneil 2018-01-22 21:49:12 -08:00 committed by GitHub
parent 2b8b849626
commit c2fc8b5441
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 35 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.DS_Store
/dist
/testdata/.env
/testdata/db/schema.sql

View file

@ -4,7 +4,11 @@ services:
install:
- docker version
- docker-compose version
- docker-compose pull
- docker-compose build
- docker-compose up -d
script:
- make
- docker-compose run --rm dbmate make test
- docker-compose run --rm dbmate make lint
- docker build -t dbmate .
- docker run --rm dbmate --help

View file

@ -18,13 +18,15 @@ RUN go get \
github.com/kisielk/errcheck
# copy source files
COPY . $GOPATH/src/github.com/amacneil/dbmate
WORKDIR $GOPATH/src/github.com/amacneil/dbmate
COPY . /go/src/github.com/amacneil/dbmate
WORKDIR /go/src/github.com/amacneil/dbmate
# build
RUN go build -ldflags '-s' -o /go/bin/dbmate ./cmd/dbmate
RUN make install build
# runtime image
FROM debian:stretch-slim
COPY --from=build /go/bin/dbmate /usr/local/bin/dbmate
ENTRYPOINT ["dbmate"]
COPY --from=build /go/src/github.com/amacneil/dbmate/dist/dbmate-linux-amd64 \
/usr/local/bin/dbmate
WORKDIR /app
ENTRYPOINT ["/usr/local/bin/dbmate"]

View file

@ -2,25 +2,35 @@ DC := docker-compose
BUILD_FLAGS := -ldflags '-s'
PACKAGES := ./cmd/... ./pkg/...
all: clean container test lint build
.PHONY: all
all: install test lint build
.PHONY: install
install:
go install -v $(PACKAGES)
.PHONY: test
test:
go test -v $(PACKAGES)
.PHONY: lint
lint:
golint -set_exit_status $(PACKAGES)
go vet $(PACKAGES)
errcheck $(PACKAGES)
.PHONY: clean
clean:
rm -rf dist
container:
.PHONY: build
build: clean
GOARCH=amd64 go build $(BUILD_FLAGS) -o dist/dbmate-linux-amd64 ./cmd/dbmate
# musl target does not support sqlite
GOARCH=amd64 CGO_ENABLED=0 go build $(BUILD_FLAGS) -o dist/dbmate-linux-musl-amd64 ./cmd/dbmate
.PHONY: docker
docker:
$(DC) pull
$(DC) build
$(DC) up -d
lint:
$(DC) run --rm dbmate golint -set_exit_status $(PACKAGES)
$(DC) run --rm dbmate go vet $(PACKAGES)
$(DC) run --rm dbmate errcheck $(PACKAGES)
test:
$(DC) run --rm dbmate go test -v $(PACKAGES)
build: clean
$(DC) run --rm -e GOARCH=amd64 dbmate go build $(BUILD_FLAGS) -o dist/dbmate-linux-amd64 ./cmd/dbmate
# musl target does not support sqlite
$(DC) run --rm -e GOARCH=amd64 -e CGO_ENABLED=0 dbmate go build $(BUILD_FLAGS) -o dist/dbmate-linux-musl-amd64 ./cmd/dbmate
$(DC) run --rm dbmate make

View file

@ -256,7 +256,7 @@ Alpine linux uses [musl libc](https://www.musl-libc.org/), which is incompatible
## Alternatives
Why another database schema migration tool? Dbmate was inspired by many other tools, primarily [Rails' ActiveRecord](http://guides.rubyonrails.org/active_record_migrations.html), with the goals of being trivial to configure, and language & framework independent. Here is a comparison between dbmate and other popular migration tools.
Why another database schema migration tool? Dbmate was inspired by many other tools, primarily [Active Record Migrations](http://guides.rubyonrails.org/active_record_migrations.html), with the goals of being trivial to configure, and language & framework independent. Here is a comparison between dbmate and other popular migration tools.
| | [goose](https://bitbucket.org/liamstask/goose/) | [sql-migrate](https://github.com/rubenv/sql-migrate) | [mattes/migrate](https://github.com/mattes/migrate) | [activerecord](http://guides.rubyonrails.org/active_record_migrations.html) | [sequelize](http://docs.sequelizejs.com/manual/tutorial/migrations.html) | [dbmate](https://github.com/amacneil/dbmate) |
| --- |:---:|:---:|:---:|:---:|:---:|:---:|
@ -282,22 +282,14 @@ Why another database schema migration tool? Dbmate was inspired by many other to
Dbmate is written in Go, pull requests are welcome.
Tests are run against a real database using docker-compose. First, install the [Docker Toolbox](https://www.docker.com/docker-toolbox).
Make sure you have docker running:
Tests are run against a real database using docker-compose. To build a docker image and run the tests:
```sh
$ docker-machine start default && eval "$(docker-machine env default)"
$ make docker
```
To build a docker image and run the tests:
To start a development shell:
```sh
$ make
```
To run just the lint and tests (without completely rebuilding the docker image):
```sh
$ make lint test
$ docker-compose run --rm dbmate bash
```