Rails schema browser plugin: proof of concept

Posted on February 12, 2008

I have just created my first rails plugin which is a proof of concept interactive graphical viewer for your database schema and relations. It is still very rough around the edges and I have not had as much time as I hoped due to starting a great job at Naked. I'm expecting to make some time for this soon though.

For an example of what you might expect see the schema browser of my own mephisto blog. Tables are drawn based on the tables defined in the current database and relations based on the belongs_to relations defined in the models.

Installing the schema_browser plugin:

cd yourapp/vendor/plugins
git clone git://github.com/tomtt/schema-browser.git
cd ../../
./script/generate schema_browser

Things may work for you this way, but it is also totally possible that you will have to add corresponding routes to config/routes.rb:

  map.connect 'schema_browser', :controller => "schema_browser"
  map.connect 'schema_browser/schema', :controller => "schema_browser", :action => "schema"

If you have to do this, you may also have to restart your server.

The schema browser is now available at http://yoursite.com/schema_browser. Note that currently this would be shown in production as well which may not be something you want. Disabling the schema browser in production is one of many future plans for this plugin.
The xml representation is available at http://yoursite.com/schema_browser/schema

The javascript code to view this was taken from http://ondras.zarovi.cz/sql/ which is an amazing piece of work.

I'd be interested to hear if it is possible to serve up a git repos as if it were an svn repos so that script/plugin install would still work.

Comments
  1. E. James O'KellyFebruary 15, 2008 @ 11:49 AM
    Grats on your first plugin Tom. Looks cool.
  2. ChrisApril 11, 2008 @ 03:16 AM
    I believe your git clone command above is incorrect. Looks like the correct line should be: git clone git://github.com/tomtt/schema-browser.git. Worked for me when I tried that instead. Interesting looking plugin - I like the idea a lot.
  3. ChrisApril 11, 2008 @ 03:21 AM
    Just tried this out and it's returning an "XMLHTTP error: 'Problem retrieving data', sorry..." error. Any thoughts on what might be causing that?
  4. ChrisApril 11, 2008 @ 03:26 AM
    Apologies for the mass commenting however it seems that error is fixed by including 'require "schema_browser"' into environment.rb. It's getting closer to working....
  5. tomttApril 11, 2008 @ 10:59 AM
    @Chris: You are right, I think that was my personal git url. I updated it. The XMLHTTP error is an indication that your schema is not generating correctly. Check if /schema_browser/schema works. The require statement should not be necessary.
  6. sharkApril 11, 2008 @ 11:39 AM
    I have this same problem |:
  7. sharkApril 11, 2008 @ 11:40 AM
    NoMethodError in Schema_browser#schema Showing schema_browser/schema.xml.builder where line #1 raised: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.empty? Extracted source (around line #1): 1: schema_browser = SchemaBrowser.new 2: xml.instruct! 3: xml.sql { 4: schema_browser.tables.each { |table|
  8. MichaelApril 11, 2008 @ 02:27 PM
    I get the same 'nil' problem as others have reported at line 5 of app/controllers/schema_browser_controller.rb
  9. tomttApril 11, 2008 @ 02:52 PM
    @shark&Michael: I'll have a look if you zip me over a working version of your app with the schema-browser installed (of even better: send me a link to a git repos that has it as a branch).
  10. CptOatmealApril 11, 2008 @ 03:52 PM
    Brilliant! I've been looking into doing something like this for a while but kept getting stymied looking for a good layout module. Nice work.
  11. PhilApril 11, 2008 @ 05:54 PM
    Nice; my first plugin was something very similar for Rails 1.1, but this is much more complete.
  12. eugeneApril 11, 2008 @ 06:58 PM
    hey, this is cool! I had same problem until i fixed the environment.rb
  13. Rob OlsonApril 11, 2008 @ 07:34 PM
    Looks sweet. There is an existing gem called Railroad (http://railroad.rubyforge.org/) that does something similar to this, however your plugin looks like it generates somewhat nicer diagrams and doesn't have any dependencies.
  14. nicolae.claudiusclaudius.nicolaeApril 11, 2008 @ 09:00 PM
    Great plugin. Considering to update to lateast version of javascript files from http://ondras.zarovi.cz/sql/ ? Becouse the current files are not the lateast.
  15. tomttApril 11, 2008 @ 11:46 PM
    @Rob: Railroad iirc only produces an image and is not interactive. @Nicolae: thanks for pointing that out, next thing I'll do is update those.
  16. Brian BurridgeApril 15, 2008 @ 04:16 AM
    This looks fantastic and would love to get it to work, but right now I get this: /!\ FAILSAFE /!\ Mon Apr 14 23:13:55 -0400 2008 Status: 500 Internal Server Error Brian
  17. dstarApril 15, 2008 @ 02:57 PM
    One suggestion -- the lines connecting the leftmost boxes run _right_ along the left border in FF3b5. You might want to consider adding in some padding on the left.
  18. tomttApril 15, 2008 @ 03:14 PM
    @dstar: I'm in the process of integrating with the new version of the javascript display library. When that's done all will be better...
  19. sharkApril 16, 2008 @ 01:53 PM
    @tomtt this is simple blog working app with schema-browser installed http://blog.shark.megiteam.pl/blog.tar.bz2
  20. KorishevApril 17, 2008 @ 02:25 AM
    Here locally, it seems that the init.rb in the vendor/plugins/schema-browser directory is empty. Adding the 'require "schema_browser"' there seemed to fix it all up for me...
  21. sharkApril 21, 2008 @ 10:18 AM
    cool, works (: thx
  22. Justin GeorgeApril 22, 2008 @ 12:06 AM
    Amazing job, also, you can add this to git as though it were an SVN external using the following command, from the root of your project. git submodule add git://github.com/tomtt/schema-browser.git vendor/plugins/schema-browser Just a tip, helped me when I got plugin-overload.
  23. steveMay 25, 2008 @ 11:13 PM
    This looks great -- I'd love to give it a try but don't want to wrestle with git just yet. When I try the download link I only get the generators folder and a few files.
  24. tomttMay 28, 2008 @ 05:38 PM
    @steve: now that rails properly supports git as well just a './script/plugin install git://github.com/tomtt/schema-browser.git' should do it
  25. PAcoJune 12, 2008 @ 01:43 AM
    I Love this plugin but there is one problem with belongs_to relationships with no simple plural like belongs_to :people, pligin searches the reflection id as people_id not as person_id
  26. IvorJune 13, 2008 @ 10:51 AM
    How would you disable this in production in an elegant way?
  27. tomttJune 13, 2008 @ 04:14 PM
    @Ivor: I agree it would be good to have a default approach for that but there currently is none. For now I would edit the generated schema_browser controller to only render if the environment is development.
  28. DimitriJune 19, 2008 @ 09:11 PM
    Thanks, its a great plugin :)
Post a comment
Comment