Friday, March 18, 2011

Export PostgreSQL into CSV

Redirect console output

First simple way is to redirect the psql console output to file:

\o '/tmp/file.csv'
SELECT * FROM users;

All the result output is redirected to the /tmp/file.csv. and then you open the csv and select the | character as the delimiter.

The obvious problem with this approach is that you cannot have | character in your data. and you also get some other text you dont want, like the result row count: (x row)

Export data into CSV format with header

Postgres has one function called COPY:

COPY (SELECT * FROM users) TO '/tmp/file.csv' CSV HEADER;

In this way, you can have a nicely escaped data with a nice header.

Monday, March 14, 2011

uninitialized constant ActiveRecord::TestFixtures

I saw this uninitialized constant ActiveRecord::TestFixtures error when i run the rake test. I have checked all the environment configuration. I am sure i am using Mongoid without ActiveRecord.

after reading the fixture_support.rb source code, I found out that the error is thrown only if ActiveRecord was called somewhere in my code. and it turned out to be caused by the Transition suggested by krzysiek: http://dev.netizer.pl/transitions-state-machine-for-rails-3.html

So, I have to switch the state machine to another gem which is not using ActiveRecord stuff. that's why i found stateflow: https://github.com/ryanza/stateflow

My test can run happily ever after.

401 unauthorized error in Heroku

if you receive 401 unauthorized when you execute heroku tasks or rake task, most likely you missed the authentication details to execute some heroku actions which required charging money.

for example, if you use autoscaling gem in some classes. You may see this error when you execute the rake task which requires adding more background worker.

to solve it, just configure the heroku user and password. the following line is extracted from workless README page:

heroku config:add HEROKU_USER=<your username> HEROKU_PASSWORD=<your password>

Monday, March 7, 2011

APIGEE - interactive way to write API call

Everytime you write a web app with social network integrated, you need to read through their API and checkout the format. although most of them are RESTful, which is simple enough. However, it is hard to debug when something is not working. is it the URL ? or is it the parameter ? or is it the permission.

Glad that APIGEE provides an interactive API console. It shows available APIs with url format provided. You can also submit the call and review the response, see if that is what you expect.

For example, find out "what things i liked" using Facebook Graph API: https://apigee.com/console/facebook

It says I need authentication ...

it is really easy to do OAuth call thru APIGEE:

a bit freaking out when it asks for facebook permission ....

OK, try again after login with facebook. Yay ~ done!