146 points · by fogus · 15y ago · 63 comments
You may want to re-think that statement.
(and I'm not trolling, I use and like rails, but the database abstraction is amongst the weakest of all popular frameworks)
But assuming that you are sincere, your reasoning is flawed. See, the fact that Rails' orm is in some way badly designed (your words - not mine) doesn't diminish this particular feature. The system could be overall crap, but still have one good thing about it. Besides, the schema-migration has very little to do with the ORM, except for the fact that they both interact with a database.
What you're doing, is called a straw man argument.
Well, this particular feature is a bandaid for an underlying misfeature (manual schema management).
Proposing this as good practice is somewhat akin to proposing "If you design a new operating system then make sure the installation of virus scanners is as easy as on Microsoft Windows".
You may call it a strawman, I'd call it "putting something into perspective".
Besides, the schema-migration has very little to do with the ORM, except for the fact that they both interact with a database.
That's a nice theory but in practice most rails users do use the builtin migration mechanism, for the lack of a better alternative (at least to my knowledge, feel free to correct me).
Consequently I don't think it's unfair to compare that mechanism to the equivalent in similar frameworks - and in that comparison rails loses, big time.
Even with a single server, not relying on IDs being sequental is useful when you want to do a database migration without temporarily shutting down the site. It is much simpler to merge the two databases together.
Some small teams (including some I work with) find it more clear to have 001,002 etc, and just have the convention to get in touch before any schema change, which is overall often a good idea anyway.
Danny you autocorrect on my Android
Mango would be a good nickname for Django + MongoDB
I know the developer - she is awesome!
while(true) {
string current_version = <sha1 of the dictionary table contents>
if (!upgrade_scripts.containsKey(current_version)) {
complain_loudly()
exit(1)
} else {
string script = upgrade_scripts[current_version]
if (script.empty())
break
else
execute(script)
}
}And resist the temptation of hacking the schema directly in your servers, it ends being a larger effort :)
Each change is stored as a script which have names like "<timestamp>_<brief description>.sql", and the timestamps serve as IDs as well in the "changelog" table in the DB. Creating the changelog table is always the first change.
The "migrate" command of the tool then has options for "status", "up" "down" etc., and features to create scripts to go from v1 -> v2. Another nice concept they provide to the mix is adding an "Undo" section to each script so that you can rollback changes and downgrade schemas just as easily as upgrading them.
Nowadays I'm primarly working with non-relational datastores where this seems to be much less of an issue. There we version the entities that we store instead of a schema and that is done entirely in source control as well.
We use a UDF called VersionNumber that returns an integer. When the continuous build processes a new database change script, that function gets modified to increment the value it returns. So you can always call VersionNumber() on any of our databases and know which version you're looking at. The builds actually use it to decide which scripts they need to run to get from where they are to where they need to be.
You already have all the commentary you need in source control and in the change scripts themselves. I don't see a reason to duplicate it in the database as well.
Rails is surprisingly anachronistic here, which probably stems from that awful conceptual separation between model and schema.
Most other platforms have semi-automatic schema evolution that usually works very well (datamapper automigrate, django south, hibernate SchemaUpdate, etc.).
(This is far easier when using SQLite, which has its own tradeoffs.)
http://news.ycombinator.com/item?id=1984609
as I don't have to google around to find out what south is (I haven't used Django yet)
A few years back, I built an installer in Ruby to apply 'plsql modules' to an Oracle database. This was a massive project with > 100 developers at this point and probably heading toward 1M lines of code.
My migrations table worked on modules, so there were a set of migrations per application area, but it was really just an extension of this idea (and I borrowed the idea from Rails too)!
Nathan also does a good job of applying submitted patches quickly.
DbDeploy http://dbdeploy.com/ DbMaintain http://www.dbmaintain.org/
Here is an older article... http://www.davedevelopment.co.uk/2008/04/14/how-to-simple-da...
Further, consider if your company had three deployments of your product. One for bleeding edge testing, one for staging/QA, and live. You'd want the schema version to be consistent across all three schemas.