db_create_table currently is:
function db_create_table(&$ret, $table) {
$statements = db_create_table_sql($table);
foreach ($statements as $statement) {
$ret[] = update_sql($statement);
}
}
I think modules should be able to inject additional SQL statements to create a table. Something like:
function db_create_table(&$ret, $table) {
$statements = array_merge(db_create_table_sql($table), module_invoke_all('create_table', $table));
foreach ($statements as $statement) {
$ret[] = update_sql($statement);
}
}
A similar hook would be necessary for db_drop_table. Not sure about db_add/drop_field, key, or index.
Why do I want this? One example: I think it would make it possible to implement foreign keys incrementally via an optional module. Any module could use hook_schema_alter() to add foreign key info and then foreign_keys.module (or whatever) would then hook_create_table() to add ALTER TABLE foo ADD FOREIGN KEY... statements, and use the new deletion API to provide cascade on delete semantics when necessary.
I'll think about this more and, if I still like the idea in the morning, will submit a patch.
Comments
Comment #1
bjaspan CreditAttribution: bjaspan commentedComment #2
webchickFWIW, I would prefer to see actual native foreign key support in core than using a hook to provide this information. Which is not to say that we couldn't add the hook if there was another reason to do that, just that I don't think this particular use case is very compelling. Let's fix this stuff in core, not contrib.
That said, I don't know if this issue is any longer valid with the new db layer.
Comment #3
Crell CreditAttribution: Crell commentedI'm still planning a major overhaul of Schema API in order to make it internally cleaner and to exterminate update_sql(), so let's hold off on this until then. My gut reaction is that hook_schema_alter() is sufficient, especially if someone takes care of #111011: Add foreign keys to core (hint hint).
Comment #4
Crell CreditAttribution: Crell commentedEven more postponed...
Comment #5
Liam MorlandThis would be a step towards solving the foreign key aspect of this request: #2254131: DBTNG should be able to create foreign keys.
Comment #6
mgiffordBarrier removed I think.
Comment #7
webchickWe're no longer adding "nice to haves" to Drupal 8.0.