I created a new Ruby on Rails app (first app I'm working on in Rails 3), and for some reason, my app seems to be confused about what environment it should be running in (unless I'm just confused).
When I run rake db:create, it's creating both the "myapp_development" and "myapp_test" databases. When I subsequently run rake db:drop, it drops the development database, but not the test database.
What's going on?
Edit 1: Here's what my database.yml file looks like:
development:
adapter: mysql
database: myapp_development
username: root
password:
# 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:
adapter: mysql
database: myapp_test
username: root
password:
production:
adapter: mysql
database: myapp_production
username: root
password:
Edit 2: Tried recreating a new app from scratch, and still have the same issue. Here's what I did.
1. Created a new rails app:
rails new myapp
2. Edited my Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'mysql', '2.8.1'
# Bundle the extra gems:
gem 'warden', '1.0.3'
gem 'devise', '1.2.1'
gem 'geokit'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'ruby-debug'
end
3. Edited my database.yml:
development:
adapter: mysql
database: myapp_development
username: root
password:
host: localhost
# 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:
adapter: mysql
database: myapp_test
username: root
password:
host: localhost
production:
adapter: mysql
database: myapp_production
username: root
password:
host: localhost
4. Running rake db:create at this point creates both myapp_development and myapp_test. If I proceed to run rake db:drop followed by rake db:create, I get a command line warning stating "myapp_test already exists" since the drop only removes myapp_development, but the create attempts to create both dev and test databases.
It would only create the development and test databases if you ran rake db:create:all, not rake db:create. The latter will only create one or the other, depending on the environment you're working in.
check your
config/database.yml
file in your app for the database configuration, you probably want test around though. but database.yml will let you access everything. It will use the development db by default though then you specify when to use production
Also a note, just running rake db:migrate will create the databases if they don't exist and migrate your models, you might want to use that instead
Related
I am using rails 5.2.3
Here is my command line
RAILS_ENV=development bundle exec rake db:drop
I was expecting only one development db got dropped. But I got these two dbs got dropped. Something I have missed?
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
It's a known issue that when you run db tasks for development environment,they are also run for test envrionemnt.
You can check this issue on the following Rails github reop
https://github.com/rails/rails/issues/27299
As a workaround you can use this:
https://github.com/ioquatix/activerecord-migrations
it fixes some of these issues
Run: bin/rails db:environment:set RAILS_ENV=development
Then run bundle exec rake db:drop
It should only remove dev database ....
So I've followed all of the guides I can find online for this issue, including similar stack posts, but things are not working.
I ran the command
heroku config:get DATABASE_URL
and got
postgres://<username>:<password>#<host_name>:<port>/<dbname>
(with all of the <> parts being my DB info of course) and ran
pg_dump --host=<host_name> --port=<port> --username=<username> --password <dbname> > output.sql
At this point I needed to run
psql -d <local_database_name> -f output.sql
but my rails app followed the Michael Hartl tutorial which uses sqlite3, so I had to switch things over. I tried this by changing my database.yml file to
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
# adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development
# database: db/development.sqlite3
# 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: db/test
# database: db/test.sqlite3
production:
<<: *default
database: db/production
# database: db/production.sqlite3
I also changed the Gemfile to use 'pg' instead of 'sqlite3' in development. At this point I ran bundle install and tried rails db:setup and rails db:migrate which did not create the databases (.. uh oh) and when I run the final step of
psql -d db/development -f db/output.sql
I get plenty of console output, but when I run a rails console sandbox, there is no user info from my Heroku database. Did I miss a step? What's going on? (I'm working on Ubuntu 14.04)
Edit:
First, I undid all of the local changes I made above. Then, I ended up following a combo of the above and the guide here http://manuelvanrijn.nl/blog/2012/01/18/convert-postgresql-to-sqlite/. I used heroku config:get DATABASE_URL to get all of the needed host, port, username, etc. info and then pulled the data with
pg_dump --host=<host_name> --port=<port> --username=<username> --password --data-only --inserts <dbname> > dump.sql
After that I followed the postgresql to sqlite database conversion (the "Longer story a.k.a please explain a little more." version) I linked above. I did all of the edits described in step 2 as well as having to remove a bunch of SELECT statements at the end of dump.sql. Another change I had to make was replacing all INSERT INTO instances with INSERT OR REPLACE INTO to satisfy some unique constraint errors in step 4. That did it!
I am trying to deploy to Heroku but can't because the default sqlite3 server is still in place.
Detected sqlite3 gem which is not supported on Heroku.
https://devcenter.heroku.com/articles/sqlite3
In another tutorial with Rails 3.2.13 I was able to use sqlite3 as the dev db and Postgres as the production db. The Gemfile looks different in Rails 4 but I have modified it to have this:
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
I then changed my database.yml file so that the production section looked like this:
production:
adapter: postgresql
database: my_production_database
pool: 5
timeout: 5000
I then ran bundle install and rake db:create and rake db:migrate but am still unable to push to Heroku. So I tried rake db:drop as well as rake db:create and rake db:migrate but am still getting the same error message.
Am I missing something? What else do I need to do to make sure I'm getting Postgres as my production database and am able to use Heroku?
Don't do it. You are just going to run into problems down the road. Use the same database in production and development. There are a lot of resources available in documenting the switch from a sqlite to postgres database.
Take the time and switch.
Have a look at this Rails Cast.
http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast
Try using this for your production DB
production:
adapter: postgresql
host: localhost
encoding: unicode
database: my_production_database
pool: 5
username:
password:
You can leave username and password blank
I've created my stack on heroku and everything has been deployed but when I try to actually visit it via URL it just defaults to the 500.html error page. The app ran fine on my localhost, but I developed it in sqlite3. I've since changed my Gemfile to look like the following and ran bundle install.
#gem 'sqlite3'
gem 'thin'
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
When you visit the url it should be pointing to my login page.
This is what my database.yml file looks like...does this have anything to do with my problem.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Thanks for any advice
Use heroku logs to see the log file showing the 500 error's cause. If you haven't run your migrations that might be the cause. Be sure to run:
heroku run rake db:migrate
before you use your app.
Your database.yml file points to SQLite still. You'll need to change that to point to the PostgreSQL DB on heroku.
I can see from the comment that you are using sqlite for production into heroku. Which is not possible since heroku doesnt support sqlite. You can either use pg or mysql2 for your production environment. You can check here for solution :
How to configure database.yml for deployment to Heroku
And once you are done with it run these commands
heroku run rake db:reset
heroku run rake db:drop db:create db:seed
heroku run rake db:migrate
I typed cd generate and then rake db:migrate, but the CMD shows that rake aborted could not open database you can refer to chap2 of the book [Ruby on Rails] OReilly Head First Rails Jan A learner's companion to Ruby on Rails 2009
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development: adapter: sqlite3 database: db/development.sqlite3 timeout: 5000
# 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: adapter: sqlite3 database: db/test.sqlite3 timeout: 5000
production: adapter: sqlite3 database: db/production.sqlite3 timeout: 5000
Sounds like your database is not properly configured. Make sure that config/database.yml matches the settings for your computer, and that you can log into the database on the host and port specified with the username and password specified.
It might help to see config/database.yml and the details of your database setup.
If your config/database.yml and adapter are configured correctly, try running rake db:create:all before running rake db:migrate