Rails: Populate Heroku database with development.sqlite3 data - ruby-on-rails

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

Related

Running queries in my new PostgreSQL production database

I just launched my Rails app that I am building in Cloud9 to Heroku (woohoo!). My default database was sqlite3, so in the deployment process I added PostgreSQL in my gem as such:
# Use postgresql as the database in production
group :production do
gem 'pg'
end
# Use sqlite3 as the database in development
group :development do
gem 'sqlite3'
end
I want to keep my sqlite3 database in development and use PostgreSQL for production. My question is how do I monitor or run queries for the PostgreSQL data in my production Heroku site.
I'm used to running "rails c" then "User.all" in my terminal to see my users. Now if I run that it doesn't show any data from production. How do I see that data? I did some searching and i think I should be using "psql" command. But if I run "psql -l" for example I get:
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
If you just want to run some PostgreSQL queries on Heroku production then type following command in your terminal:
heroku pg:psql (or heroku pg:psql -a your-heroku-app-name)
Here you will be able to run such queries like select * from users. To exit the terminal type \q.
You can also invoke Heroku production console with
heroku run console (or heroku run console -a your-heroku-app-name)
to run such queries like User.all.

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

Ruby rails heroku I have added pg to gemfile yet it says it did not

I am trying to git push heroku master my app into production for heroku, and despite the fact that my gemfile looks like this:
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :production do
gem 'pg', '0.18.3'
gem 'rails_12factor'
end
and I do :
bundle install --without production
and
git commit -a -m
and finally
git push heroku master
it will reject the transfer saying that pg is not installed in gemfile
Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: rake aborted!
remote: Gem::LoadError: 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).
So, when I do heroku open
I get this
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.
I dont know if the two errors are related. Heroku did upload my id_rsa.pub key and I did log in via command line through emails and pwd.
So this is where I think the error was, in case someone goes through the same.
1) when I tried to create a ssh key through
cat ~/.ssh/id_rsa.pub
despite the fact that I do have ssh installed by default on my linux it that command did not work at first, so I ended up generating in through a ssh keygene command. I copied it and pasted it (the public one) in my bitbucket ssh keys, but the private one which had been generated did not match with the original one. and the error was due to this mismatch of keys between the private local and public.
I think I also messed up as I was using two different computers and if you do that you have to go on to another schema of different keys for the same account etc.
I restarted from scratch, I deleted my git directory, i deleted my repository at bitbucket and the app at heroku and tried that command again.
For some reason this time the command worked (no, it was not misspelled the first time), so now the synchronization of keys was correct. This time I copied it and pasted it straightway in the ssh keys of bitbucket.
and I went trough the same stuff again like this:
This follows the instructions from the great book by Michael Hartl (The Ruby on Rails Tutorial)
cat ~/.ssh/id_rsa.pub
$ git remote add origin git#bitbucket.org:<username>/hello_app.git
$ git push -u origin --all # pushes up the repo and its refs for the first time
$ git remote add origin git#bitbucket.org:myname/hello_app.git
now for the gemfile
group :production do
gem 'pg', '0.18.3'
gem 'rails_12factor', '0.0.2'
end
$ bundle install --without production
curiously now it did not complain about the pg but about mysql2, so this really does not matter much appearingly
$ git commit -a -m "Update Gemfile.lock for Heroku"
$ heroku version
$ heroku login
$ heroku keys:add
(this time the keys were added or fetched correctly)
$ heroku create
Creating damp-fortress-5769... done, stack is cedar
http://damp-fortress-5769.herokuapp.com/ | git#heroku.com:damp-fortress-5769.git
Git remote heroku added
$ git push heroku master
heroku open

Ruby on Rails - Heroku deployment issues

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

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