The Plugin Walkthrough: Custom Tables and Backwards Compatibility
We've already looked at how to create tables for our plugin in Movable Type's database and I showed you a few ways to have Movable Type automatically create and maintain those tables for you. Unfortunately, the techniques we discussed last time are compatible with Movable Type 3.31 and above.
If you want to maintain backwards compatibility for Movable Type 3.2 users, Brad Choate created the following subroutine which you can use in your plugin's MT::App subclass
sub repair_class {
my $app = shift;
my ($class) = @_;
require MT::Upgrade;
my $driver = MT::Object->driver;
my $diff = MT::Upgrade->class_diff($class);
print STDERR "diff=$diff\n";
if ($diff) {
my @stmt;
if ($diff->{fix}) {
@stmt = $driver->fix_class($class);
} else {
if ($diff->{add}) {
push @stmt, $driver->add_column($class, $_->{name})
foreach @{$diff->{add}};
}
if ($diff->{alter}) {
push @stmt, $driver->alter_column($class, $_->{name})
foreach @{$diff->{alter}};
}
if ($diff->{drop}) {
push @stmt, $driver->drop_column($class, $_->{name})
foreach @{$diff->{drop}};
}
}
if (@stmt) {
$driver->sql(\@stmt) or return $app->error($driver->errstr);
}
}
return $diff;
}
Call it by passing it your MT::Object subclass like so
$app->repair_class('MT::Foo');
$app->repair_class('MT::Bar');
You will, of course, need to create the interface to display the results of this routine. This thread on mt-dev provides more information on using this routine.
To see an example of this routine in use, download MediaManager 1.08 (the version compatible with Movable Type 3.2). This code can be found in MediaManager::Install (plugins/MediaManager/lib/MediaManager/Install.pm)

Leave a comment