I've created quite a few tables and am going to move their creation into .install files.

I was thinking it would be easier if there was a tool that could just analyze my tables and show the code that should go in hook_install(). Similar to how show create table gives the statement to create the table.

is there anything like this out there?

Comments

jordojuice’s picture

If you're talking about creating tables for use in Drupal modules then in most cases you shouldn't be using queries to create the tables at all and they shouldn't be created in hook_install(). You should be using hook_schema() (http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_schema/7) to notify the system of your tables. It gives you a way of setting up your tables without needing to know the SQL required anyways. In Drupal 7, the tables defined in hook_schema() are automatically created. But hook_install() can be used for inserting data into those or other tables if necessary. In short, no, there's no way to automatically generate that code. But setting up the arrays in hook_schema() is pretty straightforward and Drupal should know they exist anyways.

jaypan’s picture

I think he's looking for a way to reverse engineer the hook_schema arrays. He already built the tables, and now wants to include the schema definition in his install file, but wants a tool that will analyze the db and create the rays for him.

Contact me to contract me for D7 -> D10/11 migrations.

jeditdog’s picture

thanks, I've been thinking I should use hook_schema instead. Sounds like the Schema module should meet my needs according to the posts below.

Web Assistant’s picture

Would the Schema module do what your describing? It says the module examines the live database and creates Schema API data structures for all tables that match the live database..

Web Assistant’s picture

I can confirm that the Schema module does show you the schema structure for tables (even non-Drupal tables), so you can just copy and paste into your .install file.

jeditdog’s picture

I'm going to check it out and see. It does sound like what I'm looking for.

Ken Ficara’s picture

Found this while Googling with the same need -- and yes, the schema module is the right answer. But note that it's got some weird issues. You have to apply a series of patches from this issue to prevent a series of undefined-index errors. You'll also get errors about any datetime fields in your database, but if you just need to grab schema definitions for hook_schema, you can ignore these and just copy them from the Inspect tab. Based on the issues, I disabled the module as soon as I was done with it. It's a great tool but doesn't seem quite ready to use on a production site.

jeditdog’s picture

good analysis Ken. I use it in a test system is all... it doesn't get uploaded for my production system. It has been working out well though; thanks to everyone for pointing me in the right direction.