Posted by Tim · 9 Comments
This is why I hate CMS software — their incessant, misguided need to over-abstract every little detail. Take for example Drupal’s system for including additional tables into your existing MySQL database. Drupal would have you create a needlessly complex multidimensional array of all the elements in your new data table schema, created with a separate function and called by another custom Drupal function. Because I guess including the actual SQL to create a table was just too fucking simple.
So here’s Drupal’s way of doing it. I hope you like nested parens!
function timestamp_schema() {
$schema['timestamp'] = array(
'description' => t("Timestamps an ID"),
'fields' => array(
'id' => array(
'type' => 'int',
'length' => 11,
'not null' => true,
),
'uid' => array(
'timestamp' => array(
'type' => 'timestamp',
'not null' => true,
'default' => 'CURRENT_TIMESTAMP',
'on update' => 'NOW()',
),
),
'primary key' => array('id'),
'indexes' => array(
'id' => array('id'),
),
);
return $schema;
}
It’s a data table that stores an ID and a timestamp, which is practically useless, but here’s my point: maybe you caught that I wanted to automatically set the ‘timestamp’ value to CURRENT_TIMESTAMP or even NOW()? But when you try that, Drupal doesn’t set your timestamp to NOW() the function, it wraps it in single quotes, setting your timestamp to the string ‘NOW()’, which isn’t particularly helpful.
Well, Drupal users have already seen this problem. Oh, good. It’s on track to be fixed when Drupal 7 is released. Whenever that is. Another thing I hate about CMSs.
So my solution is this. Already there’s another stupid function you need, just to call the timestamp_scheme() method above. Drupal says make it look like this:
function timestamp_install() {
// Create tables
drupal_install_schema('timestamp');
}
I say make it look like this:
function timestamp_install() {
// Create tables
db_query("CREATE TABLE {timestamp} (
`id` INT(11) NOT NULL,
`timestamp` NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE NOW(),
PRIMARY KEY (id),
INDEX id (id)
)");
}
Oh look, it’s a MySQL query! One you can just plug into a MySQL session with hardly any modification! And you didn’t need to spend an afternoon turning it into some bullshit array. Plus it still looks like MySQL, which you would have to know anyway to produce that mess of nested arrays above. It even supports constants like CURRENT_TIMESTAMP and functions like NOW(). Because it’s still fucking MySQL and not a dumb array.
The uninstall function is the same thing:
function timestamp_uninstall() {
// Drop tables
db_query("DROP TABLE {timestamp}");
}
Now wasn’t that easier?