Generating Entity-Relationship Diagram for Ruby on Rails

Installation
Install graphviz (if not yet)
window http://www.graphviz.org/Download_windows.php
linux sudo apt-get install graphviz
osx brew install graphviz

Add the gem

In the project gemfile add

group :development do
  gem 'rails-erd'
end


Then execute cd PATH_OF_THE_PROJECT && bundle install

Using it
It's as simple as a bundle exec rake erd and it will generate a PDF file in the root of the app.

PostgreSQL :: How do you get the description for all the tables, views and column ?

เอ่อออ!!!!!!!! ต้องทำ Datadict จาก database ที่ใช้งานมาแล้ว 5 ปี (ไม่เคยมี comment หรือ description อะไรมาก่อนด้วย T T )

ใจสู้รึป่าว ไหวมั๊ยบอกมา ... ไม่ไหวก็เลิกซะนะ

ลองค้นๆ ดู ได้ความว่า  comment หรือ description ใน postgresql เนี่ย มันจะเก็บอยู่ใน pg_description.description

แต่ว่าตัวที่จะบอกว่า table, view, column ชื่ออะไรนั้นมันเก็บไว้ที่ pg_class.relname

หาไปหามาได้ query ออกมาเป็นแบบนี้

สำหรับ table

SELECT c.relname As tname, c.relkind AS type, pg_get_userbyid(c.relowner) AS towner,
       t.spcname AS tspace, n.nspname AS sname,  d.description
FROM pg_class As c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_description As d ON (d.objoid = c.oid AND d.objsubid = 0)
WHERE c.relkind IN('r') AND pg_get_userbyid(c.relowner) = 'owner_name'
ORDER BY n.nspname, c.relname ;

สำหรับ column

SELECT c.relname As tname, a.attname As column_name, c.relkind AS type,
       pg_get_userbyid(c.relowner) AS towner, t.spcname AS tspace,
       n.nspname AS sname,  d.description
FROM pg_class As c
INNER JOIN pg_attribute As a ON c.oid = a.attrelid
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_description As d ON (d.objoid = c.oid AND d.objsubid = a.attnum)
WHERE c.relkind IN('r') AND pg_get_userbyid(c.relowner) = 'owner_name'
AND a.attname NOT IN ('tableoid', 'cmax', 'xmax', 'cmin', 'xmin', 'oid', 'ctid')
ORDER BY n.nspname, c.relname ;

credit :: http://www.postgresonline.com/journal/archives/215-Querying-table,-view,-column-and-function-descriptions.html