jducoeur: (Default)
jducoeur ([personal profile] jducoeur) wrote2008-11-19 02:47 pm
Entry tags:

Fear of the Unknown

It is occasionally fascinating to step back, watch myself program, and critique it.

Today's observation is that I am still scared of database programming. I'm *doing* it, mind you, but I don't have that innate comfort with it that I do with nice in-memory OO programming. There's a lingering sense that I'm impersonating a real DB programmer.

This is particularly coming up today because I am realizing that I need to do a slightly nasty database refactoring for CommYou. (Currently, Communities are linked to Persons -- that is, a Person is a Member of a Community. I am realizing that this is just making my life harder: Communities should be linked to the Identity that they are imported from, which would make synchronization vastly easier and more efficient.)

In principle, this is straightforward, and it's the sort of thing I do in ordinary code without even blinking. After all, the IDE protects me from most of the simple errors, and the test harness from the rest. But refactoring the DB is just plain *scary* to me. I understand the process just fine, and can outline the steps I need to take in detail with no difficulty, but I find myself procrastinating about actually *doing* it.

I can give some objective reasons for that. For one, the IDE does *not* protect me from errors in this very well. And while it's very straightforward for me to test new data created after this change, testing the migrated data comprehensively is much harder. It would be easy for subtle bugs to sneak in.

But mostly, I suspect I'm just still a bit intimidated by the prospect, because I haven't done it much. Nothing for it but to press through, make the changes, and convince myself that the world didn't end...

[identity profile] metahacker.livejournal.com 2008-11-19 10:55 pm (UTC)(link)
I hate that feeling.

Would it be helpful to have someone with more DB experience take a look at your design?

The thing about DB design for me is that it's so bleeding obvious, and all the black magic really doesn't matter...until you have 1e09 records and it's too late to transition to a new schema. So it feels like there's this steep ramp, where it's too easy at the start, and then too hard (since I don't understand the black magic) very shortly thereafter.
siderea: (Default)

[personal profile] siderea 2008-11-20 12:11 am (UTC)(link)
Huh. I often feel like a db programmer masquerading as an application programmer.

I still spend much of my life in low-grade terror of making changes, however. Man, you guys with IDEs are spoiled.
mindways: (Default)

[personal profile] mindways 2008-11-20 01:45 pm (UTC)(link)
You could do something like what we do at work: whenever mucking with the database schema or doing data conversion of some sort, we write a pair of SQL scripts: one which makes the changes, and one which backs out those changes. If the migration involves dropping data and/or transforming it in a way that can't be reversed, we copy that data over into tables in a storage database (with table name prefixed by the name of the script-pair which creates/uses it, so we know what it's for and can blow away really old ones if we need to).

If you wanted, with such a scheme you could write an automated test for migrations:
1. Back up database;
2. Run update SQL;
3, Run backout SQL;
4. Compare contents of current database to contents of the backup - all your tables should be identical. [System tables are likely to be different.]

For large databases, step #4 could take a while, so you might want to limit it to comparing data only in some specified set of tables (those you touched in the update/rollback), or it might not be something you'd always want to run.