For those not on or closely monitoring ProNet, Byrne posted this
handy piece of code below. Where was this when I needed it!? ;) I
really wanted to do something like this, but simply didn't have the
bandwidth to make sense of that code without documentation. This will
be quite useful to my future work. It's simple things like this that
make me happy. <tim/>
---
Archived at: http://www.sixapart.com/mailman/private/pronet/2006-
January/003754.html
# CREDIT GOES TO BRAD CHOATE who wrote the repair_class method for me. I
can't wait to use this in Media Manager!!!!
sub initialize_plugin {
my $app = shift;
my $tmpl = $app->init_tmpl('init.tmpl');
$tmpl->param('blog_id' => $app->{query}->param('blog_id'));
require MTAmazon3::Item;
my $diff = $app->repair_class("MTAmazon3::Item");
$tmpl->param('diff' => $diff);
$app->add_breadcrumb("Main Menu",$app->{mtscript_url});
$app->add_breadcrumb("MTAmazon Initialization");
$app->{breadcrumbs}[-1]{is_last} = 1;
$tmpl->param(breadcrumbs => $app->{breadcrumbs});
$tmpl->param(plugin_version => $MT::Plugin::MTAmazon3::VERSION);
$app->l10n_filter($tmpl->output);
}
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;
}