Rake tasks seem to ignore database.yml configuration - ruby-on-rails

I am using MAC OS X along with postgresql installed via Homebrew. I am developing using Rails 4.2.1 and ruby 2.2.0. The connection with postgresql server is fine but for some reason every application accesses the database "kstavrou" which is my system username, as a development database and creates the rest as defined by database.yml. That is troublesome if you have more than 1 rails app.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
ecomm_intel_test already exists
strange thing is that if I empty database.yml still connects fine to postgresql and tries to create again the database "kstavrou" executing there all the migrations, without trying to create the test database.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
database.yml
default: &default
adapter: postgresql
host: localhost
encoding: utf8
username: pguser
password: 123456
pool: 5
production:
<<: *default
database: ecomm_intel_prod
development:
<<: *default
database: ecomm_intel_dev
test:
<<: *default
database: ecomm_intel_test

Well the problem was that the ENV['DATABASE_URL'] was set (by some install script) so it was overwriting the database.yml configuration, as noted by steve klein, so I just removed it.
`export DATABASE_URL=postgres:///$(whoami)`

Related

ActiveRecord connection requires reset?

An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy

How to setup a MySQL DB with rails in google could SQL

I have a rails app setup on a google cloud instance. I want to have the db in a SQL instance for the extra performance. But I cant see how to do this for a rails app.
I understand you create the SQL instance, start it, install mysql, on it but then how can I have the db and tables added? Creating them manually isn't going to be the solution because normally with rails apps you run rake db:create and rake db:migrate create the DB with tables and columns but this just makes a development.sqlite3 file not a mysql db..
I haven't deployed a rails app before so I think I'm missing something.
Here is my config/databast.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: 130.211.71.150
database: dbname
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I cant find out what needs to be done to have the db be created and tables and columns migrated into the mysql DB.
In your gem file you can put the sqlite gem under a development/test block and you can add the mysql gem in a production block.
In your database.yml file you can keep the development settings you have currently but then add another setting for production settings. Here you can include your mysql db settings (including the host and port of your SQL instance node)
When you launch your app, you can then launch it locally in development mode to use sqlite for development, but when deploying you can launch in production mode to utilize the mysql specific settings. From there you should be able to use db:create db:migrate etc to connect to that host and setup your Db.
Here is a nice article describes this process.
https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2
As a team, we chose to use mysql for local development as it more closely mimics what your prod environment will be like.

rake aborted! can't convert nil into Hash

rake aborted! Cannot load Rails.application.database_configuration: can't convert nil into Hash
I am trying to deploy ruby on rails on openshift, and then i try to run rake db:migrate. Then i face this problem.
I create the app with https://github.com/kohjx/TestYourCode.git on openshift with ruby on rails + mysql 5.1
Then i configure the config/database.yml
default: &default
#adapter: sqlite3
adapter: mysql2
pool: 5
timeout: 5000
database: "<%=ENV['OPENSHIFT_APP_NAME']%>"
username: "<%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>"
password: "<%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>"
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
development:
<<: *default
#database: db/development.sqlite3
test:
<<: *default
#database: db/test.sqlite3
database: testyourcode_test
production:
<<: *default
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
#database: db/production.sqlite3
I setup the RAILS_ENV=development, but i can't figure out how to solve. Any help would be greatly appreciated!
At first glance it looks like you're using the Openshift DB as your default and then all your Rails environments are using that default setup. Your local machine (where I'm assuming you're running the rake db:migrate) isn't able to use those connection details to the OPenshift DB.
Instead of having the Openshift DB configuration in the default section move it down to the production setting and switch the RAILS_ENV=production with rhc set-env RAILS_ENV=production. Then configure the development with settings that your localmachine can use. After thats all done, try running your db:migrate once more.

Rails with Octopus gem. How to create db's defined in shards.yml with rake

I need to configure my app to use multiple shards, and even multiple db adapters. I have noticed that all rake commands like rake db:migrate are working, and have consequence on shards defined in shards.yml, except rake db:create.
It will be a real pain to create all this manually.
How can I make it work?
My database.yml (I have defined here, only my master shard)
development:
adapter: postgresql
host: localhost
encoding: unicode
database: db_workload_master_development
pool: 5
username:
password:
production:
......
My shards.yml
octopus:
environments:
- production
- development
development:
shards:
mysql:
host: localhost
adapter: mysql2
database: db_workload_mysql_shard_development
sqlite:
host: localhost
adapter: sqlite3
database: db_workload_sqlite_shard_development
pg:
host: localhost
adapter: postgresql
database: db_workload_pg_shard_development
pool: 5
username:
password:
production:
....
Only db's from database.yml are created with rake-task rake db:create.
I would recommend creating your own rake task that will read the shards.yml and create the db's as appropriate.
I think the Octopus gem using shards rake task like db
Try rake shards:create

My cucumber tests are modifying the dev database. How do I get it to modify the test db instead?

At the top of my features/support/env.rb file:
ENV["RAILS_ENV"] = 'test'
BUt the tests still modify the development db....
Even when I do
rake cucumber:authentication RAILS_ENV=test
it still changes the dev db
What else do I need to change?
database.yml:
development:
adapter: mysql
encoding: utf8
database: app_dev
username: root
password:
test:
adapter: mysql
encoding: utf8
database: app_test
username: root
password:
production:
adapter: mysql
encoding: utf8
database: app_production
username: root
password:
cucumber: &CUCUMBER
adapter: mysql
encoding: utf8
database: app_cuke
username: root
password:
culerity:
<<: *CUCUMBER
using:
ruby 1.8.7
rails 2.3.8
and cucumber 1.0.2
Also dotenv might cause this because if there is a DATABASE_URL set in your .env file, this database will be used no matter what. This caused my tests to connect to my development database for me.
check database.yml
Probably you have specified development database for test env?
Well, basic install steps are
bundle install
rails generate cucumber:install
check database.yml, to specify right connection details
rake db:create
rake db:migrate
check cucumber
rake cucumber
what were the results of this steps?
especially have you created and migrated db for tests?
I had a similar problem and I solved it like this:
In cucumber.rake I added the rake task
task :ensure_test_env do
ENV['RAILS_ENV'] = 'test'
end
and then I made all cucumber tasks depend on it like
Cucumber::Rake::Task.new({:ok => ['db:test:prepare', :ensure_test_env]}, ...

Resources