Capistrano updating my app from Sqlite3 to Rails, Gem::LoadError - ruby-on-rails

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.

Related

Rails : How to configure database.yml for Heroku?

I'm new on RoR. I build a little app using ActiveAdmin and Devise and I wish to deploy it on Heroku.
When I had push my app on Heroku it run properly but the db seems to be empty ! In effect, my local login dont match when I try to log in my ActiveAdmin administration panel...
In addition, the others db of my app are totaly empty...
I guess that i had not fill the database.yml correctly but I dont find how I'm suppose to do it... :/
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
I would be grateful if you could help me or direct me to a solution!
Thank you for your attention ! ^^
production:
<<: *default
database: db/production.sqlite3
You can't use sqlite3 on heroku, because it is filesystem-based and on each restart a new dyno is created, with a completely new filesystem. The old dyno is killed and your sqlite3 data file with it.
Use a client-server DB on heroku, like Postgresql. This is well covered in heroku guides.
These steps is how to push the local database to heroku and to create database
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
Get database info
heroku pg
-> Gave me the name of the database
eg: HEROKU_POSTGRESQL_CYAN
Reset the database on heroku
heroku pg:reset HEROKU_POSTGRESQL_CYAN
Look for the name of the local database
Opened config/database.yml and found the database
name for the development environment.
eg: fashions_development => put the name that the terminal give it to you
Run push the local database to Heroku
Opened config/database.yml and found the database
heroku pg:push fashions_development HEROKU_POSTGRESQL_CYAN

Rails: No connection pool for ActiveRecord::Base

I'm trying to use rails 4.2.6 to develop an app. I'm trying to use postgres for database. Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error.
What could it be?
EDIT
The pg gem wasn't working properly. I had to comment it before starting the server and then uncomment it from my GemFile afterwards. I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended). I removed ruby 2.3 and set up everything under ruby 2.0 environment. It's now working properly.
I had read somewhere that there were some issues with the 'pg' gem in newer Rails releases, requiring people to use 'gem install pg --pre' instead for installing the gem. I tried that, but then my app was requiring the 'pg' gem in my GemFile and, well, the problem stated above showed up again.
This is how my database.yml file ended up:
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: -------
password: -------
pool: 5
development:
<<: *default
database: myDbName
If you are experiencing this error from a rake task, chances are you are not running the :environment task before your task.
Changing:
task :task_name do
end
to:
task task_name: :environment do
end
Should fix the issue.
This issue arises when the server cannot find the corresponding database it depends on to pull data from.
I had same issue from my end, I updated my version of Sqlite3, and it was higher than the version that the current Puma Server version supports.
I simply had to uninstall the Sqlite3 version, and then install the version supported by the current version of Puma Server.
gem uninstall sqlite3
This will uninstall the updated version of Sqlite3, and then you run the code below stating the version supported by your current server.
gem install sqlite3
Or you can as well open your Gemfile, and then include the version for the database that you are using
gem 'sqlite3', '~> 1.3.6'
N/B: The sqlite3 version is the most current version as at the time of writing this answer
And then run
bundle update
to install the version of the database that you specified.
That's all.
I hope this helps.
For PostgreSQL your database.yml file should look something like that:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
development:
<<: *default
database: your_db_name
Also make sure that you have the gem installed: in your Gemfile:
gem 'pg'
Finally, restart your server.
Hope that helps
Edit: I almost forgot, make sure you have your PostgresSQL running, check this link for download and setup.
Check the database.yml if all settings are ok. If its the first time and you didn't create the database yet use this command to create the database
rake db:create
If the database already exists try reset the db migrations,
rake db:migrate:reset
Hope it'll solve the problem. Go to rails console and try something to check if its working or not.
I had to simply restart the server for the warning to go away.
I've encountered the same problem when I try to access the Model before creating the DataBase.
Make sure you run rake db:migrate at least once.
If you already run migration then check the Database as mentioned in previous answers.
In my case, the config/database.yml was taking variables from the environment:
# ...
test:
<<: *default
database: <%= ENV.fetch("DB_NAME") %>_test
username: <%= ENV.fetch("DB_USER") %>
password: <%= ENV.fetch("DB_PASS") %>
# ...
The error was coming when the new terminal window where I was running the bundle exec rails spec did not have those variables initialized.
Doing,
$ export DB_NAME=mydb
$ export DB_USER=myuser
$ export DB_PASS=mypass
$ bundle exec rails spec
fixed the issue.
I had to restart the server, and make sure you entered username and password. Create development and test databases
when you are running rails db:migrate in data base rows will be created according to your migration files. you can see schema for further info.
Rails 5
New app
Using Postgresql for both test and development
Specs run, so Rails can connect to Postgresql
But when I started the web app, I got "No connection pool with id primary found."
Ran 'bin/rails db:migrate:reset' and restarted the app. It worked. No idea why.
database.yml:
development:
adapter: postgresql
encoding: unicode
database: rails5_development
pool: 5
username: foo
password: bar
host: localhost
port: 5432
test:
adapter: postgresql
encoding: unicode
database: rails5_test
pool: 5
username: foo
password: bar
host: localhost
port: 5432

rake db:create picking up old adapter database

I experimented a few weeks ago by adding a second database to my database.yml. While everything worked, I didn't like the approach and removed all the models etc of that second DB. The primary database was postgresql and the second was sqlite3. The db location was the absolute path to the sqlite3 db in database.yml.
Every once in a while I will dump my production db on the server and pull it into my development db. I'd do this with:
rake db:drop
rake db:create
psql -d {database} < {output from pg_dump}
The first time I tried this procedure after my experiment, I got a rake error:
xuserAir:db xuser$ rake db:create
(in /Users/xuser/Work/v/vfw-post)
/Users/xuser/vfw/gnucash/export/vfwexport.gnucash already exists
vfw-post_test already exists
rake aborted!
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).
Gem::LoadError: sqlite3 is not part of the bundle. Add it to Gemfile.
There is no reference anywhere in my application root to sqlite3, the database path or any of the file names or directory. rake db seems to be picking up the old information from someplace by I have no idea where. I may have had db/development.db set up as a symbolic like to the export.db, but that is also gone.
Also rake db:drop deleted the sqlite3 database that was outside the app root.
I striped everything in my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: vfw-post_development
test:
<<: *default
database: vfw-post_test
production:
<<: *default
database: vfw-post_production
username: vfw
# password: <%= ENV['VFW-POST_DATABASE_PASSWORD'] %>
And still get the same error.
Stuck
Rails 4.2
I found out where it was coming from.
Somewhere in my experiment with two database I set up
DATABASE_URL=sqlite3:/Users/xuser/vfw/gnucash/export/vfwexport.gnucash
I think to help when I deployed. I didn't remove that and it goes to the DATABASE_URL first.

Fresh rails app defaulting to Postgres instead of SQLite3

I've just installed rbenv, ruby 2.2.3 and rails 4.2.4 for the first time on this machine. I've started my rails application with no change to any of the code, just the default generated documents from using rails new ., I then started the server with rails server.
When hitting http://localhost:3000 I'm getting the following error:
"Specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
I've got postgres installed from a previous project with Node, but my database.yml still reads as you'd expect from a new application:
# 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:
<<: *default
database: db/production.sqlite3
I don't really want to use Postgres at the moment, I'm just starting out and I'd rather keep things simple with SQLite3 for a bit. Does anyone know what may be going on and what I could do to get it using SQLite3 so that this error stops?
There is another solution, for those who actually need to keep the DATABASE_URL environment variable without affecting Rails (like me). You can use a url sub key:
development:
<<: *default
url: sqlite3:db/development.sqlite3
This is documented here.
The problem is that when you start the server it is looking for environment variable DATABASE_URL which is probably set to postgres and this takes precedence over the database.yml file. You can delete the environment variable, and it should work, or you can reset it to SQLite.
It's an old post but maybe somebody will find this useful.
In my case I had the DATABASE_URL variable exported in ~/.bash_profile like so
# Postgres installation
export DATABASE_URL=postgres:///$(whoami)
On Linux it might be in ~/.bashrc file.
Just remove or comment out the export line if you don't use Postgres anymore or use AlienBishop's solution if you do.
As IliaAptsiauri mentioned, without DATABASE_URL variable applications should use settings from database.yml file.

rails passenger deployment - Could not find table 'users'

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!

Resources