db:migrate creates the file db/development.sqlite3 - ruby-on-rails-3

db:migrate creates the file db/development.sqlite3 if the rails app uses sqlite. I prefer to use postgresql for development and production before deploying to heroku.
Part of my database.yml file:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: app-development
username: postgres
password: *******
When I run bundle exec rake db:migrate should I be expecting a file such as db/development.postgresql? Because no file is created in the db directory and neither is an error produced.
$ bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "
users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table
"users"
-> 0.2060s
== CreateUsers: migrated (0.2070s) ===========================================

Well, postgresql is very very different from sqlite. Try reading this article so you can spot the main differences:
http://saaientist.blogspot.com.br/2007/07/choice-of-databases-or-postgres-vs.html.
The thing about your question is that sqlite work with a database file inside your application folder, and postgres don't work this way. There are plenty of resources on how postgresql work with Rails, and i recommend you to read some and try to understand.

Related

Bundle Exec Rake DB Migrate

I have been getting this error when I run bundle exec rake db:migrate for a basic RoR website. I am a beginner and found similar errors on this site and Treehouse but nothing with the specific second half of this error (from the NOT NULLC onward). I am still not sure how to resolve this - can anyone advise? I am running this on windows.
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "em
ail" varchar(255) DEFAULT '' NOT NULLC:/Sites/code/omrails-master/db/migrate/201
30804201341_add_devise_to_users.rb:5:in `block in up'
I guess it happens because you already have the email column in your users table (a previous migration added it, propably that created the table) and the Devise migration you're running (201
30804201341_add_devise_to_users.rb) is trying to re-add it. Is that the case?
If so, open the migration file which first creates the users table and remove the line that creates the email column (it looks something like t.string :email). Close your Rails server, then do the following.
$ bundle exec rake db:drop
$ bundle exec rake db:create
$ bundle exec rake db:migrate
I came across this problem when first starting out. I always solved it by just reseting the database. You already have a column created so reseting it will probably work.
run
bundle exec rake db:reset
than
bundle exec rake db:create
and finally
bundle exec rake db:migrate

Upgrade to Rails 3 and lost ability to run Active Record database migrations

I have a corporate web application that, for various reasons, has Active Record connections to SQL Server as well as Mongoid Documents. There has been resistance to updating to Rails 3 until now (yes, I know, just in time for Rails 4). The app is mostly working fine and both databases play well with each other. However, I seem to have lost the ability to run SQL database migrations.
rake -T gives me the following db options:
rake db:drop # Drops all the collections for the database for the current Rails.env
rake db:mongoid:create_indexes # Create the indexes defined on your mongoid models
rake db:mongoid:drop # Drops the database for the current Rails.env
rake db:mongoid:purge # Drop all collections except the system collections
rake db:mongoid:remove_indexes # Remove the indexes defined on your mongoid models without questions!
rake db:purge # Drop all collections except the system collections
rake db:reseed # Delete data and seed
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, and initialize with the seed data
rake db:migrate just returns without any information whatsoever.
It looks like the mongoid gem might be the culprit as migrations work fine with a blank project and the only db options are mongoid related.
My question is: How do I restore ActiveRecord migrations after upgrading to rails 3 and mongoid 3.1.3?
Figured it out, at least the missing rake tasks part.
Turns out you need to have the require 'rails/all' in your config/application.rb file.
This returns the db tasks to rake.

Uable to run migration on heroku on a postgre database

I need to add a property/column to a table in a production database(Postgre) on Heroku.com (Rails app) by doing migration.
When I do the migration it looks ok, but when I view the columns on the table it has has not added the column!
My development db is sqlite3 and the production db is postgre
I do the following:
heroku run rails generate migration AddUtc_OffsetToEvents utc_offset:integer RAILS_ENV=production --app app-name-1111
and it returns:
invoke active_record
create db/migrate/20130304070946_add_utc_offset_to_events.rb
And then I run the migration
heroku run rake db:migrate RAILS_ENV=production --app app-name-1111
And then:
heroku restart
When I run
heroku pg:psql HEROKU_POSTGRESQL_GRAY_URL --app app-name-1111
and check the columns of the table:
\d+ events
It still does not have the utc_offset column, and no errors are displyed while doing the previous cmds.
Any ideas or hints?
It looks like you are doing several calls to heroku run
Each time you do heroku run it spins up a completely new dyno with your latest code, and when run is over that dyno is destroyed. So the second heroku run does not have the migration filed created in the first.
Since you are already familiar with psql you can just use ALTER TABLE directly. Otherwise you'll need to check your migration into your code and git push heroku master it to heroku, then run it.
Why not just download the code, add the migration and push the changes ?
After, just run the heroku run rake db:migrate on the app.

Problem with rake: "development database is not configured"

I am novice rails/terminal user and just did a clean install of Lion + Xcode + Rails. Unlike before (on Snow Leopard), I now get an error running rake db:migrate.
I have cloned my code through git which worked fine and created the database witht the "createdb" command but when I try run "rake db:migrate" in terminal it now comes up with this error:
rake aborted!
development database is not configured
My config/database.yml file looks like below in the development section which is exactly the way it looked before on Snow Leopard where it worked fine, so don't know if the error I am now getting is related to Lion.
development:
adapter: postgresql
database: my_db
username: rasmus
encoding: utf8
pool: 5
Can anyone help, please?
I got the same error and in my case it was because the database.yml was not indented correctly. All the configuration parameters should be indented.
Note, be sure to follow the proper spacing conventions. The database config is whitespace aware. Two spaces per attribute works fine. In the following code, note how each attribute has two spaces. Do not use tabs. If you don't use spaces for attributes, rake will not work and throw the same error.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: db/production
pool: 5
timeout: 5000
password:
You might also want to look for syntax errors in the file. This is the error that will appear if you have a syntax error in your config/database.yml file and you try to do something like start the local web server or run rake db:migrate.
In my case I had accidentally removed the comment from a line at the top of the file and I was seeing this error since the uncommented line made this an invalid yml file.
Solved!
My "gem install pg" had not been run so basically I was missing the pg gem. After "gem install pg" in terminal everything works fine.
Here's a PEBCAK answer for Googlers - check your Gemfile and make sure you have specified your database adapter gem in the proper group in your Gemfile. I had mine defined for only :production and :staging, and at one point must have manually ran gem install pg on my development machine after switching from mysql. This morning I emptied all of the gems for the app and re-bundle install-ed them, then couldn't figure out why the database wouldn't connect. Moving the pg gem spec outside of any group and running bundle install resolved the problem.
A note to others who land on this question page: be sure that you are running the rake db command correctly, i.e:
rake db:migrate instead of rake db migrate
What worked in my case, having tried all the above when rake db:create failed, was to make sure that my Rakefile was properly configured.
This did the job:
require "sinatra/activerecord/rake"
require 'sinatra/asset_pipeline/task'
namespace :db do
task :load_config do
require "./app"
end
end

rake aborted! ERROR: must be owner of database

I am working through Michael Hartl's excellent tutorial but when trying to prepare the test database with the command:
bundle exec rake db:test:prepare
I get this error message:
ERROR: must be owner of database sample_app_test...
which I never got when using the development database, because I had created the following database role for my Rails app:
CREATE ROLE demo_app WITH CREATEDB LOGIN
(this is using Postgresql)
Does anyone understand why this is failing in the test environment?
TIA...
Did you ensure the ownership of the test DB? try running the \l command on Postgres console client and check the ownerships. you can also try the following query:
ALTER DATABASE sample_app_test OWNER TO demo_app;
First post, writing this down for posterity. I was having the same problem but was able to fix it. You just have to make sure you were/are signed in as a superuser when you create your databases (or the one that is throwing the error).
I was logging into psql with this code:
sudo sudo -u postgres psql
And created my databases. This is bad. You want to log in with these superuser credentials:
sudo su - postgres
And then after you're logged in to postgres:
psql
Then create your databases. You can kill your old databases with the command
DROP DATABASE "database_to_drop";
Recreate them and you should be good to go!