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 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 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 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.
Comment #23
smustgrave commentedThank you for creating this issue to improve Drupal.
We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #24
smustgrave commentedSince there's been no follow up in 3+ months going to close this one out. If still valid task please re-open.
Thanks