Show
Ignore:
Timestamp:
04/17/08 17:50:50 (20 months ago)
Author:
mpaschal
Message:

Check in MT::Tool helper
Convert report-slow-requests and upgrade tools to use MT::Tool
BugzID: 67410

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-35/tools/upgrade

    r1174 r1950  
    77# $Id$ 
    88 
     9package MT::Tool::Upgrade; 
    910use strict; 
    1011 
    11 use Getopt::Long; 
     12use lib  qw( extlib lib ); 
     13use base qw( MT::Tool ); 
     14 
    1215use Carp qw(confess); 
    13 GetOptions("dryrun", \my($dryrun), "name:s", \my($name), "sql", \my($sqlonly)); 
    14  
    15 use lib 'extlib'; 
    16 use lib 'lib'; 
    17  
    18 use MT; 
    1916use MT::Upgrade; 
    2017 
    21 my $mt = new MT(Config => 'mt.cfg') or die MT->errstr; 
     18sub usage { '[--dryrun] [--sql] [--name <name>]' } 
    2219 
    23 MT->add_callback('MT::Upgrade::SQL', 1, undef, \&sql_cb) if $sqlonly; 
     20sub help { 
     21    return q{ 
     22        Installs or upgrades a database to the current MT schema. 
    2423 
    25 $dryrun = 1 if $sqlonly; 
    26  
    27 if (!$sqlonly) { 
    28     print "upgrade -- A command line tool for upgrading the schema for Movable Type.\n"; 
    29     print "(Non-destructive mode)\n" if $dryrun; 
     24        --dryrun         Determine the upgrade steps required without 
     25                         executing any changes. 
     26        --sql            Report the SQL that would be performed instead 
     27                         of executing it. 
     28        --name <name>    The author as whom to perform the upgrade steps. 
     29                         Required when performing an upgrade (not at 
     30                         initial install). 
     31    }; 
    3032} 
    3133 
    32 my $install; 
    33 my $driver = MT::Object->driver; 
    34 if (!$driver || !$driver->table_exists('MT::Author')) { 
    35     $install = 1; 
     34my ($dryrun, $name, $sqlonly); 
     35 
     36sub options { 
     37    return ( 
     38        'dryrun!' => \$dryrun, 
     39        'sql!'    => \$sqlonly, 
     40        'name=s'  => \$name, 
     41    ); 
    3642} 
    3743 
    38 unless ($install || $name) { 
    39     print "Please set username to set superuser at upgrading.  cf: upgrade --name Melody\n"; 
    40     exit; 
    41 } 
    4244 
    43 my $author_id; 
    44 if (!$install && $name) { 
    45    require MT::BasicAuthor; 
    46    my $a = MT::BasicAuthor->load({name => $name}); 
    47    die "Not found user $name:" . MT::BasicAuthor->errstr if !$a; 
    48    $author_id = $a->id || 0; 
    49 } 
     45sub main { 
     46    my $class = shift; 
     47    my ($verbose) = $class->SUPER::main(@_); 
    5048 
    51 my $updated = MT::Upgrade->do_upgrade(App => 'main',  
    52                                       DryRun => $dryrun, 
    53                                       Install => $install, 
    54                                       SuperUser => $author_id, 
    55                                       CLI => 1, 
    56                                       ); 
     49    if ($sqlonly) { 
     50        $dryrun = 1; 
     51        MT->add_callback('MT::Upgrade::SQL', 1, undef, \&sql_cb); 
     52    } 
     53    else { 
     54        print "upgrade -- A command line tool for upgrading the schema for Movable Type.\n"; 
     55        print "(Non-destructive mode)\n" if $dryrun; 
     56    } 
    5757 
    58 if ($install) { 
    59     print "Installation complete.\n"; 
    60 } else { 
    61     print "Upgrade complete!\n" if !$dryrun && $updated; 
    62     print "Your schema is up to date already.\n" if defined $updated && !$updated; 
     58    my $install; 
     59    my $driver = MT::Object->driver; 
     60    if (!$driver || !$driver->table_exists('MT::Author')) { 
     61        $install = 1; 
     62    } 
     63 
     64    unless ($install || $name) { 
     65        print "Please set username to set superuser at upgrading.  cf: upgrade --name Melody\n"; 
     66        exit; 
     67    } 
     68 
     69    my $author_id; 
     70    if (!$install && $name) { 
     71        require MT::BasicAuthor; 
     72        my $a = MT::BasicAuthor->load({name => $name}) 
     73            or die "Not found user $name:" . MT::BasicAuthor->errstr; 
     74        $author_id = $a->id; 
     75    } 
     76 
     77    my $updated = MT::Upgrade->do_upgrade( 
     78        App       => __PACKAGE__,  
     79        DryRun    => $dryrun, 
     80        Install   => $install, 
     81        SuperUser => $author_id, 
     82        CLI       => 1, 
     83    ); 
     84 
     85    if ($install) { 
     86        print "Installation complete.\n"; 
     87    } else { 
     88        print "Upgrade complete!\n" if !$dryrun && $updated; 
     89        print "Your schema is up to date already.\n" if defined $updated && !$updated; 
     90    } 
    6391} 
    6492 
     
    6896    print "\t* " . $msg . "\n" unless $sqlonly; 
    6997} 
     98 
    7099sub error { 
    71100    my $pkg = shift; 
     
    73102    confess $err; 
    74103} 
     104 
    75105sub sql_cb { 
    76106    my $cb = shift; 
     
    78108    print "$stmt\n"; 
    79109} 
     110 
     111 
     112__PACKAGE__->main() unless caller; 
     113 
     1141; 
     115