rails passenger deployment - Could not find table 'users' - ruby-on-rails

I made the passengers's offical tutorial with no error. I pull my code from git and used this commands
bundle install --deployment --without development test
bundle exec rake assets:precompile db:migrate
my 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: 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:
adapter: sqlite3
database: db/production.sqlite3
log/production.log:
I, [2015-08-28T15:54:47.310372 #32086] INFO -- : Started GET "/" for 176.219.167.108 at 2015-08-28 15:54:47 -0400
I, [2015-08-28T15:54:47.334003 #32086] INFO -- : Processing by ArticlesController#index as HTML
I, [2015-08-28T15:54:47.365923 #32086] INFO -- : Completed 500 Internal Server Error in 31ms (ActiveRecord: 0.9ms)
F, [2015-08-28T15:54:47.367834 #32086] FATAL -- :
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/models/ability.rb:5:in `initialize'
I checked I've users table with this command on rails console
ActiveRecord::Base.connection.tables
btw, I use devise gem for users.
I'm absolute beginner about rails, I hope the info which I write is enough to you understand problem.
and my server info: Digital Ocean 5$ droplet. Ubuntu 14.04. Nginx, passenger, rvm, ruby 2.2.2.
and my 500 page : image

You should be able to solve this by simply running the following:
RAILS_ENV=production bundle exec rake db:migrate
However, I don't advise you use SQLite in production, as this type of database is best for testing and development purposes. You might want to look into using PostgreSQL. The following should work for you:
in your Gemfile, add:
gem 'pg', :group => :production
Run bundle install.
Then in database.yml use:
default: &default
adapter: postgresql
encoding: unicode
pool: 10
timeout: 5000
login: &login
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASS'] %>
production:
<<: *default
<<: *login
database: your_database_name
For security reasons, it's best not to write your database username and password in plaintext within files in your Rails app. Far better to set them as environment variables. To do this, edit the ~/.bash_profile file on your Droplet and add the following:
export DATABASE_USER=your_username
export DATABASE_PASS=your_password
Exit that file and run source ~/.bash_profile from the command line to load these new settings into memory.
You will then need to create the PostgreSQL database, for which there are plenty of guides online.
Once that's done you will want to run the same command that works for SQLite when running migrations in production:
RAILS_ENV=production bundle exec rake db:migrate
Hope this helps!

Related

Capistrano updating my app from Sqlite3 to Rails, Gem::LoadError

I am upgrading my rails app from Sqlite3 to Postgresql. I am using Ansible to setup my server and Capistrano to deploy. I was able to successfully run ansible and now I'm trying to deploy my branch to an existing server that had Sqlite3 installed on it.
On the capistrano step:
deploy:assets:precompile
I get this error
$HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile
01 rake aborted!
01 Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
But I don't understand. I have Postgresql set up in my branch I'm trying to deploy.
Here is my database yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: {appname}_development
test:
<<: *default
database: {appname}_test
staging:
<<: *default
database: {appname}_staging
username: <%= Rails.application.secrets[:db_username] %>
password: <%= Rails.application.secrets[:db_password] %>
Obviously changed {appname} with my actual application's name. I didn't include a production key cause this app does not have a production website only a staging server. It is more of a playground I use for rails.
Does anyone have any experience with fixing this issue? I'm really not sure if there is an issue with the Capfile or if I'm missing something. Thanks (and let me know if you need more of my code)
Worth noting this app is still running Rails 5.1 (I'm going to update this next I just wanted to get it on a Postgres db first)
EDIT: OK I am onto something. i discovered that this capistrano step:
04 ln -s /var/www/{appname}/shared/config/database.yml /var/www/{appname}/releases/20190923224949/config/database.yml
Is not updating my database yml to be the updated postgres db. I'm guessing I'm missing something somewhere. :cry:
I GOT IT. I'M A GENIUS.
I had to rereun the webserver playbook to get my database.yml file onto the server.

RAILS_ENV not working properly in command line

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 ....

Cloning Heroku postgresql DB to local db for "live" testing

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!

Rails app points to 500.html page when uploaded to heroku

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

could not open database

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

Resources