#!/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("imp db must exist") unless ( -f "/var/lib/mysql/horde/imp_sentmail.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 $imp_DATABASE = 'horde';
our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};

my $db_imphandle = DBI->connect
                   ("DBI:mysql:$imp_DATABASE",
                     $username, $password, $dbi_options )
                     || die ("Connection error: $DBI::errstr");


# These are all safe to run multiple times

# We now need to create some columns, but we need to first check
# whether they exist already
my $sth = $db_imphandle->prepare("show columns from imp_sentmail");
$sth->execute;
my $imp_sentmail = $sth->fetchall_hashref('Field');

# Create an index for sentmail_ts if needed
unless ($imp_sentmail->{sentmail_ts}->{Key})
{
    my $statement = 'alter table imp_sentmail ' .
                    'add index sentmail_ts_idx (sentmail_ts)';
    $statement = $db_imphandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

# Create an index for sentmail_who if needed
unless ($imp_sentmail->{sentmail_who}->{Key})
{
    my $statement = 'alter table imp_sentmail ' .
                    'add index sentmail_who_idx (sentmail_who)';
    $statement = $db_imphandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

# Create an index for sentmail_success if needed
unless ($imp_sentmail->{sentmail_success}->{Key})
{
    my $statement = 'alter table imp_sentmail ' .
                    'add index sentmail_success_idx (sentmail_success)';
    $statement = $db_imphandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

