forked from movabletype/movabletype
/
upgrade
executable file
·131 lines (105 loc) · 3.21 KB
/
upgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl -w
# Movable Type (r) Open Source (C) 2001-2011 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::Tool::Upgrade;
use strict;
use utf8;
use lib qw( extlib lib );
use base qw( MT::Tool );
use Carp qw(confess);
use MT::Upgrade;
sub usage { '[--dryrun] [--sql] [--name <name>]' }
sub help {
return q{
Installs or upgrades a database to the current MT schema.
--dryrun Determine the upgrade steps required without
executing any changes.
--sql Report the SQL that would be performed instead
of executing it.
--name <name> The author as whom to perform the upgrade steps.
Required when performing an upgrade (not at
initial install).
};
}
my ($dryrun, $name, $sqlonly);
sub options {
return (
'dryrun!' => \$dryrun,
'sql!' => \$sqlonly,
'name=s' => \$name,
);
}
sub main {
my $class = shift;
my ($verbose) = $class->SUPER::main(@_);
if ($sqlonly) {
$dryrun = 1;
MT->add_callback('MT::Upgrade::SQL', 1, undef, \&sql_cb);
}
else {
print "upgrade -- A command line tool for upgrading the schema for Movable Type.\n";
print "(Non-destructive mode)\n" if $dryrun;
}
my $install;
my $driver = MT::Object->driver;
if (!$driver || !$driver->table_exists('MT::Author')) {
$install = 1;
}
unless ($install || $name) {
print "Please set username to set superuser at upgrading. cf: upgrade --name Melody\n";
exit;
}
my $author_id;
if (!$install && $name) {
require MT::BasicAuthor;
my $a = MT::BasicAuthor->load({name => $name})
or die "Not found user $name:" . MT::BasicAuthor->errstr;
$author_id = $a->id;
}
if ( $install ) {
$MT::Upgrade::Installing = 1;
}
local $SIG{__WARN__} = sub { __PACKAGE__->trace( $_[0] ) };
my $updated = MT::Upgrade->do_upgrade(
App => __PACKAGE__,
DryRun => $dryrun,
Install => $install,
SuperUser => $author_id,
CLI => 1,
User => { user_nickname => 'Melody Nelson' },
);
if ($install) {
my $blog = MT->model('blog')->load(1);
$blog->theme_id('classic_website');
$blog->save;
$blog->apply_theme;
print "Installation complete.\n";
} else {
print "Upgrade complete!\n" if !$dryrun && $updated;
print "Your schema is up to date already.\n" if defined $updated && !$updated;
}
}
sub progress {
my $pkg = shift;
my $msg = shift;
print "\t* " . Encode::encode( MT->config->PublishCharset || 'UTF-8', $msg ) . "\n" unless $sqlonly;
}
sub error {
my $pkg = shift;
my $err = shift;
confess Encode::encode( MT->config->PublishCharset || 'UTF-8', $err );
}
sub sql_cb {
my $cb = shift;
my ($upgrade, $stmt) = @_;
print "$stmt\n";
}
sub trace {
my $pkg = shift;
print "[warn] >> \t" . Encode::encode( MT->config->PublishCharset || 'UTF-8', $_[0] ) . "\n";
}
__PACKAGE__->main() unless caller;
1;