From 7dd1eba9428e8598a8477ca8fcc5e53622301cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Tue, 24 May 2016 04:31:34 +0200 Subject: [PATCH 1/2] Update the README / dev process explanation --- README.md | 23 ++++++++++++++++++- .../docker-compose.yml | 2 ++ docker/init.sql | 1 + main.go | 2 +- .../{001_data.down.sql => 101_data.down.sql} | 0 sqltest/{001_data.up.sql => 101_data.up.sql} | 0 6 files changed, 26 insertions(+), 2 deletions(-) rename docker-compose.yml => docker/docker-compose.yml (80%) create mode 100644 docker/init.sql rename sqltest/{001_data.down.sql => 101_data.down.sql} (100%) rename sqltest/{001_data.up.sql => 101_data.up.sql} (100%) diff --git a/README.md b/README.md index d9b8848..f33c4cf 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,29 @@ Just run the docker-compose file to create the database. docker-compose up -d ``` +## Dev -## Run the tests +### Connect to the database + +``` +docker run -it --rm --link canape_postgresql_dev:postgres postgres:9.5 psql -h postgres -U test +``` + +Then connect to the database you wanna check : + +``` +\c dev +``` + +### Apply the migrations + +``` +migrate -url 'postgres://test:test@127.0.0.1:5432/dev?sslmode=disable' -path ./sql up +migrate -url 'postgres://test:test@127.0.0.1:5432/dev?sslmode=disable' -path ./sqltest up +``` + + +### Run the tests ``` diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 80% rename from docker-compose.yml rename to docker/docker-compose.yml index 75fd8ea..0413f89 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,5 +5,7 @@ postgresql: environment: - POSTGRES_PASSWORD=test - POSTGRES_USER=test + volumes: + - .:/docker-entrypoint-initdb.d/ ports: - 127.0.0.1:5432:5432 diff --git a/docker/init.sql b/docker/init.sql new file mode 100644 index 0000000..3c2e2cb --- /dev/null +++ b/docker/init.sql @@ -0,0 +1 @@ +CREATE DATABASE dev; diff --git a/main.go b/main.go index 8762ee8..6d17083 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func (b *UserBackend) Get(username string) (auth.User, error) { func main() { log := logrus.NewEntry(logrus.New()) - pgdsn := "postgres://test:test@127.0.0.1:5432/test?sslmode=disable" + pgdsn := "postgres://test:test@127.0.0.1:5432/dev?sslmode=disable" db, err := sqlx.Connect("postgres", pgdsn) if err != nil { log.Panic(err) diff --git a/sqltest/001_data.down.sql b/sqltest/101_data.down.sql similarity index 100% rename from sqltest/001_data.down.sql rename to sqltest/101_data.down.sql diff --git a/sqltest/001_data.up.sql b/sqltest/101_data.up.sql similarity index 100% rename from sqltest/001_data.up.sql rename to sqltest/101_data.up.sql From 4afbd63b91ef9335086ba55dde47445e657a1881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Wed, 25 May 2016 18:56:19 +0200 Subject: [PATCH 2/2] Add a makefile to setup the env --- Makefile | 30 ++++++++++++++++++++++++++++++ README.md | 41 +++++++++++++++-------------------------- 2 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7c19b61 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +.PHONY: docker migration dev clean test + +DB_USER=test +DB_PASS=test +DB_DATABASE=test +DB_DSN=postgres://$(DB_USER):$(DB_PASS)@127.0.0.1:5432/$(DB_DATABASE)?sslmode=disable + +MIGRATION=migrate -url '$(DB_DSN)' +MIGRATION_SCHEMA=./sql +MIGRATION_TEST_DATA=./sqltest + +DOCKER_COMPOSE_FILE=./docker/docker-compose.yml +DOCKER_COMPOSE=docker-compose -f $(DOCKER_COMPOSE_FILE) + +docker: + $(DOCKER_COMPOSE) up -d + sleep 2 + +migration: + $(MIGRATION) -path $(MIGRATION_SCHEMA) up + $(MIGRATION) -path $(MIGRATION_TEST_DATA) up + +dev: docker migration + +clean: + $(DOCKER_COMPOSE) stop + $(DOCKER_COMPOSE) rm --force --all + +test: docker + POSTGRES_DSN="$(DB_DSN)" go test -v -p=1 ./... diff --git a/README.md b/README.md index f33c4cf..0b9ea33 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,26 @@ -## Setup the dev env - -Just run the docker-compose file to create the database. - - -``` -docker-compose up -d -``` - ## Dev -### Connect to the database +``` +make dev +``` + +To setup the dev env. + +## Connect to the database ``` docker run -it --rm --link canape_postgresql_dev:postgres postgres:9.5 psql -h postgres -U test ``` -Then connect to the database you wanna check : - -``` -\c dev -``` - -### Apply the migrations - -``` -migrate -url 'postgres://test:test@127.0.0.1:5432/dev?sslmode=disable' -path ./sql up -migrate -url 'postgres://test:test@127.0.0.1:5432/dev?sslmode=disable' -path ./sqltest up -``` - - -### Run the tests +## Run the tests ``` -POSTGRES_DSN="postgres://test:test@127.0.0.1:5432/test?sslmode=disable" go test -v -p=1 ./... +make test +``` + +## To clean up + +``` +make clean ```