Probably this article is about something that everyone in the world knows. Problem is I did mistakes about this TWO times, without learning from them, so writing down it could be useful for this head of mine to remember how to do things right.
Writing a timestamp whenever a database row is created or updated is a standard task and everyone need it. Many database systems can do it by themselves (MySQL), dumber ones cannot (SQLite). Are we interested in this? Obviously not, DBIX::Class (the one and only) give us a good abstraction about that and it's easy to use.
When you know the trap to avoid.
Syntax to have a timestamp that set itself on create is really simple. In your model class:
__PACKAGE__->add_columns(
"timestamp",
{ data_type => "datetime", is_nullable => 0, set_on_create => 1 },
);
It's something so used that there's a flag to manage it. You set it to true, the timestamp will be written.
IF and only IF you remember this:
__PACKAGE__->load_components("TimeStamp");
and you installed DBIx::Class::Timestamp!
Are you hearing the Wotan worshipper roaring? Why should he? It's a procedure so clean!
Well, perl is a language that worship optimization (more than Wotan! Grrrrr). Often optimization means a fine grane library control. So in many important modules you see methods different from use... to load libraries. Most of these methods have a little side-effect: they don't give errors.
In this particular case you can write the first piece of code, forget the second or write the second and forget to install the library. The code will run. What will happen about the timestamp? Absolutly nothing.
But now I wrote this. Now this thing is encarved in my blog and in my mind. I will never be tricked again.
What were we talking about?
No comments:
Post a Comment