From 81a6408ea6c8d6cd34ba13d78629766ec88d48fa Mon Sep 17 00:00:00 2001 From: Preston Skaggs Date: Thu, 19 Oct 2017 19:14:53 -0700 Subject: [PATCH 1/4] Added an entrypoint to docker-compose to run database migrations when starting docker. --- docker-compose.yml | 2 +- lib/docker-entrypoint.sh | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100755 lib/docker-entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index 46a7b2db..a7a42df6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,7 @@ version: '2' services: web: build: . + entrypoint: ["lib/docker-entrypoint.sh"] command: bundle exec puma -C config/puma.rb volumes: - .:/app @@ -41,4 +42,3 @@ services: - redis env_file: - .env - diff --git a/lib/docker-entrypoint.sh b/lib/docker-entrypoint.sh new file mode 100755 index 00000000..f0dc839d --- /dev/null +++ b/lib/docker-entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e + +exec bundle exec rake db:migrate From 4cbbe6748ce39d5f47361e4de2099d2977203d89 Mon Sep 17 00:00:00 2001 From: Preston Skaggs Date: Mon, 30 Oct 2017 20:15:58 -0700 Subject: [PATCH 2/4] Added a database check to the docker-entrypoint script, removed the 'CMD' from the Dockerfile and changed the syntax of the 'command' in docker-compose.yml --- Dockerfile | 2 -- docker-compose.yml | 2 +- lib/docker-entrypoint.sh | 4 +++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 557c686f..346a360e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,5 +17,3 @@ ENV BUNDLE_GEMFILE=$APP_HOME/Gemfile \ RUN bundle install --system ADD . $APP_HOME/ - -CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"] diff --git a/docker-compose.yml b/docker-compose.yml index a7a42df6..06279365 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: web: build: . entrypoint: ["lib/docker-entrypoint.sh"] - command: bundle exec puma -C config/puma.rb + command: ["bundle", "exec", "puma", "-C", "config/puma.rb"] volumes: - .:/app volumes_from: diff --git a/lib/docker-entrypoint.sh b/lib/docker-entrypoint.sh index f0dc839d..76bd2645 100755 --- a/lib/docker-entrypoint.sh +++ b/lib/docker-entrypoint.sh @@ -2,4 +2,6 @@ set -e -exec bundle exec rake db:migrate +bundle exec rake db:migrate || bundle exec rake db:setup + +exec bundle exec "$@" From 9ab279eedf5dbf2e342b084dd0036edc8bbd8849 Mon Sep 17 00:00:00 2001 From: Preston Skaggs Date: Tue, 31 Oct 2017 13:53:01 -0700 Subject: [PATCH 3/4] RE #61 removed the postgres timeout loop from travis.yml becuase it is already built into 'make test'. Also, removed the explicit RAILS_ENV setting from the .env file becuase order of ENV variables in Docker is shell->env_file->Dockerfile. Hence setting the RAILS_ENV in the shell was getting overridden by the .env. --- .env | 2 +- .travis.yml | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.env b/.env index d8d063bf..ebfb4136 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ COMPOSE_PROJECT_NAME=operationcodebackend REDIS_URL=redis://redis:6379/0 -RAILS_ENV=development \ No newline at end of file +#RAILS_ENV=development diff --git a/.travis.yml b/.travis.yml index 7cf89219..14c0f91b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,11 +7,6 @@ services: script: - make build - - | - while ! psql --host=localhost --username=postgres -c 'SELECT 1'> /dev/null 2>&1; do - echo 'Waiting for postgres...' - sleep 1; - done; - make test - if [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then make upgrade; fi From c7d3a5afa787ff7cf728b06484d0a0cda2b5a2fb Mon Sep 17 00:00:00 2001 From: Preston Skaggs Date: Thu, 2 Nov 2017 21:19:07 -0700 Subject: [PATCH 4/4] Added a test to check verify the test environment is using the correct database. Added some explicit environment settings in the Makfile test to ensure the test database is used --- Makefile | 2 +- test/lib/verify_db_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/lib/verify_db_test.rb diff --git a/Makefile b/Makefile index 386d39e7..7c598d6f 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ db_seed: .PHONY: test test: bg docker-compose run operationcode-psql bash -c "while ! psql --host=operationcode-psql --username=postgres -c 'SELECT 1'; do sleep 5; done;" - docker-compose run ${RAILS_CONTAINER} bash -c 'export RAILS_ENV=test && rake db:test:prepare && rake test' + docker-compose run ${RAILS_CONTAINER} bash -c 'RAILS_ENV=test bin/rails db:environment:set && rake db:test:prepare && RAILS_ENV=test bin/rails db:environment:set && rake test' .PHONY: bundle bundle: diff --git a/test/lib/verify_db_test.rb b/test/lib/verify_db_test.rb new file mode 100644 index 00000000..bb5c676b --- /dev/null +++ b/test/lib/verify_db_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class VerifyDbTest < ActiveSupport::TestCase + + test 'verify test database' do + #Check that the correct database used for testing + query = "SELECT * FROM ar_internal_metadata" + sql = ActiveRecord::Base.connection.execute(query).first + assert_equal("test", sql["value"] ) + end + +end