Faster module development thanks to Schema

For some time now, my standard operating procedure for writing new modules to client specifications has included a tedious conversion of database tables into schema arrays for use in each module's install file. Usually, I'd begin with copying and pasting into the install file a schema array I'd used elsewhere, and I would modify that array to match up to the table I had thrown together to store the module's data while testing. I would then have to go through the process of uninstalling and re-installing my module to make sure I didn't mess up the schema somewhere.

Lo and behold, at the end of May I learned about the <a href="http://drupal.org/project/schema">Schema</a> module at an event where <em>I</em> was supposed to be the instructor. <img src="http://www.bywombats.com/sites/all/modules/smileys/packs/example/tongue.... /> I must have missed this somewhere along the way when converting all my code to Drupal 6, but it sure does make new module development a lot easier!

The Schema module offers several features for examining the tables in your site's database and comparing them with the schema data defined in your site's module install files. If you use it for a full inspection of your site's database, any table it finds that does not have a matching schema array will be listed on the page with a schema array already built for you. It even includes little placeholders where you should insert table and field descriptions. <img src="http://www.bywombats.com/sites/all/modules/smileys/packs/example/lol.png" />

So, now with the Schema module, I can copy and paste the auto generated schema array into my install files, making module development <em>that</em> much faster. I'm really digging it. One pitfall that you should be aware of: if you're using <b>drupal_write_record()</b> to insert and update rows in these new database tables, you must copy the schema arrays into your module's <b>hook_schema()</b> <em>before</em> it will work. If <b>drupal_write_record()</b> can't find the schema array for the table to which you want to write the record, it will not be able to construct the insert or update query properly. Remember to clear your cache (by visiting the modules page, for example) before testing it so Drupal can rebuild the cached schema data.

I hope it makes your life easier, too! Many thanks to Barry and the other Schema maintainer(s).

Topics: 

Comments

You want more speed? Check out the newest patch for Table Wizard, which _builds your views for you_ and then exports them to code.

http://drupal.org/node/433656

TW leverages Schema to create Views so you don't have to.

Hot hot hot!

Nice, one of the most lame parts of module development extinct.

Perfect!

Thanks for pointing out the hook_schema requirement - I'm sure I'd never have found it by myself.

I have some problems with module development and I have been searching for several hours how to solve everything. Luckily I have found your blog man, you helped me a lot. I use drupal_write_record() and I couldn't insert and update rows in new database tables. With your help everything now works perfectly. Well I have to admit that I am a beginner working with it. Thanks a lot for the useful information!

Regards,

Simon Peterson