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

my $db_treanhandle = DBI->connect
                   ("DBI:mysql:$trean_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_treanhandle->prepare("show columns from trean_bookmarks");
$sth->execute;
my $trean_bookmarks = $sth->fetchall_hashref('Field');

my $sth1 = $db_treanhandle->prepare("show columns from trean_favicons");
$sth1->execute;
my $trean_favicons = $sth1->fetchall_hashref('Field');


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

# Create an index for bookmark_clicks_id if needed
unless ($trean_bookmarks->{bookmark_clicks}->{Key})
{
    my $statement = 'alter table trean_bookmarks ' .
                    'add index trean_bookmarks_clicks_idx (bookmark_clicks)';
    $statement = $db_treanhandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

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

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

