Ruby on rails rake db:create already exists - ruby-on-rails-3

I am trying to create a database using:
rake db:create
That output a error: db already exists.
When I do a
rake db:drop
The output error is: db does not exist.
I have use external to connect to the DB server and indeed the DB does not exist.
Any help is much appreciated.

Related

Rails db:seed unknown attribute

Here is a question.
I have an Rails project. When I want to clear my database and fill it with some test data I run:
rake db:drop db:create db:migrate db:seed
and I have an error:
NoMethodError: undefined method login for #<User:0x007fecf46afe80>
When I run separately:
rake db:drop db:create db:migrate
rake db:seed
all goes fine.
Also all my actions in db/seeds.rb are wrapped in ActiveRecord::Base.transaction block.
I had to add User.reset_column_information in the top of my db/seeds.rb to make
rake db:drop db:create db:migrate db:seed
working.
I didn't have the same errors before without reset_column_information. Does somebody have any ideas why it's happen?
PS: after running rake db:drop db:create db:migrate there is "missing" column in db/schema.rb and I can see this column in DB directly
From the docs:
reset_column_information() public
Resets all the cached information
about columns, which will cause them to be reloaded on the next
request.
The most common usage pattern for this method is probably in a
migration, when just after creating a table you want to populate it
with some default values
You're db:migrate task changes the column information in the users table, but it looks like these changes are not properly written to the db/schema.rb file until the rake cammand finishes. Your db:seed task looks at db/schema.rb to see if the columns it needs exists, but this shows the schema as it was before the db:migrate unless you put it in a separate rake command or you run reset_column_information() before it.

How to check SQL commands executed using rake in Ruby on Rails

I want to count the votes a user has given to an article and save it somewhere.
I want to check all the SQL INSERT or CREATE lines executed when we do something like:
>$ bundle exec rake db:reset
>$ bundle exec rake db:seed
>$ bundle exec rake test:prepare
Is there a way I can check the SQL commands in Ruby on Rails?
You can add a custom Rake task and use it whenever you need to log the SQL output:
task log: :environment do
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
Now you can run:
bundle exec rake log db:reset
bundle exec rake log db:seed
bundle exec rake log test:prepare
See "Is it possible to output the SQL change scripts that 'rake db:migrate' produces?"

Issues with setting PostgreSQL up and db:rake

I've had numerous issues with setting up Postresql for a project I'm working on.
Some information:
-I'm running Ubuntu 12.04
-I have a postgres user account installed
-I'm trying to use a project in RoR.
I'm trying to use this command:
"bundle exec rake db:create"
I tried using it as sudo, computer user, and postgres.
When I run it as my computer user it returns "fe_sendauth: no password supplied" and then aborts the rake.
I know my (user account) ruby version is 2.1.0 because when I run "ruby --version", it returns
"ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]"
However, when I run "ruby --version" as postgres, it returns
"ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]"
How can I have separate ruby versions for my user account and postgres? I tried using sudo to change the ruby version with RVM, but I was denied permission.
I don't remember setting the password for postgres sudo, and I cannot figure it out.
What can I do to
Recover/reset my sudo postgres password.
Install/set the newest (2.1.0) version of ruby on the postgres account once I have done step 1.
Run bundle exec rake db:create after all of that?
Hm, not sure any specific issues with linux, but it sounds like you have it installed okay.
Hard to say without looking at your database.yml, but the rails convention seems to be to connect to the database with the username <%= your_project_name %> and a blank password. Based on the error, you may not have created such a user/role in postgres.
# enter the psql shell as the postgres admin user
psql -d postgres
CREATE ROLE <%= your_project_name %> WITH LOGIN CREATEDB SUPERUSER;
\q
# create the dbs and load the schema
bundle exec rake db:create
bundle exec rake db:schema:load
bundle exec rake db:migrate
bundle exec rake db:test:prepare
# revoke superuser, it was just there in case there were any extensions that needed installing
psql -d postgres
ALTER ROLE <%= your_project_name %> WITH NOSUPERUSER;
\q
That should do that trick, although it sounds like you might be having issues with RVM as well. There is a ton of documentation on RVM on the website.
You also may be confusing the concept of users/roles in the DB with the concept of users in the OS. You should be able to access the database with any unix user but you need to authenticate rails into the database using database.yml. As noted above, there seem to be defaults that most rails projects i've seen use, but of course you could use whatever you want.
Ok, so I fixed my main issue with my ruby version being incorrect and not being able to fix it because I did not have authentication.
I did this with
$ /bin/bash --login
This let me then type
$ rvm --default use 2.1.0
This set my ruby version in user postgres to 2.1.0.
However, I then tried to run
$ bundle install
in user postgres, and I was denied and given this error
Unfortunately, a fatal error has occurred. Please see the Bundler troubleshooting documentation at
http://bit.ly/bundler-issues. Thanks!
/home/myusername/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/fileutils.rb:250:in `mkdir': Permission denied # dir_s_mkdir - /home/myusername/.bundler (Errno::EACCES)
I cannot access sudo for user postgres because I did not set the password for it. therefore, I believe that the answer to my question is to run
$ sudo bundle install
but I do not have the sudo password.
Any help with this issue would be appreciated.

I get a "database does not exist error" when running unit tests

Every time I run the command:
rake test test/models/post_test.rb test_the_truth
I get the following error:
`initialize': FATAL: database "knome_test" does not exist (PG::Error).
Can someone explain and help me to debug it?
It sounds like you have not created the database.
Try running the following to create, migrate, then prepare your test database.
rake db:create
rake db:migrate
rake db:test:prepare
You have create a new database in postgres and update database.yml "test" section with that db_name, username & password. The same way you have done in "development" section.

Rails 3, rake db:test:prepare doesn't work when using a postgres Schema

I have a Rails 3.2.1 project that I'm trying to run my specs on.
When I run:
rake
rake aborted! PG::Error: ERROR: invalid value for parameter
"search_path": "example" DETAIL: schema "example" does not exist :
SET search_path TO example
From what I can work out, rake db:test:prepare drops the test database and then attempts to recreate it from the schema.rb. The thing is, database.yml has the line
schema_search_path: example
so when it tries to reconnect, it fails with the above error.
I think my question is, how can I get rake db:test:prepare to setup the schema_path too?
One solution is to add a bit of code to the db:create task that creates the schema. Put this in a rake task, for exmaple 'APP_ROOT/lib/tasks/db_create_schema.rake'
namespace :db do
task :create do
config = Rails.configuration.database_configuration[Rails.env].merge!({'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("CREATE SCHEMA schema_name")
end
end
https://gist.github.com/4280172
You should set the schema_search_path in your config/database.yml
e.g.
development:
adapter: postgresql
encoding: unicode
database: test
pool: 5
username: user
password: password
#host: localhost
#port: 5432
# Schema search path. The server defaults to $user,public
schema_search_path: example