| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 4 | # This program is distributed under the terms of the |
|---|
| 5 | # GNU General Public License, version 2. |
|---|
| 6 | # |
|---|
| 7 | # $Id$ |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | use Getopt::Long; |
|---|
| 12 | use 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; |
|---|
| 19 | use MT::Upgrade; |
|---|
| 20 | |
|---|
| 21 | my $mt = new MT(Config => 'mt.cfg') or die MT->errstr; |
|---|
| 22 | |
|---|
| 23 | MT->add_callback('MT::Upgrade::SQL', 1, undef, \&sql_cb) if $sqlonly; |
|---|
| 24 | |
|---|
| 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; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | my $install; |
|---|
| 33 | my $driver = MT::Object->driver; |
|---|
| 34 | if (!$driver || !$driver->table_exists('MT::Author')) { |
|---|
| 35 | $install = 1; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | unless ($install || $name) { |
|---|
| 39 | print "Please set username to set superuser at upgrading. cf: upgrade --name Melody\n"; |
|---|
| 40 | exit; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 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 | } |
|---|
| 50 | |
|---|
| 51 | my $updated = MT::Upgrade->do_upgrade(App => 'main', |
|---|
| 52 | DryRun => $dryrun, |
|---|
| 53 | Install => $install, |
|---|
| 54 | SuperUser => $author_id, |
|---|
| 55 | CLI => 1, |
|---|
| 56 | ); |
|---|
| 57 | |
|---|
| 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; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | sub progress { |
|---|
| 66 | my $pkg = shift; |
|---|
| 67 | my $msg = shift; |
|---|
| 68 | print "\t* " . $msg . "\n" unless $sqlonly; |
|---|
| 69 | } |
|---|
| 70 | sub error { |
|---|
| 71 | my $pkg = shift; |
|---|
| 72 | my $err = shift; |
|---|
| 73 | confess $err; |
|---|
| 74 | } |
|---|
| 75 | sub sql_cb { |
|---|
| 76 | my $cb = shift; |
|---|
| 77 | my ($upgrade, $stmt) = @_; |
|---|
| 78 | print "$stmt\n"; |
|---|
| 79 | } |
|---|