Use Drush to compare your database with your schema

Topics: 

The Schema module, which I am now maintaining, lets you compare your actual database tables against the schema that your database is supposed to match. It's a useful tool that tells you when you've got extra tables (say from a previous version of Drupal), missing tables (that were mistakenly or maliciously dropped) or mismatched tables, ones whose implementations do not match their definitions.

I've recently added a feature which provides these lists through Drush for use on the command line or in scripts.

The Drush command is schema-compare. To get it, you need a version of Schema that is at least 7.x-1.0-beta4. It has the following four (4) subcommands:

  • match
  • mismatch
  • missing
  • extra

drush schema-compare match will list all tables in the database whose schemas match their schema definitions.

drush schema-compare mismatch will list all tables in the database whose schemas do not match their schema definitions.

drush schema-compare missing will list all missing tables from the database existing in schema definitions.

drush schema-compare extra will list all tables in the database whose schema definitions do not exist.

You can then make use of standard command-line tools to find out more information or act on the list items. For example, you can:

  • Pipe these through "wc" to determine the numbers of each.
  • Write a script to drop the list of extra tables. (However, I'd recommend performing an analysis on each to these tables to make sure that they contain no useful data before removing them.)

Enjoy!

Add new comment

You must have Javascript enabled to use this form.