Im attempting to bring up the backend on this Ruby on Rails app using docker but am getting the following error after I try to run the following commands on my ssh'd ec2 instance.
[ec2-user#ip-10-10-3-155 ~]$ docker run --rm \
>-it --name *****-db-setup \
>839014*****.dkr.ecr.us-east-1.amazonaws.com/*****-backend:dev-latest \
>bundle exec rake db:drop db:create db:migrate
rake aborted!
PG::ConnectionBad: could not translate host name "db"
to address: Name or service not known
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/usr/local/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:695:in
`connect'
/usr/local/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in
`initialize'
/usr/local/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
/usr/local/bundle/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection' /myapp/app/models/admin.rb:7:in `include'
/myapp/app/models/admin.rb:7:in `<class:Admin>'
/myapp/app/models/admin.rb:3:in `<top (required)>'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:476:in
`load' .......Tasks: TOP => db:drop =>
db:check_protected_environments => environment
this is my env file I created
$ export AWS_ACCESS_KEY_ID=***********
$ export AWS_SECRET_ACCESS_KEY=***************************
$ export AWS_DEFAULT_REGION=us-east-1
$ export DB_USER=*****
$ export DB_PASS=****test1
$ export DB_NAME=*****_backend
$ export DB_HOST=tf-*****-backend-2021051***********.c8o12l*****.us-east-1.rds.amazonaws.com
$ export DB_PORT=5432
Docker-compose.yml
version: '3'
services:
db:
image: postgres
backend:
build: backend
# https://stackoverflow.com/a/38732187/42559
entrypoint: /myapp/entrypoint.sh
command: bundle exec unicorn -p 3001
volumes:
- ./backend/:/myapp
ports:
- "3001:3001"
depends_on:
- db
env_file:
- ./.env
frontend:
build: frontend
command: yarn server
volumes:
- ./frontend/:/app
- /app/node_modules
ports:
- "3000:3000"
depends_on:
- backend
This is the Database.yml
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV.fetch("DB_HOST") { "db" } %>
username: <%= ENV.fetch("DB_USER") { "postgres" } %>
port: <%= ENV["DB_PORT"] %>
password: <%= ENV["DB_PASS"] %>
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: backend_development
test:
<<: *default
database: backend_test
production:
<<: *default
database: <%= ENV["DB_NAME"] %>
From my observations and attempts I believe it due to how the Docker-compose.yml and env file are interacting or something with my ruby server. I already have the backend image in the EC2 instance repository, now im trying to convert it to a container and bring up the backend. Thanks for any insight provided in advance !
Related
I have a rails app with docker setup. I have two containers and i have verified they are on the same network but still i am getting error
PG::ConnectionBad: connection to server at "192.168.160.2", port 5432 failed: Operation timed out Is the server running on that host and accepting TCP/IP connections?
I have changed network too, also removed all images and volumes but still no luck.
Everything was working fine until I composed down the containers and when i tried to run again make setup command i started facing the issue.
Here is my docker-compose.yml
x-toolbox-environment: &toolbox-environment
- PROJECT=frameworq
- APPLICATION=portal
- AWS_REGION
- AWS_DEFAULT_REGION
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_SESSION_TOKEN
- AWS_SECURITY_TOKEN
- AWS_SESSION_EXPIRATION
x-web-volumes: &web-volumes
- .:/app:cached
- bundle_cache:/usr/local/bundle
- rails_cache:/app/tmp/cache:cached
- public_cache:/app/public:cached
x-web-environment: &web-environment
- RAILS_PORT=4000
- MAILER_FROM=noreply#frameworq.com
- APP_DOMAIN=lvh.me
services:
web:
build: .
volumes: *web-volumes
ports:
- '4000:4000'
- '3035:3035'
environment: *web-environment
command: bash -c "bundle exec foreman s"
links:
- db
db:
image: postgres:12
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql:cached
environment:
POSTGRES_HOST_AUTH_METHOD: trust
toolbox:
image: partos/ecs_toolbox:latest
environment: *toolbox-environment
volumes:
- .:/app:cached
- /var/run/docker.sock:/var/run/docker.sock
- .aws:/root/.aws
remote:
build: ./docker/remote
environment: *toolbox-environment
entrypoint: ""
volumes:
- ./docker/remote:/app:cached
- .aws:/root/.aws
volumes:
bundle_cache:
rails_cache:
postgres:
public_cache:
localstack_data:
driver: local
database.yml
# # SQL Server (2012 or higher required)
# #
# # Install the adapters and driver
# # gem install tiny_tds
# # gem install activerecord-sqlserver-adapter
# #
# # Ensure the activerecord adapter and db driver gems are defined in your Gemfile
# # gem 'tiny_tds'
# # gem 'activerecord-sqlserver-adapter'
# #
# default: &default
# adapter: sqlserver
# encoding: utf8
# azure: false
# mode: dblib
# host: localhost
# username: sa
# password: this-is-the-kms-test-database-p2ssw0rd
# azure: &azure
# adapter: sqlserver
# encoding: utf8
# azure: true
# mode: dblib
# host: <%= Rails.application.credentials.database_host %>
# username: <%= Rails.application.credentials.database_user %>
# password: <%= Rails.application.credentials.database_password %>
# #----------
# development:
# <<: *default
# database: kmsDev
# legacy:
# <<: *azure
# database: kmsprod
# # Warning: The database defined as "test" will be erased and
# # re-generated from your development database when you run "rake".
# # Do not set this db to the same as development or production.
# test:
# <<: *default
# database: kmsTest
# production:
# <<: *azure
# database: <%= ENV['DATABASE_NAME'] %>
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV["DB_HOST"] %>
database: <%= ENV["DB_NAME"] %>
username: <%= ENV["DB_USERNAME"] %>
password: <%= ENV["DB_PASSWORD"] %>
port: <%= ENV["DB_PORT"] %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: <%= ENV.fetch("PGHOST") { 'db' } %>
database: <%= ENV.fetch("POSTGRES_DB") { 'frameworq_development' } %>
username: <%= ENV.fetch("POSTGRES_USER") { 'postgres' } %>
password: <%= ENV.fetch("POSTGRES_PASSWORD") { '' } %>
port: <%= ENV.fetch("PGPORT") { 5432 } %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: frameworq_test
host: <%= ENV.fetch("PGHOST") { 'db' } %>
database: <%= ENV.fetch("POSTGRES_DB") { 'frameworq_test' } %>
username: <%= ENV.fetch("POSTGRES_USER") { 'postgres' } %>
password: <%= ENV.fetch("POSTGRES_PASSWORD") { '' } %>
port: <%= ENV.fetch("PGPORT") { 5432 } %>
qa:
<<: *default
staging:
<<: *default
production:
<<: *default
Dockerfile
FROM ruby:3.0.4-alpine
RUN apk --update add --no-cache build-base bash git vim curl postgresql-dev openssl-dev nodejs npm yarn tzdata less \
imagemagick postgresql-client gcompat chromium chromium-chromedriver
RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle update --bundler
RUN bundle install --jobs 5
ADD . /app
RUN yarn install
RUN RAILS_SECRET_KEY_BASE=secret_key_base RAILS_ENV=production bundle exec rails assets:precompile apipie:cache
EXPOSE 5000
VOLUME ["/app/public", "/usr/local/bundle"]
CMD bash -c "bundle exec puma -C config/puma.rb"
Makefile
# DEVELOPMENT TARGETS
setup: destroy_shell build_shell prepare_shell up
destroy_shell:
docker-compose down -v
build_shell:
docker-compose build
prepare_shell:
docker-compose run --rm web bash -c "make prepare"
up:
docker-compose run --rm --service-ports --use-aliases web
shell:
docker-compose run --rm web bash
toolbox:
docker-compose run --rm toolbox bash
prepare_bundle:
bundle -j 4
yarn
prepare_db_first_time:
bundle exec rails db:create db:schema:load
prepare_db:
bundle exec rails db:migrate:with_data
prepare_test_db:
bundle exec rails db:create db:schema:load
prepare: prepare_bundle prepare_db_first_time
check: test_web fix_lint_web lint_front
lint_web:
bundle exec rubocop
fix_lint_web:
bundle exec rubocop -A
test_web:
RAILS_ENV=test bundle exec rails assets:precompile && bundle exec rails test
test_compiled_web:
RAILS_ENV=test bundle exec rails test
prepare_test_assets:
# We're not using webpacker now, importmaps instead
# cp -R public/packs public/packs-test
RAILS_ENV=test bundle exec rails assets:precompile
test_web_with_db: prepare_test_assets prepare_test_db test_compiled_web
lint_front:
# We're using importmaps, not yarn now
# yarn install && yarn run eslint --fix app/javascript/
generate_ts_routes:
docker-compose run --rm web bash -c 'bundle exec rails ts:routes'
db-reset:
bundle exec rails db:drop
bundle exec rails db:create
bundle exec rails db:schema:load
bundle exec rails db:migrate
bundle exec rails db:seed
eslint:
# We're using importmaps, not yarn now
# NODE_ENV=development yarn install
# yarn eslint --ext .js --ext .jsx /app/app/javascript/
tailwind:
docker-compose run --rm web bash -c 'bin/rails tailwindcss:watch'
guard:
docker-compose run --rm web bash -c 'bundle exec guard'
console:
docker-compose run --rm web bash -c 'rails c'
taildev:
docker-compose run --rm web bash -c 'tail -f log/development.log'
tailstaging:
docker-compose run --rm web bash -c 'tail -f log/staging.log'
I am working on rails application and it uses Docker and Kubernetes.
I do not know Docker and Kubernetes and I need to run the seed file on the production server but it's not working.
Following is my Docker file
FROM ruby:2.6.5-alpine
RUN apk update
RUN apk add bash build-base libxml2-dev libxslt-dev postgresql postgresql-dev nodejs vim
yarn libc6-compat curl
RUN mkdir /app
WORKDIR /app
COPY template-app/Gemfile* ./
RUN gem install bundler && bundle config https://gem.abc.io/xyz/ nvHuX-
OXxLY2OpiQkFVfgnYgd4ertyufdds
RUN bundle install
#RUN bundle exec rake db:seed(not working)
COPY template-app/ .
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]
In the Docker file, I have added RUN bundle exec rake db:seed but it's not working and my build is not passed(deployed)
I am using postgres database.
I do not have docker composer file.
Thanks.
Edit:
I got the following error when I write rake db:migrate in the docker file
Removing intermediate container 3634d992e850
---> 1bd575429f8e
Step 9/12 : RUN bundle exec rake db:seed
---> Running in f5d8c63f7db7
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/usr/local/bundle/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)
Edit
I have added following code in dockerfile
RUN yarn install --check-files
RUN bundle exec rake db:seed
I am able to run the seed command but get the following error
Edit
My application directory structure
Here my rails app is in template-app folder and dockerfile is outside the folder
Database.yml file
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV["RAILS_DB_POOL"] || ENV['RAILS_MAX_THREADS'] || 5 %>
idle_timeout: <%= ENV["RAILS_IDLE_CONNECTION"] || 60 %>
port: <%= ENV['RAILS_DATABASE_PORT'] || 5432 %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
development:
<<: *default
adapter: postgresql
database: <%= ENV['TEMPLATEAPP_DATABASE'] %>
username: <%= ENV['TEMPLATEAPP_DATABASE_USER'] %>
password: <%= ENV['TEMPLATEAPP_DATABASE_PASSWORD'] %>
host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>
test:
<<: *default
adapter: postgresql
database: <%= ENV['TEMPLATEAPP_DATABASE'] %>
username: <%= ENV['TEMPLATEAPP_DATABASE_USER'] %>
password: <%= ENV['TEMPLATEAPP_DATABASE_PASSWORD'] %>
host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>
production:
<<: *default
adapter: postgresql
database: <%= ENV['TEMPLATEAPP_DATABASE'] %>
username: <%= ENV['TEMPLATEAPP_DATABASE_USER'] %>
password: <%= ENV['TEMPLATEAPP_DATABASE_PASSWORD'] %>
host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>
Well there are lot of things going on in your Dockerfile, first you are installing postgresql but you are not doing its initial configuration. Normal we use docker-compose file and use pre build postgresql for it with following docker image
db:
image: postgres:11
ports:
- "5432:5432"
volumes:
- '../../tmp/data/pg11:/var/lib/postgresql/data:cached'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ''
POSTGRES_HOST_AUTH_METHOD: trust
PGDATA: /var/lib/postgresql/data/pgdata
Now another issue is you are using rails 6 mean you have to run yarn to install all packages. so I also added following line
RUN yarn install --check-files
The other issue is you are running your db migration before copying your project. you can use following Dockerfile
FROM ruby:2.6.5-alpine
RUN apk update
RUN apk add bash build-base libxml2-dev libxslt-dev postgresql postgresql-contrib postgresql-dev nodejs vim yarn libc6-compat curl
RUN mkdir /app
WORKDIR /app
COPY template-app/Gemfile* ./
RUN gem install bundler
# RUN gem install bundler && bundle config https://gem.abc.io/xyz/ nvHuX-
RUN bundle install
COPY template-app/ .
RUN yarn install --check-files
RUN bundle exec rake db:seed
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]
But as I told you due to postgresql it wont' work, till you setup postgres and take consideration using its credentials in your config/database.yml file.
Please use following format
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV["RAILS_DB_POOL"] || ENV['RAILS_MAX_THREADS'] || 5 %>
idle_timeout: <%= ENV["RAILS_IDLE_CONNECTION"] || 60 %>
port: <%= ENV['RAILS_DATABASE_PORT'] || 5432 %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
development:
<<: *default
adapter: postgresql
database: <%= ENV['TEMPLATEAPP_DATABASE'] %>
username: <%= ENV['TEMPLATEAPP_DATABASE_USER'] %>
password: <%= ENV['TEMPLATEAPP_DATABASE_PASSWORD'] %>
host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>
test:
<<: *default
adapter: postgresql
database: <%= ENV['TEMPLATEAPP_DATABASE'] %>
username: <%= ENV['TEMPLATEAPP_DATABASE_USER'] %>
password: <%= ENV['TEMPLATEAPP_DATABASE_PASSWORD'] %>
host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>
I'm new to the world of DevOps, I'm running a simple rails app on docker-compose. When I hardcode the DB credentials in the config.yml app works fine, but when I refer to the ENV variables declared in the docker-compose.yaml my app container isn't establishing a connection with DB.
Below is my config.yaml
default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: <%= ENV['DB_DATABASE']%>
host: <%= ENV['DB_HOST'] %>
user: <%= ENV['DB_USERNAME'] %>
password: <%= ['DB_PASSWORD'] %>
timeout: 5000
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
host: <%= ENV['DB_HOST'] %>
user: <%= ENV['DB_USERNAME'] %>
password: <%= ['DB_PASSWORD'] %>
socket: /var/run/mysqld/mysqld.sock
database: <%= ENV['DB_DATABASE']%>
production:
<<: *default
database: <%= ENV['DB_DATABASE']%>
host: <%= ENV['DB_HOST'] %>
user: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
socket: /var/run/mysqld/mysqld.sock
Below is my docker-compose.yaml file.
version: '3'
services:
webapp:
build: .
command: bash -c "bundle exec rails s -p 3001 -b '0.0.0.0'"
ports:
- '3001:3001'
volumes:
- '.:/data/checklist'
depends_on:
- db
environment:
DB_USERNAME: "root"
DB_PASSWORD: "Mission2019"
DB_DATABASE: "list"
DB_PORT: 3306
DB_HOST: db
RAILS_ENV: production
RAILS_MAX_THREADS: 5
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: "list"
MYSQL_ROOT_PASSWORD: "Mission2019"
MYSQL_USERNAME: "root"
MYSQL_PASSWORD: "Mission2019"
ports:
- '3307:3306'
expose:
- '3306'
I get below error
webapp_1 | ============= END WARNING FROM mysql2 =========
db_1 | 2020-07-21T07:44:59.886096Z 2 [Note] Access denied for user 'root'#'172.21.0.3' (using password: YES)
webapp_1 | I, [2020-07-21T07:44:59.887550 #1] INFO -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] Completed 500 Internal Server Error in 40ms
webapp_1 | F, [2020-07-21T07:44:59.889463 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b]
webapp_1 | F, [2020-07-21T07:44:59.889559 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] Mysql2::Error (Access denied for user 'root'#'172.21.0.3' (using password: YES)):
webapp_1 | F, [2020-07-21T07:44:59.889600 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b]
webapp_1 | F, [2020-07-21T07:44:59.889658 #1] FATAL -- : [e78ce127-bb02-45f2-be3a-91b05e564b4b] mysql2 (0.4.10) lib/mysql2/client.rb:89:in `connect'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] mysql2 (0.4.10) lib/mysql2/client.rb:89:in `initialize'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
webapp_1 | [e78ce127-bb02-45f2-be3a-91b05e564b4b] activerecord (5.2.4.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
Firstly, you're referencing database.yml file to config.yml. You don't need to create custom config file instead you can use database.yml.
Error message explains that database is accessible but password is not correct. This may due to the typo mistake in config.yml (database.yml) at line "password: <%= ['DB_PASSWORD'] %>" but this should be "password: <%= ENV['DB_PASSWORD'] %>" for default, development environment. Please verify the same for production environment as well.
I took your files modified it and it works for me for all environments. The files are as follows below.
The update database.yml file:
default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: <%= ENV['DB_DATABASE']%>
host: <%= ENV['DB_HOST'] %>
port: <%= ENV["DB_PORT"] %>
user: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
timeout: 5000
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
production:
<<: *default
The updated docker-compose.yml file:
version: '3'
services:
webapp:
build: .
command: bash -c "bundle exec rake db:migrate && bundle exec rails s -p 3001 -b '0.0.0.0'"
ports:
- '3001:3001'
volumes:
- '.:/data/checklist'
depends_on:
- db
environment:
DB_USERNAME: "root"
DB_PASSWORD: "Mission2019"
DB_DATABASE: "list"
DB_PORT: 3306
DB_HOST: db
RAILS_ENV: production
RAILS_MAX_THREADS: 5
volumes:
- .:/code
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: "list"
MYSQL_ROOT_PASSWORD: "Mission2019"
MYSQL_USERNAME: "root"
MYSQL_PASSWORD: "Mission2019"
The ports and expose key in 'docker-compose.yml' is not require if you will not be accessing the database from your host machine. Normally the value of the port should be "3306:3306" except if you won't access the database from different port from your host machine.
I got an error on gitlab during execution .gitlab-ci.yml
$ bundle exec rspec
in .gitlab-ci.yml
Locally I have postgresql for dev and test environment. All rspec tests are passing.
But after upload project on gitlab it raises error:
An error occurred while loading ./spec/requests/api/packages_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
PG::ConnectionBad:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"
Before I had sqlite3 db. But than I migrated to pg.
database.yml:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: pg_app_development
test:
<<: *default
database: pg_app_test
Add in database.yml port and host var:
test:
<<: *default
database: pg_app_test
user: postgres
host: <%= ENV.fetch("DATABASE_HOST") { 'localhost' } %>
port: 5432
And db variables to .gitlab-ci.yml
variables:
RAILS_ENV: test
POSTGRES_DB: pg_app_test
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
DATABASE_HOST: postgres
I'm trying to use Travis Continuous Integration on a Rails project. The documentation says that the test db must be configured as following for SQLite3:
test:
adapter: sqlite3
database: ":memory:"
timeout: 500
But I'd like to keep my default configuration for local tests. Is it possible to keep both my local settings and the Travis requirements?
My solution for this problem is fully based on a blog post with a few differences:
Travis CI specific settings in config/database.travis.yml;
cp config/database.travis.yml config/database.yml in before script section of .travis.yml;
I don't have config/database.yml in source tree.
Here is full listing for both files:
# .travis.yml
language: ruby
rvm:
- 1.9.3
env:
- DB=sqlite
- DB=mysql
- DB=postgresql
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- bundle exec rake
before_script:
- cp config/database.travis.yml config/database.yml
- mysql -e 'create database strano_test'
- psql -c 'create database strano_test' -U postgres
# config/database.travis.yml
sqlite: &sqlite
adapter: sqlite3
database: db/<%= Rails.env %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: strano_<%= Rails.env %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: strano_<%= Rails.env %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "postgresql" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
#mrm's blog post doesn't say anything about answering your question.
I faced the same problem where my postgreql credentials are different on my local machine than travis default. This is the simplest solution I came up with:
# config/database.yml
test:
adapter: postgresql
database: medscraper_test
username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>
Note that Travis CI automatically sets TRAVIS environment variable.
Your solution would be:
# config/database.yml
test:
adapter: sqlite3
database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
timeout: 500
I just wrote a blog post describing how to do this.