Ruby on Rails - Heroku deployment issues - ruby-on-rails

I seem to have reached a bump, searched here and on other foruns but nothing. I'm running rails 3.2.3 and ruby 1.9.3 and want to deploy my app on heroku.
I've created the cedar and although I can git push heroku master I am getting a complete 500 server error.
I suspect is it because my DB isn't there. However, I can't seem to get it there.
I've run:
heroku run rake db:create -> This gives out some warnings about deprecation and then dkhgclqccm already exists
So it exists already? So lets migrate it:
heroku run rake db:migrate
However this outputs:
<deprecation errors>
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "hphotos" does not exist
: ALTER TABLE "hphotos" ADD COLUMN "description" character varying(255)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
That particular migration is:
class AddDescriptionToHphotos < ActiveRecord::Migration
def change
add_column :hphotos, :description, :string
end
end
It seems good to me, don't know why its giving me this error :/
One Last thing, even if this migrations worked, my DB would be empty and me seeds.rb doesn't have all the necessary data for the database. So I though about pushing the hole DB.
heroku db:push
! Taps Load Error: cannot load such file -- sqlite3
! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
!
! sudo gem install taps
Why is this showing?
I installed the 'heroku' gem and the 'taps' gem and I got this in my gem file:
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'pg'
end
Also, when I run heroku run rake db:version, it shows: Current version: 20120508130957
This is actually 5 migrations short on my currrent version, but I can't migrate it as shows the error I spoke of above...
Thanks in advance

Heroku comes with a database set up (which is why db:create didn't work). have you tried heroku run rake db:schema:load? This should take your schema.rb file and load it up into the DB. This is a much better way of doing it than db:migrate every time you want to setup a new DB
Edit:
For your last question about taps, it looks like it's trying to use sqlite locally but you only have pg in your Gemfile. You probably have in config/database.yml adapter: sqlite. So either you need to use postgres locally and change that adapter to postgres, or go the easier route and use sqlite locally and add that to the :development group.
Note that heroku only uses postgres so I would not suggest developing off of mysql since there are some inconsistencies in some syntax and how you do a couple of things between the two platforms. Then again, if you're using only ANSI-compatible queries or just using rails' methods to activate queries then you should be ok either way.

I think you need to check your migrations closely to see if you actually have a file which says:
def up
create_table :hphotos do |t|
[...]
end
It seems that the table hasn't been created remotely and you are attempting to modify it.

The solution is to add not only taps gem but also sqlite3 gem into the Gemfile, into the :development group. If you are using sqlite3 in your development, then adding taps gem would be enough.
But since you are using mysql2 on your development so to solve that problem you have to add both.
group :development do
gem 'taps'
gem 'sqlite3'
end

Related

Database migrations to heroku don't work

I've been trying to migrate my database to heroku, without success. I have added a few more users to my database and when trying to log them in with email address and password on my heroku app, I get invalid email/password error, even though this works perfectly fine on my local server. NO errors at all when doing all the steps described below.
As suggested in a previous post, I've tried the following:
Made changes to local code
Ran any migrations LOCALLY - I used bundle exec rake db:migrate
Added all changed files to Git git add -A
Commit all added files to git git commit -m "Adding features"
Pushed the changes to Heroku git push heroku master
Ran heroku run rake db:migrate
After I run this I get:
astelvida:~/workspace/sample_app (master) $ heroku run rake db:migrate
Running rake db:migrate on ⬢ shrouded-ravine-80000... up, run.2794
ActiveRecord::SchemaMigration Load (0.8ms)
SELECT "schema_migrations".* FROM "schema_migrations"
Following migrations do heroku restart
I've also checked my .sqlite3 file to check that the new users actually exist in the database.
I've also tried this: $ bundle exec rake db:migrate RAILS_ENV=production
I've also updated my gemfile.lock.
My gems in dev & production:
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '3.1.0'
end
Note: I run bundle install --without production, however this is how I've always been using it, and the login data works for some of the users i've created in the past. Also I'm using rails 4.2.2.
Ok... let's get something clear here.
Rake db:migrate doesn't migrate the data of the database. It runs all migrations (Table creations, updates, etc) so that the database structure is the same, however the data isn't! It's a fresh new database with the same structure.
What you're doing is making sure your PG database has the same structure as your sqlite3 database and it does. But if you want to pass the data from one to another It's gonna be hard i would say. You have to create a dump file from your sqlite 3 database, change it to pg and run in your heroku app.
Here is a question about it.
Convert SQLITE SQL dump file to POSTGRESQL

schema.rb - dump errors - NoMethodError undefined method `default_function'

I went to look at my schema.rb file and found the following:
ActiveRecord::Schema.define(version: 20140729164926) do
# Could not dump table "account_services_indices" because of following NoMethodError
# undefined method `default_function' for
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x757a77>
...
Those last three lines are repeated for every table in the database!
The database seems to be working fine. I can do a db:rollback and db:migrate with no problem except that the regenerated schema.rb still looks like the above. Looking back through my GIT repository, it happened about 7/29/14. The only other db related change in that commit was a minor migration, adding a column to each of two tables. I tried a db:rollback, then db:schema:dump, but no luck. Other commits at that point were changes to views and controllers only.
I'm using the 'activerecord-jdbcpostgresql-adapter' gem. Version 1.3.9 was released July 7, 2014. Forced that back to version 1.3.8 in the Gemfile, bundled and tried db:schema:dump again, but still the same thing. Dropping the database and starting over might work and wouldn't be catastrophic, but I hate to lose all my development data right now.
Renamed the database in database.yml. Created that new database and built it out with db:migrate. Resulting schema.rb is still a list of the same errors.
Any suggestions?
Solved it!
I'm still not sure of the exact cause but here's my resolution. I created a rails app from scratch called Blog, then started to replicate the gems I'm using in the real application. The test app was generated with
gem 'rails', '4.1.4'
and worked with no problem. I changed that to
gem 'rails', '4.0.0'
to match my real app and did a bundle update and bundle install. Doing a rake db:schema:dump then gave me the following error:
rake aborted!
NoMethodError: undefined method `configure' for #<Blog::Application:0x6932cb>
/home/mpipkorn/rails_projects/blog/config/environments/development.rb:1:in `(root)'
/home/mpipkorn/rails_projects/blog/config/environment.rb:5:in `(root)'
Tasks: TOP => db:schema:dump => environment
Following a lead in StackOverflow, I did the following:
Change the first line in app/config/initializers/development.rb from
Rails.application.configure do
to
Blog::Application.configure do
That matches the format in my real app. I did rake db:schema:dump again. This time the rake task ran with no errors, but looking at the resulting schema.rb file, I see
ActiveRecord::Schema.define(version: 20140806174338) do
# Could not dump table "articles" because of following NoMethodError
# undefined method `default_function' for #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x183e66e>
end
Changing the Blog Gemfile (back) to
gem 'rails', '4.1.4'
and doing a bundle update and bundle install, then doing a rake db:schema:dump now results in a normal looking schema.rb file with no error messages!
The bottom line is that changing the rails gem from 4.0.0 to 4.1.4 in my real application solved my problem. What exactly caused this to begin with I'm still not certain, but an update along the way seems to be at fault and there are certain version combinations of gems that don't work, perhaps the combination of activerecord-jdbcpostgresql-adapter 1.3.9 with Rails 4.0.0.

Rails: Populate Heroku database with development.sqlite3 data

I would not ask this if my peculiar case had already been answered on this forum so please pardon my re-asking.
I have a rails app which uses an sqlite3 database for development and it already contains all the data I need for the app. On pushing this app to heroku I was unable to view the app over there until I had to run heroku run rake db:migrate. Now I can view the app, but I can't login in with admin accounts or do any other thing that requires data from the database. i already did all that is required (I think) as shown in my gem file below:
source 'https://rubygems.org'
gem 'rails', '3.2.12'
gem 'bcrypt-ruby'
gem 'kaminari'
gem 'gon'
.
.
.
gem 'jquery-rails', '2.0.2'
group :development, :test do
gem 'sqlite3', '1.3.7'
end
group :production do
gem 'pg', '0.12.2'
end
.
.
I also tried heroku db:push but that didn't help either. The main issue is that i am working on this app with someone else and we employ github for version control. But whenever I we get updates form each other our sqlite database always seems to empty itself. So to solve this problem we usually have to email the updated database directly to the other, who then proceeds to use it and replace the empty on in his local repo. I suspect that the reason why heroku rakedb:migrate is this.
But giving as we've come this far in our development, we really cannot afford to start over. Is there a way we can populate the remote heroku database with the content of our local repo db? Thanks for your responses
You should export your data from sqlite into a sql dump file and start using postgres locally. If you continue to use sqlite locally and postgres remotely, you will have a bad time.
After you've migrated locally, follow this guide on Importing and Exporting Heroku Postgres Databases with PG Backups.
Also, don't use db:push or db:pull; they are deprecated.
Update
To get the sql file in your local postgres, first install and setup postgres (Homebrew, macports, other...) then do something like so:
$ psql -U user -d database < file.sql
From there, you can use the pg_dump utility to export to a .dump file. You can also import the sql directly to Heroku Postgres:
# this will delete ALL data in your postgres, so make sure it's what you want
$ heroku pg:reset HEROKU_POSTGRESQL_COLOR --app app_name
$ heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql

Database issues with Heroku deployment

I've got a basic app that I'm trying to deploy to Heroku. I can push it to Heroku with git and it shows the default Rails welcome page, but when I try to access any subpages, (pages that work when I deploy on a local server with rails server) I get "something went wrong" messages.
I pulled up the log and I'm get 500 responses from the server and lots of ActiveRecord::ConnectionNotEstablished messages which seems to be an issue connecting to the database.
Further, when I tried to run heroku rake db:migrate, I get the following errors
rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adap
ter` (pg is not part of the bundle. Add it to Gemfile.)
Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)
Per the suggestion above, I did add gem 'pg' to the Gemfile After running gem install pg and then bundle install. Also, I tried gem install activerecord-postgresql-adapter (on Windows) and get
ERROR: Could not find a valid gem 'activerecord-postgresql-adapter' (>= 0) in a
ny repository
ERROR: Possible alternatives: activerecord-jdbcpostgresql-adapter, activerecord
-postgis-adapter, activerecord-jdbcmssql-adapter, activerecord-jdbcmysql-adapter
, activerecord-postgresql-cursors
From the suggestion of the book that I'm reading, I ran heroku db:push and it competed without error, but it didn't solve the problem of the pages not rendering once the app is push to Heroku.
update: I've tried installing the activerecord-jdbcpostgresql-adapter as suggested as a possible alternative. I updated the Gemfile with
group :production, :staging do
gem 'pg'
end
and the database.yml file with
production:
adapter: jdbcpostgresql
and when I heroku rake db:migrate, it still gives me the message telling me to install postgresql adapter
If heroku rake db:migrate runs without error, then your database connection info is correct. It's likely that there's another error preventing the page from rendering correctly. If you tail your heroku logs while making a request, are you still getting the same database error? I suspect that you'll find it's a different error that you'll need to fix.

heroku rake db:migrate rake aborted! unable to open database

I am currently making my way through the Ruby on Rails tutorial over at http://ruby.railstutorial.org/ and I am trying to migrate the demo_app database to heroku.
heroku rake db:migrate
rake aborted!
unable to open database file
I have read on other stackoverflow posts that some people fixed this by entering
group :production, :staging do
gem "pg"
end
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
in the gemfile. I also entered it into my gemfile and then deleted my old gemfile.lock and redid my bundle install AND rake db:migrate command. I am still receiving the same error.
I am obviously brand new to ruby, rails and heroku but I understand that the problem seems to be that I am using sqlite locally and postgresql in production (on heroku). Do I now have to install this postgresql onto my machine and then RE-migrate the DB? I am afraid I will not be able to get much more out of the tutorials (or ruby on rails itself) if I cannot use heroku.
Kill it!
I was having the same problem and found no solutions. I think something we are doing in those tutorials is leading us to mangle the database.yml file that heroku generates.
I ended up destroying my heroku app
heroku destroy
and then creating a new one, pushing a fresh copy, and running
heroku create
git push heroku master
heroku rakedb:migrate
This time everything worked fine! Just make sure you have the pg gem in your gemfile for production
group :production do
gem "pg"
end
and add config/database.yml to your .gitignore file too for good measure.
or if it's working ok locally do a heroku db:push to magically put your local sqlite DB into Heroku's postgresql db.
I always work with the same DB platform locally just so I don't run into any differences (usually only when you start doing DB specific SQL) so I run Postgresql locally too.
Had the same problem... with Heroku interface... ran:
heroku rake db:migrate --trace
and found the problem to be with faker, not being found...Since 'faker' in our Gemfile is loaded in the development group, I loaded it in the production group as well.
saved Gemfile
bundle install
git add .
git commit -m "fixed faker"
git push
git heroku push
heroku rake db:migrate
heroku rake db:populate
now everything works...the QUESTION, now is what to do with 100 users on my production site?
At least I can continue with Hartl's tutorial!!

Resources