Pathologically Eclectic Rubbish Lister | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Lately I've been scratching my head over how to most effectively keep constants in code and constants in a database table synchronized. Since I haven't found any relevant discussions via search, I thought I'd share my thinking here and see how other monks might approach this problem.
As a contrived example, imagine we have a database table PAYMENT_TYPES that contains the following: PAYMENT_TYPE_ID | PAYMENT_TYPE 1 | VISA 2 | MASTERCARD 3 | AMERICAN EXPRESS And somewhere in our code we have corresponding constants: or, more likely, the equivalent enum. Since this enumeration lives in two places, how do we ensure that they stay in sync? (Alternately, can we find a way to eliminate one or the other?) Some approaches that come to mind are: 1. Keep code and data in sync manually. 2. Eliminate the enumeration from the database and keep it in code only. 3. Eliminate the enumeration from the code and keep it in the database only. 4. Keep the enumeration in the database and query it on program startup to define appropriate variables. 5. Keep the official enumeration in the database; query the database to generate code that contains appropriate constants/enums. 6. Don't worry about it! I think I'm torn between #1 and #5 right now. #1 has the obvious virtue of simplicity, but I worry about maintainability -- my experience is that if it's possible to forgot to do something, someone (myself included) *will* forgot to do it sooner or later. Admittedly, since I'm pretty rigorous about test suites, it wouldn't be too hard to detect this before deployment. #5 seems a little bit more pure, because the enumeration really lives in only one place, but it does rely on code generation being trigged when the database changes (which is probably not a big deal since I do "builds" to my test environment with ANT anyway). Then there's the part of me that's convinced that the reasoning behind #6 makes this a moot point anyway. Any other monks who have contemplated this issue? I'd be curious to hear what pros and cons you see in the various approaches (or some totally new alternatives)... and how you ultimately resolved it. $perlmonks{seattlejohn} = 'John Clyman'; In reply to Synchronizing constants/enums in database and code by seattlejohn
|
|