Politics, Programming and Possibilities
27 Apr
Note: Please see the updated ColumnComments v.1.2 post for the latest with bug fixes.
Here’s a small plugin that may be useful for documenting your database. Based on Dave Thomas’s AnnotateModels plugin, this plugin goes one step further and allows you to store comments on each column in the database (MySQL only).
Since the Rails philosophy uses the database as the “authoritative source” for the data model, it’s been troublesome to know where to store helpful developer comments. If you write them in the model files (where it would make sense to do it), you repeat a lot of work because the models don’t store column information.
Dave Thomas solved part of this problem by introducing the AnnotateModels plugin–with it you can take the basic information about each table and auto-generate a comment-header inside each model file. But still, where would you put comments? If you edit the auto-generated header then you run the risk of accidentally (or by necessity) overwriting your own comments when you re-generate the headers.
The ColumnComments plugin makes the necessary modifications to ActiveRecord to allow for an optional :comment in your table migrations. In addition, it includes the same feature provided by the AnnotateModels plugin (rake annotate_models), but also includes the database-stored comments in the auto-generated header.
Here’s an example of the output:
# Schema as of Thu Apr 27 12:07:32 MDT 2006 (schema version 10)
#
# id integer(11) not null
# login string(255)
# A user-generated login name.
#
# salted_password string(40) default(), not null
# Encrypted using SHA1 and the "salt" column
#
# email string(60) default(), not null
# Mandatory email address for each user
There are two times when you might want to add comments to your columns: while you create a new table or after a table already exists.
While You Create a New Table
def self.up
create_table "users" do |t|
t.column "first_name", :string, :comment => "The member's given name."
t.column "last_name", :string, :comment => "The member's family name."
end
end
After a Table Already Exists
def self.up
column_comment "tags", "id", "The unique ID of any tag in the system."
end
Or, if you want to mass-assign comments (for example, just after you install the plugin on an already-built app):
def self.up
column_comments({
:users => {:first_name => "User's given name", :last_name => "Family name"},
:tags => {:id => "Tag IDentifier"}})
end
syncPEOPLE maintains an open source subversion repository for plugins and other useful code that we’ve open-sourced. ColumnComments is released under the MIT license and can be installed from:
svn://syncid.textdriven.com/svn/opensource/column_comments/trunk
8 Responses for "ColumnComments Rails Plugin"
Great job, Duane! I was thinking of creating something like this myself. Keeping little notes like this definitely makes a lot of sense.
I’ve managed to port this to postgresql.
Patch here: http://tarmo.itech.ee/column_comments_postgresql.patch
As this is my first patch for activerecord I can’t guarantee that it’s perfect, but it seems to work fine here.
Cool! Thanks, Tarmo. Is this something that could work together with the mysql code?
I should work together, although there is an overhead, I think it sets the comments twice for mysql. As you can see it sets the comments at the end of the create_tabel function because postgresql ‘CREATE TABLE’ does not allow directly specifiying comments lik mysql. Perhaps you could remove the ‘add_column_options!’ method and only rely on the ‘column_comment’ method? otherwise postgresql requires a different create_table method.
[...] Recently however, with the development of the ColumnComments plugin, I decided to move on once again in search of the Ultimate Tool. I needed something that would give me the ability to add comments to mysql columns, preferably in a nice and intuitive GUI. I tried Navicat, but couldn’t figure out how to add comments. [...]
Great stuff. I do have one problem, however. I can get column_comments to work just fine, but comments do not appear when I include them in table creation.
Example
create_table “blah” do |t|
t.column “abbr”, :string,:comment => “Short-name (abbreviation) for quick-reference.”
…
As far as I can tell, options[:comment] is empty in this case. Any ideas? I’m using 1.1.2, frozen (all source in vendor dir).
Umm… the patch Tarmo sent is not yet applied to trunk. I think it would not be hard to apply it and eliminate the overhead for the whole world to use?
Mislav: I talked with Tarmo about this and he said the patch, by enabling postgres, may not work well with MySQL. I haven’t had the time to make the adjustments myself to ensure both databases work.
Leave a reply