Category: Ruby


Review of AssemblySys dataServices

On a large data migration project that I am currently spearheading, we have a large installed userbase of over 2 million users running on a social networking engine. The schema has been redesigned from scratch, and code is being written to match the new schema, using the all-powerful MySQL database as the system to manage all that data.

Since this social network is global, we need good and reliable location information. The current location model is flawed and full of holes, so we have chosen AssemblySys‘ data to replace it.

We are not using AssemblySys’ schema, as we’ve rolled our own. I’ve designed our new schema to be hierarchial in nature, treating all locations on the planet as ‘nodes’ with a tree relationship, with “Earth” being the parent of all nodes. This model allows us to account for all countries and their idiosyncratic ways they divy up their adminstrative divisions, which to say the least varies a lot.

Currently AssemblySys does not have strong support for postal codes, and only about 5 countries use postal codes anyway. However, I was able to secure zip codes from a different vendor and graft them in to our location model.

The AssemblySys location database is quite through and complete, with accurate geodata for the cities. In fact, it is so complete it even lists some towns that don’t show up on Google Maps! I verified that some of these obscurities I found do, in fact, exist.

And I uncovered a good bit of curious geographical trivia, like the fact that there are 5 towns in Kentucky called “Boston”. Must be a nightmare for the Post Office there! I also found there is a town called “Philadelphia” in South Africa! At first, I thought these must be errors, but I verified that these obscure towns do indeed exist.

Next came the task of transforming their location data to our model. This is  where I had the most problems, because their data is not arranged in the nice, clean, hierarchical fashion our model is. In fact, it’s laid out in a very cumbersome fashion requiring a number of sub-keys to cull out the proper hierarchy.

To their credit, though, AssemblySys was quick to respond to my questions about how to access their data and shot back examples that was very helpful with the effort. But I felt their model was way too complicated than it needed to be, and perhaps could have used a bit more normalization. But I was able to do the transform after a few days of wrestling with it.

Overall, I am pleased with the quality of the AssemblySys product. I am not happy with their schema layout and the rather obtuse and complicated queries to cull out the structure. However, perhaps most users will use their database as is and perhaps it works better in that context, though the queries can get quite cumbersome from my estimation. The service is good, though completely email-based. The price is reasonable and the data is accurate.


Using Ruby to Migrate Databases

If you deal with databases for a living, eventually you’ll come across cases where you’ll need to migrate a lot of data from one schema to another. I am not just talking about migrating from one different type of database to another, like from Oracle to MySQL, but from, for instance, a badly-designed schema to one more expertly crafted.

If there are minor differences between the source and target schema, this is a trivial affair. On the other hand, if the schema is completely different, this can be quite a challenge. Moreover, the database being migrated might represent a high-demand website that will need to be done with little or no downtime, with lots of planning and preparation to boot. You may be interacting with the application developers, the systems crew, and juggling tight deadlines as well.

Well, as you may have guessed, I have described some of the roles I now play at a leading social networking company. We are indeed in the midst of creating the “NextGen” product — a complete rewrite and redesign. The new system is designed with modularity and scalability in mind. The old system we are transitioning from was created when the company was much smaller and had 2 orders of magnitude or more less demand. Suffice it to say, it has all the appearances of being crafted by a bunch of “juniors” that just quickly browsed through “PHP for Dummies”, “Database Design for Idiots”, and the like the night prior. That the aging application still works at all is seen as the “8th Wonder of the World”, but to it’s credit it brings in millions in revenue despite all of its faults.

I am an “old veteran” when it comes to software development. In my “advance age”, I’ve decided to do databases as something that I’ve not done before in my 30-year career as software developer. The nice thing is that I find much I’ve learned about algorithms and data structures can also be applied to schema design. It also helps with interacting with the applications development team as I can relate to what their needs are and “bridge the gap”, as it were between the code and the database.

I have chosen Ruby out of all the languages I know — Python, Perl, PHP, C++,Java, etc. — because of it’s expressive power and meta-programming capabilities that most of the other languages don’t either do well, or lack  a clean syntax to accomplish the same.

First, let me speak of my general approach to data migration. You have your source and destination databases. Of your source databases, you will obviously have the main database containing the enterprise’s lifeblood information. Some of that data will relate directly to customer/account activity; some may relate to configuration of how that data is handled; other data may serve as a reference, such as a zip-code database.

Similarly, you will also have target databases, with the same type of data, but organized differently — hopefully more efficiently. Also, what may have been denormalized in the source database you might choose to normalize it in the target, or vice-versa. Perhaps password for user accounts were in plaintext in the source and now you need to md5 them in the target.  Perhaps there were a fixed number of columns in the source tables representing some resource that you wish to store as separate rows in the target for added flexibility and expandability. Again, if you are only dealing with a couple of tables, it’s trivial to do the migration. If, on the other hand, you are dealing with dozens of tables, the problem explodes in complexity.

Since I want to illustrate doing a migration, I don’t want to bog you down with a complex schema; instead, I will take a simple example. Suppose you have a picture display site where each picture was represented by a column in the users table, and you need to migrate this to a more flexible system that will allow any number of pictures per user. If you have 10 million users in this table, doing a ALTER TABLE every  time needed to expand on the number of pictures would be just plain silly.

1
2
3
4
5
6
7
8
9
10
CREATE TABLE old_accounts (
  id INT auto_increment primary key,
  name varchar(100) not null,
  email varchar(100),
  picture1 varchar(100),
  picture2  varchar(100),
  picture3  varchar(100),
  picture4  varchar(100),
  picture5  varchar(100)
) ENGINE=MyISAM;

And here is the new schema we wish to migrate this to:

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE new_account(
  userID INT auto_increment primary key,
  given varchar(50) not null,
  sur varchar(50) not null,
  email varchar(100)
  ) ENGINE=InnoDB;
 
CREATE TABLE pictures (
  pictureID int not null auto_increment,
  userID INT not null,
  url varchar(100) not null,
  unique index(userID, url)
) ENGINE = InnoDB;

I have deliberately left out the foreign key specifications for clarity — and some would argue it would be a nasty performance hit under some circumstances, though I’ve not run into that problem personally.

I have written a complete Ruby framework specifically for migration, but as of the time of this writing, that code is proprietary and not yet released to open-source, though eventually I may do that if I get clearance. But basically, I use Ruby classes to represent a “unit” of migration — normally a single source table to one or more target tables. So, using my Migration framework, here’s what this migration would look like in Ruby:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class UserMigration < Migration
    def migrate_map
        @src_table = {
             :old_accounts => {:PK => :id}
        }
        @dest_table = {
            :new_account = {
                :PK => :userID,
                :id => :userID,
                :name => :given,
                :email => :email,
            },
 
            :pictures => {
                :PK => :pictureID,
                :FK => {:new_account => {:userID => :userID}}
                :picture1 => :url,
            },
 
           :pictures => {
                :PK => :pictureID,
                :FK => {:new_account => {:userID => :userID}}
                :picture2 => :url,
            }, ... 
        }
    end
end

Well, that’s it — almost, and there’s a problem in the Ruby code that you will catch right off the bat if you know Ruby — and I think that if you look at it for a bit, you can figure out what’s going on here. So I’ll leave that as an exercise for you to mull over. You don’t really need to know Ruby at all to understand what’s going on here, and that’s the bit I like about Ruby. You can use it as a type of “meta-language” if you know what you’re doing.