Every module that requires at least one new database table has a hook_schema to define the table layout. It's very handy because you don't have to write the SQL create table syntax yourself. Although useful, it's useless without the implementation of hook_install, and a call to drupal_install_schema to install the schema for you.
So this begs the question, why don't we just automatically install/uninstall the module's hook_schema when the module is installed/uninstalled, without requiring the implementation of hook_install/hook_uninstall? We can see examples of where this would benefit us within the Drupal core code itself. If you look at the source of aggregator.install, you see aggregator_install with one line telling Drupal to install its own module. Shouldn't Drupal be smart enough to just install the schema for us?
Comment | File | Size | Author |
---|---|---|---|
#4 | 312618.patch | 27.55 KB | RobLoach |
#3 | 312618.patch | 26.16 KB | RobLoach |
Comments
Comment #1
Crell CreditAttribution: Crell commentedExcellent question! :-) I'd ask Barry if there was a particular reason he didn't do that originally. If so, it should be documented. Can someone point him in this direction?
Comment #2
RobLoachThing brings up another issue of system_install installing the schemas of the 'system', 'filter', 'block', 'user', 'node', 'comment', and 'taxonomy' modules.
Comment #3
RobLoachTests not running? Hmmm.....
Comment #4
RobLoachWhoops, fixed some things. You can install and uninstall modules, but tests don't run and I'm not quite sure why.... Going to have to investigate later on.
Comment #5
bjaspan CreditAttribution: bjaspan commentedWe discussed this when developing the initial schema api patch. I think the main reason we left it out was just to have one fewer thing change in what was already a pretty large patch.
One advantage to not installing and uninstalling the schema automatically is that it lets a module control whether other actions it wants to perform occur before or after the tables are created or dropped. Sure, the current approach requires a one-line hook_un/install, but that really is not a particularly big burden.
I could see an argument that if a module has hook_schema but not hook_un/install, then the schema is automatically created/dropped, but if there is a hook_un/install, it is responsible to creating/dropping the tables itself as it is now.
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #7
David_Rothstein CreditAttribution: David_Rothstein commentedThis was recently committed in #306151: Automatically install/uninstall schema, so if @bjaspan's concerns are still relevant it might be worth repeating them over there. I can't at the moment think of a case where a module would really need to control the order that carefully, but maybe there is...
I see the above patch also has code for things like creating a new user_install() function, so if that is still relevant, this issue could be repurposed and reopened.