views:

95

answers:

3

I'm working on a project with developers who have not worked with Ruby OR Rails before.

They have created a schema that is too complicated, in my opinion. The schema has 117 tables, and obtaining the simplest piece of information would require traversing/joining 7 tabels...and of course, there's no "main" table that serves as a sort of key between them. The schema renders many of the rails tools like 'find' method, and many of the has_many/belongs to relationships almost useless. And coding for all of these relationships will likely be more time-consuming than we have the money to code for.

THE QUESTION:

Assuming you are VERY convinced (IMHO...hehe) that the schema is not ideal, and there are multiple ways to represent the domain, how would you argue FOR simplifying the schema (aside from what I've already said)?

+2  A: 

Your DB schema should reflect the domain and its relationships.

De-normalisation should only be done when you have measured that there is a performance problem.

7 joins is not excessive or bad, provided you have good indexes in place.

Mitch Wheat
Ayay...:-/...I was hoping not to get into the discussion that "there are multiple ways to describe a domain." BUT, I guess I should have made that part of the question...I'll remove the word "denormalize" from the question and edit accordingly.
btelles
+2  A: 

I'll stand up in 2 roles here

  • DBA: Database admin/designer.
  • Dev: Application developer.

I assume the DBA is a person who really know all the Database tricks. Reaallyy Knows.


DBA:
Database is the key of the application and should have predefined structure in order to serve its purpose well and with best performance.
If you cannot use random schema (which is reasonably normalised and good) then the tools are wrong.

Dev:
The database is just a data store, so we need to keep it simple and concentrate on the application.

DBA:
Database is not a store it is the core of the application. There is no application without database.

Dev:
No. The application is the core. There is no application without the front-end and the business logic applied to it.

And the war begins...


Both points are valid and it is always trade off.

If the database will ONLY be used by RoR, then you can use it more like a simple store.
If the DB can be used by other application OR it will be used with large amount of data and high traffic it must enforce some best practices.

Generally there is no way you can disagree with DBA.
But they can understand your situation and might allow you to loose the standards a bit so you could be more productive.

So you need to work closely, together.

And you need to talk to each other to explain and prove the point why database should be like this or that.
Otherwise, the team is broken and project can be failure with hight probability.


ActiveRecord is a very handy tool. But it cannot do everything for you. It does not provide Database structure by default that you expect exactly. So it should be tuned.

On the other side. If DBA can accept that all PKs are Auto incremented integers that would make Developer's life easier (ActiveRecord does it by default).

On the other side, if developers would accept some of DBA constraints it would make DBA's life easier.


Now to answer your question:

how would you argue FOR simplifying the schema

Do not argue. Meet the team and deliver the message and point on WHY it should be done.
Maybe it really shouldn't and you don't know all the things, maybe they are not aware of something.

You could agree on the general structure of the database AND try to describe it using RoR migrations as a meta language.

This way they would see the general picture, and you would use your great ActiveRecords. And also everybody would be on the same page.

Dmytrii Nagirniak
Very well put, Dmitriy. I especially like your zen attitude toward 'arguing'
btelles
+2  A: 

The general way to make this argument up the chain is based on cost. If you do things simply, there will be less code and fewer bugs. The system will be able to be built more quickly, or with more features, and thus will create more ROI. If you can get the money manager on board with that approach, he or she may let you dictate terms to the team. There is the counterargument that extreme over-normalization prevents bad data, but I have found that this is not the case, as the complexity it engenders tends to lead to more errors and more database code in general.

The architectural and technical argument here is simple. You have decided to use Ruby on Rails. Therefore you have decided to use the ActiveRecord pattern. The ActiveRecord pattern is driven by having the database tables match the object model. That's the pattern in use here, and in many other places, so the best practices they are trying to apply for extreme data normalization simply do not apply. Buy a copy of Patterns of Enterprise Application Architecture and put the little red bookmark at page 160 so they can understand how the pattern works from the architecture perspective.

What the DBA types tend to be unaware of is how much work ActiveRecord does for you, from query generation, cascading deletes, optimistic locking, auto populated columns, versioning (with acts_as_versioned), soft deletes (with acts_as_paranoid), etc. There is a strong argument to use well tested, community supported library functions to perform these operations versus custom code that must be maintained by a DBA.

The real issue with DBAs is then that they need some work to do. Let them focus on monitoring performance, finding slow queries in the code, creating indexes and doing backups.

If you end up losing the political battle for a sane schema, you may want to consider switching to DataMapper. It's the next pattern in PoEAA. The other thing you may be able to get them to do is to create views in the database that correspond to the object model. This way, you could use many of the finding capabilities in the ActiveRecord model based on the views, but have custom insert, update, and delete methods.

MattMcKnight