smeserver-nag/root/etc/e-smith/events/actions/nag_upgrade

301 lines
11 KiB
Perl

#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2002-20085 Mitel Networks Corporation
# copyright (C) 2002-2008 SME Server, INC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#----------------------------------------------------------------------
use strict;
use DBI;
use esmith::ConfigDB;
use esmith::util;
# Exit early if there is nothing to do
die("horde db must exist") unless ( -d "/var/lib/mysql/horde/");
die("nag db must exist") unless ( -f "/var/lib/mysql/horde/nag_tasks.frm");
die("nag db must exist") unless ( -f "/var/lib/mysql/horde/nag_shares.frm");
# This is a translation of the script 'mysql_upgrade_1.1_to_1.2.sql
# that is safe to run multiple times, and which can be run on a 1.2
# installation without barfing.
my $conf = esmith::ConfigDB->open_ro
or die "Can't open configuration database: $!\n";
our $username = 'root';
our $password = esmith::util::LdapPassword();
our $nag_DATABASE = 'horde';
our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};
my $db_naghandle = DBI->connect
("DBI:mysql:$nag_DATABASE",
$username, $password, $dbi_options )
|| die ("Connection error: $DBI::errstr");
# These are all safe to run multiple times
my @statements = (
"ALTER TABLE nag_shares CHANGE share_owner share_owner VARCHAR(255)",
"ALTER TABLE nag_shares_users CHANGE user_uid user_uid VARCHAR(255)",
"ALTER TABLE nag_shares_groups CHANGE group_uid group_uid VARCHAR(255)",
);
foreach my $statement (@statements)
{
$statement =
$db_naghandle->prepare("$statement")
or die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# We now need to create some columns, but we need to first check
# whether they exist already
my $sth = $db_naghandle->prepare("show columns from nag_tasks");
$sth->execute;
my $nag_tasks = $sth->fetchall_hashref('Field');
my $sth1 = $db_naghandle->prepare("show columns from nag_shares");
$sth1->execute;
my $nag_shares = $sth1->fetchall_hashref('Field');
my $sth2 = $db_naghandle->prepare("show columns from nag_shares_groups");
$sth2->execute;
my $nag_shares_groups = $sth2->fetchall_hashref('Field');
my $sth3 = $db_naghandle->prepare("show columns from nag_shares_users");
$sth3->execute;
my $nag_shares_users = $sth3->fetchall_hashref('Field');
unless (defined $nag_tasks->{task_estimate})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
'ALTER TABLE nag_tasks ADD task_estimate FLOAT';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_completed_date})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
'ALTER TABLE nag_tasks ADD task_completed_date INT';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_start})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
'ALTER TABLE nag_tasks ADD task_start INT';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_parent})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
"ALTER TABLE nag_tasks ADD task_parent VARCHAR(255) ".
"DEFAULT '' NOT NULL";
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_creator})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
"ALTER TABLE nag_tasks ADD task_creator VARCHAR(255) NOT NULL ";
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_assignee})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
"ALTER TABLE nag_tasks ADD task_assignee VARCHAR(255) ";
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
unless (defined $nag_tasks->{task_private})
{
# We need to be careful about this one as it will fail if the
# column exists, so we check the error.
my $statement =
"ALTER TABLE nag_tasks ADD task_private SMALLINT ".
"DEFAULT 0 NOT NULL";
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for task_owner if needed
unless ($nag_tasks->{task_owner}->{Key})
{
my $statement = 'alter table nag_tasks ' .
'add index nag_tasklist_idx (task_owner)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for task_uid if needed
unless ($nag_tasks->{task_uid}->{Key})
{
my $statement = 'alter table nag_tasks ' .
'add index nag_uid_idx (task_uid)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for task_start if needed
unless ($nag_tasks->{task_start}->{Key})
{
my $statement = 'alter table nag_tasks ' .
'add index nag_start_idx (task_start)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for share_name if needed
unless ($nag_shares->{share_name}->{Key})
{
my $statement = 'alter table nag_shares ' .
'add index nag_shares_share_name_idx (share_name)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for share_owner if needed
unless ($nag_shares->{share_owner}->{Key})
{
my $statement = 'alter table nag_shares ' .
'add index nag_shares_share_owner_idx (share_owner)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for perm_creator if needed
unless ($nag_shares->{perm_creator}->{Key})
{
my $statement = 'alter table nag_shares ' .
'add index nag_shares_perm_creator_idx (perm_creator)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for perm_default if needed
unless ($nag_shares->{perm_default}->{Key})
{
my $statement = 'alter table nag_shares ' .
'add index nag_shares_perm_default_idx (perm_default)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for perm_guest if needed
unless ($nag_shares->{perm_guest}->{Key})
{
my $statement = 'alter table nag_shares ' .
'add index nag_shares_perm_guest_idx (perm_guest)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for share_id if needed
unless ($nag_shares_groups->{share_id}->{Key})
{
my $statement = 'alter table nag_shares_groups ' .
'add index nag_shares_groups_share_id_idx (share_id)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for group_uid if needed
unless ($nag_shares_groups->{group_uid}->{Key})
{
my $statement = 'alter table nag_shares_groups ' .
'add index nag_shares_groups_group_uid_idx (group_uid)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for perm if needed
unless ($nag_shares_groups->{perm}->{Key})
{
my $statement = 'alter table nag_shares_groups ' .
'add index nag_shares_groups_perm_idx (perm)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for share_id if needed
unless ($nag_shares_users->{share_id}->{Key})
{
my $statement = 'alter table nag_shares_users ' .
'add index nag_shares_users_share_id_idx (share_id)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for user_uid if needed
unless ($nag_shares_users->{user_uid}->{Key})
{
my $statement = 'alter table nag_shares_users ' .
'add index nag_shares_users_user_uid_idx (user_uid)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}
# Create an index for perm if needed
unless ($nag_shares_users->{perm}->{Key})
{
my $statement = 'alter table nag_shares_users ' .
'add index nag_shares_users_perm_idx (perm)';
$statement = $db_naghandle->prepare($statement) or
die "prepare: $$statement: $DBI::errstr";
$statement->execute or die "execute: $$statement: $DBI::errstr";
}