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.
Related
I have to run a Cron task involving some data cleaning in my ActiveRecord database. I am using Whenever gem. Here is the code :
schedule.rb
every 1.hour do
rake 'notifications:clear'
end
notifications.rake
namespace :notifications do
task clear: :environment do
Rpush::Notification.delete_all
end
end
Running this gives me the following error:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "user_prod" does not exist
I am in development environment. Here is my database.yml file :
default: &default
adapter: postgresql
encoding: unicode
# 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: development_database
test:
<<: *default
database: test_database
staging:
<<: *default
database: staging_database
username: user_staging
password: <%= ENV['DATABASE_PASSWORD'] %>
production:
<<: *default
database: production_database
username: user_prod
password: <%= ENV['DATABASE_PASSWORD'] %>
Any ideas on why my ActiveRecord instruction seems to connect to my production environment ? Thanks in advance !
Update Your code with below :-
In schedule.rb file :-
every 1.hour do
rake 'notifications:clear', :environment => "development"
end
And finally executed this command :-
whenever --update-crontab
OR
Clear existing cron jobs.
crontab -r
Update cronjob with the environment.
whenever --update-crontab --set environment='development'
have been pulling my hair due this issue :
When I run my rails application on my mac it seems the host is mistaken as database name and it only happens on development environment.
This is my database.yml :
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['DATABASE_USERNAME'] || 'admin' %>
password: <%= ENV['DATABASE_PASSWORD'] || 'password'%>
host: <%= ENV['DATABASE_URL'] || 'localhost'%>
development:
<<: *default
database: cid_dev
test:
<<: *default
database: cid_test
production:
<<: *default
database: cid_api
Then when I run :
$ bundle exec rake db:create
It returns me this :
Database 'localhost' already exists
anyone has any idea what happen in my local environment ?
FYI, I tried rbenv and rvm both has same issue.
Thank you.
I found the answer myself.
Rails using DATABASE_URL environment variable as a place to put connection string. So when I put database host inside env variable DATABASE_URL it will use it as default
https://github.com/rails/rails/blob/fb764ba63e53b728873075a0d207b993409798a2/railties/lib/rails/application/configuration.rb#L88-L102
So to fix it what I need is rename DATABASE_URL to DATABASE_HOST which is the correct one anyway. Thank you
I tried running db:migrate on my app and I'm getting this error.Not sure what is the cause.
My database is MySQL
using MySQL 64-bit connector
ruby version:ruby 2.2.6p396 (2016-11-15 revision 56800) [i386-mingw32]
I've done a google search and I'm not getting anything..Can anyone explain this error pls?
NotImplementedError: NotImplementedError
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:85:in exec_query'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:377:in 'select_prepared'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:39:inselect_all'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:95:in select_all'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/querying.rb:39:infind_by_sql'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/relation.rb:702:in `exec_queries'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rake-12.0.0/exe/rake:27:in <top (required)>'
C:/RailsInstaller/Ruby2.2.0/bin/rake:23:inload'
C:/RailsInstaller/Ruby2.2.0/bin/rake:23:in `'
Tasks: TOP => db:
migrate
You can try this..
Check your config/database.yml file.
your config/database.yml must look like and username, password should be change your mysql's username, password..
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: username
password: password
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: uBuild-rails_development
test:
<<: *default
database: uBuild-rails_test
production:
<<: *default
database: uBuild-rails_development
username: username
password: password
Also check you Gemfile
gem 'mysql2', '< 0.3' # as stated above
I had the same problem, what I did was to create the project again using this command:
rails new my_project -d=mysql
This way the configuration in database.yml and gemfile is created automatically avoiding problems. You only need to edit database.yml mysql password after this you can use
rails g scaffold Examples attrib1:string attrib2:string
to create views etc
rake db:create
to create the database and then
rake db:migrate
hope this helps...
Database file is in config/database.yml and configuration be
default: &default
adapter: mysql2 #if use postgres the add postgresql
pool: 5
timeout: 5000
username : username #mysql username
password : password #mysql password
development:
<<: *default
database: application_name
test:
<<: *default
database: application_name
production:
<<: *default
database: application_name
Run following command to setup database:
run rake db:create
run rake db:migrate
I got some problems with my rails_admin gem in the production when I trying make first user admin in Rails console.
In the development all works fine. Look at the error and code.
Error terminal:
2.3.0 :001 > u = User.first
ActiveRecord::NoDatabaseError: FATAL: database "myApp_development" does not exist
database.production.yml
default: &default
adapter: postgresql
encoding: UTF-8
pool: 5
development:
<<: *default
database: myApp_development
username: deployer
password: password
test:
<<: *default
database: myApp_test
username: deployer
password: password
production:
<<: *default
database: myApp_production
username: deployer
password: password
To run rails console in the production environment you should use bundle exec rails console production or bundle exec rails console RAILS_ENV=production, the command bundle exec rails console run console in the development env by default.
Create the database table and do
rake db:migrate RAILS_ENV=production
rails console RAILS_ENV=production
As topic says, if I do a rake db:test:prepare or rake db:test:clone no test.sqlite3 gets created. Also did a db:migrate before
Terminal dont give any output.
Thats my database.yml
sqlite: &sqlite
adapter: sqlite3
database: db/<%= Rails.env %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: myapp_<%= Rails.env %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: myapp_<%= Rails.env %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "sqlite" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
Any Ideas or suggestions?
Ah me is stupid...
had to give the rails.env, due to my database.yml with the command
RAILS_ENV=test rake db:test:prepare