Location of postgresql database in nitrous.io project - ruby-on-rails

I'm not sure if I'm going mad or if Heroku's dataclips are acting up but I have the following problem.
I've set up a ruby on rails development environment on Nitrous.io and have connected to a database on Heroku. In Nitrous.io I then type psqlto access postgresql.
From there I type \lto list the databases.
Then \c <name of development database hosted on heroku>
Then \dt to list the relations, one of which is users
Finally, I input the command SELECT * FROM users; and this returns relevant information on users such as email address, encrypted password etc etc.
However, when I go to Heroku and create a dataclip with the code select * from usersto be run against the same database, I'm getting the following warning
Error: Dataclip cannot be created
ERROR: relation "users" does not exist
LINE 2: select * from users
Am I completely missing something here or is the dataclip throwing an error for no reason? I'm using Devise (for the first time) in my app. Would this have anything to do with it?
Edit
I'm starting to think I've stored the database on Nitrous.io and not in Heroku. I think I may have used autoparts to install postgresql on my nitrous.io vm
I've run the command SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' as a dataclip on Heroku and the users table just isn't there. I then ran it on some other projects I have on Heroku and I can clearly see the tables I've created.
Can anyone confirm my suspicion or know how I can check the location of the database?

Yes I had indeed set the postgresql database up in nitrous.io itself rather than on Heroku. This page explains it: http://blog.nitrous.io/2013/07/02/building-a-rails-4.0-app-on-nitrous-io.html

Related

Rails: Migration and database differences between development and production

I recently deployed a website to heroku and I am having problems with the database. I am using ruby on rails and when I first created my local databases I used SQLite. When deploying to heroku I had to change my databases to Postgres and in the process several migration files got removed and changed into a single migration. Now, when I run the postgres database on my localhost, I am still seeing all of the columns in my database, but when I visit the page on heroku one of the columns is missing.
I have checked both my local console and the heroku console and the databases are now different. The local console includes all of the columns in the database and the heroku console is missing one of the columns in the database, but is generating all of the other columns correctly. I have tried running a rake task on heroku and have pushed the most recent changes to heroku.
I tried to to add an additional migration to add the missing column to the database, but whenever I try to rake the migration it tells me that attribute already exists. Any help I can get is appreciated!
The best way to set up your database on heroku is with
rake db:schema:load
From the guides on migrations
Migrations, mighty as they may be, are not the authoritative source
for your database schema. That role falls to either db/schema.rb or an
SQL file which Active Record generates by examining the database. They
are not designed to be edited, they just represent the current state
of the database.
There is no need (and it is error prone) to deploy a new instance of
an app by replaying the entire migration history. It is much simpler
and faster to just load into the database a description of the current
schema.

Everything works except rails console after moving to Postgresql on Cloud9

I've moved my development environment from sqlite to postgresql on Cloud9 IDE, following this post Cloud9 postgres.
Now just about everything works,
created a new db
migrated table changes
saved data to db with rake tasks
rendered data from the db in views
But when I run rails c and try and load data there, it fails.
Item
=> Item (call 'Item.connection' to establish a connection)
Item.all
=> PG::ConnectionBad: fe_sendauth: no password supplied
I don't think its actually a no password issue because in every other way I can access and work with the database... just not in rails console.
Does maybe something need to be done to setup a new connection here?
the main issue is POSTGRES need a role to be setup , no doubt the app must be working fine, but when u type in rails c , the control access the schema file and tries to load all the table, here the table ITEM must be the first one to be read by control.
all you need to do is create a role in postgres as superuser and type in its credentials in database.yml.
let me know if this works

Rails DB Migration - permissions

I'm a Rails novice, but am currently trying to modify an existing application that I inherited.
I'm running some db migrations that create new tables on our staging server (Not local machine) and all the new DB tables create seem to have different permissions than the tables already created.
I am running "rake db:migrate VERSION=20110213100531 RAILS_ENV=staging" with one sshlogin and I have my sql login defined in my database.yml under staging as separate mysql database login.
Is there any particular settings/user I should be running my db migration under?
From what you've said I think most likely your problems are being caused from the way you have your user permissions set up in the remote database. This is not something that can be altered from the rails side.
Your database.yml file will just specify your login details into that database, but it's up to you to modify your user permissions settings in your database.
If you've got admin access into your database, I'd start by comparing the previous user's permissions to your own and seeing what's the difference.
All the best

ActiveRecord PGError on Heroku not locally

I am getting an ActiveRecord PGError on Heroku not locally. The error message from the Heroku Logs is:
ActiveRecord::StatementInvalid (PGError: ERROR: column items.user_id does not exist
LINE 1: SELECT "items".* FROM "items" WHERE ("items".user_id = 4) OR
This code works locally (SQLite) but not on Heroku (postgresql) so I have been reading about the differences. At first I thought that it was a case issue as I recognised that it should be User_ID -- but now I'm not sure, it's something about "items" that isn't quite right and I can't work out exactly what it is.
As I said this works absolutely fine on my local machine, it's only on heroku and postgres that the problem comes up.
Thanks in advance.
It appears that your database does not have the 'user_id' column in the items table. This could be because your migration has not been run recently, or perhaps you have edited an existing migration rather than making a new migration to add this column.
Do you care about your data on heroku at the moment? If not you can use Taps to take whatever you have in your local database and set Heroku's database to a (virtual) synchronized copy of it. On your local machine from your project's directory:
heroku db:push
This will wipe your heroku database so use this wisely.
Try restarting the app, using the following command:
heroku restart
It was a case issue after all - postgres does (I believe) a downcase on everything before sending it to the db -- my user_id was actually called, inexplicably, User_ID; after migration everything is working -- thanks to those that helped.

How do I exclude data from local table schema_migrations from being pushed to Heroku DB?

I was able to push my Ruby on Rails app with MySQL(local dev) to the Heroku server along with migrating my model with the command heroku rake db:migrate. I have also read the documentation on Database Import/Export. Is that doc referring to pushing actual data from my local dev DB to whichever Heroku's DB? Do I need to modify anything in the file database.yml to make it happen?
I ran the following command:
heroku db:push
and I am getting the error:
Sending data
2 tables, 3 records
!!! Caught Server Exception | ETA: --:--:--
Taps Server Error: PGError ERROR: duplicate key value violates unique constraint
"unique_schema_migrations"
I have 2 tables, one I create for my app and the other schema_migrations. The total number of entries among the 2 tables is 3. I'm also printing the number of entries I have in the table I have created and it's showing 0.
Any ideas what I might be missing or what I am doing wrong?
EDIT:
I figured out the above, Heroku's DB already have schema_migrations the moment I ran migrate.
New question: Does anyone know how I can exclude data from a specific table from being pushed to Heroku DB. The table to exclude in this case will be schema_migrations.
Not so good solution:
I googled around and someone else was having the same issue. He suggested naming the schema_migrations table to zschema_migrations. In this way data from the other tables will be pushed properly until it fails on the last table. It's a pretty bad solution but will do for the time being.
A better solution will be to use an existing Rails command which can reset a specific table from a database. I don't think Rake can do that.
Two possible options:
The heroku gem and the taps gem (which it uses to synchronize databases) are both open-source - you could fork them, alter the taps client API to support excluding tables from a push, then alter the heroku gem to use that new option.
You could write a wrapper script that uses pgdump to backup the schema_migrations table, drops that table, heroku pushes the database, then reloads the table.
This is kind of a guess based on the error you're getting, but push looks like it grabs the schema and the data. I'd try push to an empty database.
I've just deployed a Rails 3 beta app to heroku on their new bamboo server. I can now upload data from my local dev machine to the heroku database by doing:
heroku rake db:fixtures:load test/fixtures/my_model.yml
The data is then properly propagated in the Heroku database. Even though I specified a specific data file, it automatically pushes data from my other yaml files. It probably has something to do with my model relationships.
If your databases are out of sync you can always reset the Heroku database before pushing using
heroku db:reset
To push/pull from specific tables
heroku db:pull --tables logs,tags
http://blog.heroku.com/archives/2010/4/21/supporting_big_data_part_1

Resources