My Ruby on Rails webapp allows for users to upload an sqlite database in order to export information from that database in a certain format.
Locally everything works fine but I can't deploy it to heroku as I am using the sqlite gem. Heroku doesn't allow it even tho I use postgres for the app itself as configured in the database.yml.
Before I move everything to another PaaS I'd like to know are there any other ways to access the information in the sqlite DB without using the sqlite gem?
I'm afraid you can't use SQLite on Heroku (and I discourage you to use it in any production environment).
When deploying to Heroku the database.yml file gets replaced. Reading from the docs:
When an application is deployed, the build phase configures the underlying Rack or Rails application to use the provisioned database if a config directory exists, and a RAILS_ENV or RACK_ENV environment variable is present. Under these conditions, a database.yml file will be created . If a database.yml file already exists, it will be replaced. The database.yml file is created as Ruby code that dynamically creates its output by parsing the DATABASE_URL environment variable.
Source: https://devcenter.heroku.com/articles/ruby-support#build-behavior
If you want to use PostreSQL in Heroku, just put the needed gem in your Gemfile:
gem 'pg', group: :production
From what I can gather, Heroku is supposed to generate a database.yml file automatically, and ignore the local one. However, I am seeing an error where that is not true, and my changes to the local database.yml are affecting the Heroku app. This is problematic because I have no idea how I should setup production portion of the file so Heroku can find the right database.
For instance with the following
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
followed by the db:migration
$:~/Apps/DancingCupid/DancingCupid$ heroku rake --trace db:migrate
spits out
rake aborted!
unable to open database file
/app/.bundle/gems/ruby/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:27:in `initialize'
...
I can get different errors depending on what type of database I sent for production.
Besides deleting the app and making a new one, is there a way to fix this problem?
Heroku definitely rewrite your database.yml on push so it doesn't matter what is in there in source control.
To confirm this do heroku run bash which will connect you to a bash session in your app then look do a cat config\database.yml and you will see how they have rewritten it.
The other answers are NOT TRUE anymore as of Rails 4.1.
config/database.yml won’t be overwritten anymore when a Rails 4.1.0
RC1 app is detected. Instead, it will be merged with DATABASE_URL so
additional options like pool size can be set in config/database.yml.
To double-check the contents of your database.yml on the Heroku server, you can run remote bash via heroku run bash and then cat config/database.yml to see its contents on the server and compare with your local one.
I don't think you are insane! ( But I thought I was )
I have been beating my way around this problem for a few days, and finally found this article:
http://article.gmane.org/gmane.comp.lang.ruby.rails.heroku/1003/match=database+yml
It led me to believe that maybe it wasn't my code at all!
I then simply destroyed my heroku app and created a new one, and pushed to it. Suddenly everything works fine! I don't know how or when or why, but I think it is possible to overwrite or corrupt the database.yml file that Heroku creates.
Hope this helps!
Try removing database.yml from version control. It's good practice to make a copy of database.yml into something like database.yml.example and adding database.yml to your .gitignore file.
That way when you push to Heroku it won't have any database configuration to refer to.
You probably also don't want the sqlite3 gem in production. Make sure it's in the development/test groups in your Gemfile.
database.yml should not be pushed to Heroku. It will try to connect to that database, timeout and then crash.
Add it to your .gitignore so it doesn't get up there.
I'm getting this error when I'm trying to connect to a mysql database. The problem is that the application works for weeks, and then randomly I get this message. When I get this error message the application is not able to reconnect to the database until I restart it.
I'm using a configuration file to connect to the database, and the adapter is specified...the database configuration is not generated at runtime.
Do you have any idea on what is going on?
when I tried to run a command line script (let's say 'my_script' here), the same error happened. The reasons were:
There is only production environment there.
I missed to set RAILS_ENV for the command line.
So, the following is the solution in my case:
$ RAILS_ENV=production my_script
I just had this problem, and it was caused by a typo in my configration.yml.
I originally had this:
production:
adapter:mysql
When I meant to have this:
production:
adapter: mysql
That one little space between adapter: and mysql makes the difference.
Another possible cause:
In Rails 3.2.x, establish_connection has a default argument set from the environment:
From connection_specification.rb:
def self.establish_connection(spec = ENV["DATABASE_URL"])
resolver = ConnectionSpecification::Resolver.new spec, configurations
spec = resolver.spec
The way ConnectionSpecification::Resolver works depends on ENV['DATABASE_URL'] giving a nil if not set. (Normally, it would be something like 'postgres://...').
So, if you happen to have misconfigured DATABASE_URL such that ENV['DATABASE_URL'] == '', that will give you database configuration does not specify adapter.
I had this error when I mistakenly started rails server with
sudo rails s -e "Production" -p 80
and I should have started rails with
sudo rails s -e "production" -p 80
I found another thing that can cause this problem: "mixing in" another YAML node using & and *.
I was originally doing something like the following to facilitate local, per-develop, Git-ignored config files:
http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams
But, after some debugging, I came to find out that establish_connection was for some reason being called with only the mixed-in key-value pairs and not the main ones. I.e. adapter, host, and database were not being passed in. I have no idea why, and this used to work for me.
Anyhow, instead of mixing in another YAML node, I now put the entire development and test hashes in the local config file, and establish_connection is once again being called correctly.
For me, this command resolved the issue.
rake db:migrate RAILS_ENV=production
I've found a couple of clues that this might be related to older library (ActiveRecord) or gem versions. For example, problems with fixtures even though rest of app seems okay (after an upgrade) or this trac ticket, which "stops gems from requiring an adapter from an old Active Record gem". Both of these are old, though, but it might be worth making sure your gems are up to date (if possible).
Are you using the native rails MySQL adapter by any chance? This is now deprecated under rails, but it's conceivable it's still limping along.
I've taken a very quick look at connection_specification.rb, too, which is where this error is coming from, and my best guess is that a reconnect is failing... but why (since it was obviously okay when you first started the app)? Are you doing something wild like calling ActiveRecord::Base.establish_connection in your application controller (or elsewhere)?
Or perhaps something like: script runner is called from cron in the dead of night when the connection has dropped. Unfortunately, the runner is invoked with an incorrect RAILS_ENV. Thus the wrong stanza is read from database.yml, and that stanza contains an invalid adapter:?
I got the same error, by typing in the following command:
db:migrate RAILS_ENV=product
Should've been:
db:migrate RAILS_ENV=production
If you get this error while deploying with Capistrano. Make sure you are setting the correct RAILS_ENV via
set :rails_env, 'production'
For example I was not explicitly setting the Rails environment in for the Capistrano staging deployment configuration. And thus Capistrano used 'staging' as the RAILS_ENV, resulting in the above error. Setting it to production like above in the staging.rb file solved the issue.
Do remember that RAILS_ENV=staging will look for a staging specification in your database.yml just as setting RAILS_ENV=production will look for a production specification in database.yml file.
Provide database configs, as shown below, for every rails environment you target using, for example
bundle exec cap staging deploy
production:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
staging:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
Remember to use the C-Based ruby gem for mysql. The ruby-based is unstable for production.
Try installing the gem
gem install mysql
Remember to copy libmySQL.dll into the ruby bin directory.
I had this error with another problem; I had specified 'development' twice, and 'test' not at all.
There are alot of bad tutorials out there on the internet that show yaml files like so:
development:
encoding: utf
database: dbname
...etc
YAML files are case sensitive and require TWO SPACES for the inner contents of each given db-type attribute. Like so:
development:
encoding: utf
database: dbname
...etc
UPDATE: I got this error again today. My VPS server had installed Rails 3.2.8 when my app was running Rails 3.2.6.
Definitely check your Gemfile and your database.yml file (of course). The problem here is clearly stated---Rails is not communicating with your database specifically due to an adapter (aka gem)
We had this issue with one of our older apps. Someone had created a boutique named environment, that even if RAIL_ENV was set to production, it was looking for a database configuration called legacy_<RAIL_ENV>, so I had to make a database environment called legacy_production for this app to work.
If you are maintain someone else's app, I would look for a copy of this app's database.yml that is working, perhaps it has some oddly named configuration. You can search your codebase for establish_connection to see if it is defining some strange variant.
I met this problem due to the 'multiple database support issue'. In my app/model folder, there is a file defined a redundant database connection:
class CacheCleanerActiveRecord < ActiveRecord::Base
establish_connection "cache_cleaner_#{Rails.env}"
self.abstract_class = true
end
but this database is not found in my database.yml ( because it's not used at all ).
so the solution is quit simple: remove this file and everything is fine !
You may have an error like:
RAILS_ENV= test
A space after the equals sign is not allowed, change it to:
RAILS_ENV=test
This happens to me, finally I found that RAILS_ENV is case sensitive, in my enviroment I set
RAILS_ENV=DEVELOPMENT , which is wrong, the value of RAILS_ENV must be lowercase.
$ RAILS_ENV=DEVELOPMENT rails server webrick
=> Booting WEBrick
=> Rails 4.2.5 application starting in DEVELOPMENT on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
* development - set it to false
* test - set it to false (unless you use a tool that preloads your test environment)
* production - set it to true
Exiting
/home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_c
onnection': 'DEVELOPMENT' database is not configured. Available: ["default", "development", "test", "production"] (ActiveRecord::AdapterNotSpecified)
from /home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:211:in `res
olve_connection'
$ RAILS_ENV=development rails server webrick
RubyDep: WARNING: Your Ruby is outdated/buggy. (To disable warnings, set RUBY_DEP_GEM_SILENCE_WARNINGS=1)
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: install 2.3.1.
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-07-20 16:41:09] INFO WEBrick 1.3.1
[2016-07-20 16:41:09] INFO ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-07-20 16:41:09] INFO WEBrick::HTTPServer#start: pid=19881 port=3000
rails -e "production" is okay
only rails -e production returns error
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
call rake assets:precompile:all
This is probably not the most likely issue to cause this error, but here it is just in case.
My problem was that I was building the database settings in a Hash using symbols as keys and then serializing it with #to_yaml to database.yaml. ActiveRecord expects the environment names to be Strings, not Symbols, so it wasn't picking up the database settings when reading the generated file. I fixed it by using string keys in the hash.
For Rails4, commenting the line fetch(:default_env).merge!(rails_env: 'production') in production.rb and adding set :rails_env, :production fixed it.
You need to specify the environment when running the server or command as your database.yml file may have only production adapter while simply runnig rake db:migrate for example will take environment variable as development.
Just for the sake of completeness, I just got this error because I natively created a parametrised Rails runner script that takes an email address, and named the command line option -e -- which of course is the one the Rails runner uses for the environment. So it was trying to find an environment configuration that matched the email address!
Luckily, just before the ActiveRecord error mentioned in the title, it gave me an error message that helped me twig what the problem actually was:
You did not specify how you would like Rails to report deprecation notices for your test#example.com environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/test#example.com.rb
Check the spelling of adapter I had adaptor and got this error.
I had this error message when upgrading from Rails 4 to 5. I was calling
establish_connection "myconnection"
where "myconnection" is a valid key in my database.yml. However, passing this parameter as a string is apparently no longer supported. Using a symbol instead got rid of the problem.
I got this from copying and pasting sudo rails server webrick –e production -d from documentation into the CLI.
I'll shut the door on my way out 😔