Good Migrations

I was out last week, but finally got my schema in a Rails friendly format and the scaffolding utility is working great! I thought I'd hit on some of the pain points I experienced during this process and how I worked through them.
  • MySql stinks when you need to change the schema of a database containing data. To make things easy, I've been working with an empty database and using the MySQL administrator to capture the SQL. The plan was to run this SQL against existing databases to upgrade them. This process doesn't work like I'd hoped.

    For some columns, the SQL query fails when I try to change a column name (like changing the primary key name to the Rails default 'id'). There are no foreign key constraints on this field and it does not fail on every table where the column name must change. I can't figure out why MySQL bombs on these specific statements. Maybe it's because these table have large row counts (~1M rows)??? Luckily, I won't be creating models for these tables (they are not used in my application), so I'll ignore upgrading these for now.

    I've run into similar problems with other update scripts (but these don't fail consistently). I had thought these other statements encountered some sort of race condition, but I'm not sure.

  • After I updated the schema I went back to DBDesigner to update the schema diagram. This tool does not look at foreign key constraints to visualize relationships (it builds relationships based on column names. Since I now have 33 'id' columns, it gets very confused). I grabbed the alpha release of MySQL Workbench, but found it painful to use.

    Eventually, I found SchemaSpy which uses Graphviz to create some great looking diagrams. What I like most is that it allows you to drill down to specific tables for a localized view of how things are connected - leaving the rest of the clutter behind. Also, it was very easy to setup and use!! Awesome tool!!

  • Finally, I used the Rake db:schema:dump utility to create a schema.rb file. I'll be able to use this as version 1 of my migration file. Like everything else I'm finding in Rails, this utility saved me a lot of time!

Comments

Popular posts from this blog

I Believe...

FRail :include

Performance Tuning JCAPS - Part 2