One of the big problems in working with databases is handling changes over time. The issue is that downstream users depend on the data model they are using.
You can isolate downstream users by having them rely on views, rather than directly accessing the base tables.
For instance, you might start with a table that has a user table with an address
column. Folks downstream access the table as:
select u.*
from users u;
Later, you realize that addresses can change and you want to implement a type-2 table for addresses -- that is, a separate table with an effective and end date on each record. Well, existing code will break.
Instead, if the downstream users are using:
select u.*
from v_users u;
Then the view only needs to change from:
create view v_users as
select u.user_id, u.address
from users u;
to:
create view v_users as
select u.user_id
from users u join
user_addresses ua
on ua.user_id = u.user_id and
current_timestamp >= ua.eff_ts and
(current_timestamp < ua.end_ts or ua.end_ts is null);
Voila! Nothing breaks downstream.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…