#!/usr/bin/env perl ################################################################################ # WeBWorK Online Homework Delivery System # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ # $CVSHeader: webwork-modperl/bin/wwaddindexing,v 1.1 2004/09/28 01:23:01 sh002i Exp $ # # This program is free software; you can redistribute it and/or modify it under # the terms of either: (a) the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any later # version, or (b) the "Artistic License" which comes with this package. # # 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 either the GNU General Public License or the # Artistic License for more details. ################################################################################ =head1 NAME wwaddindexing - add indices to an existing sql_single course. =head1 SYNOPSIS wwaddindexing COURSEID =head1 DESCRIPTION Adds indices to the course named COURSEID. The course must use the sql_single database layout. =cut BEGIN { # hide arguments (there could be passwords there!) $0 = "$0"; } use strict; use warnings; use DBI; BEGIN { die "WEBWORK_ROOT not found in environment.\n" unless exists $ENV{WEBWORK_ROOT}; } use lib "$ENV{WEBWORK_ROOT}/lib"; use WeBWorK::CourseEnvironment; use WeBWorK::DB; use WeBWorK::Utils qw/runtime_use/; use WeBWorK::Utils::CourseManagement qw/dbLayoutSQLSources/; sub usage { print STDERR "usage: $0 COURSEID \n"; exit; } sub usage_error { print STDERR "$0: @_\n"; usage(); } # get command-line options my ($courseID) = @ARGV; # perform sanity check usage_error("must specify COURSEID.") unless $courseID and $courseID ne ""; # bring up a minimal course environment my $ce = WeBWorK::CourseEnvironment->new({ webwork_dir => $ENV{WEBWORK_ROOT}, courseName => $courseID, }); # make sure the course actually uses the 'sql_single' layout usage_error("$courseID: does not use 'sql_single' database layout.") unless $ce->{dbLayoutName} eq "sql_single"; # get database layout source data my %sources = dbLayoutSQLSources($ce->{dbLayout}); foreach my $source (keys %sources) { my %source = %{$sources{$source}}; my @tables = @{$source{tables}}; my $username = $source{username}; my $password = $source{password}; my $dbh = DBI->connect($source, $username, $password); foreach my $table (@tables) { # this stuff straight out of sql_single.pm my %table = %{ $ce->{dbLayout}{$table} }; my %params = %{ $table{params} }; my $source = $table{source}; my $tableOverride = $params{tableOverride}; my $recordClass = $table{record}; runtime_use($recordClass); my @fields = $recordClass->FIELDS; my @keyfields = $recordClass->KEYFIELDS; if (exists $params{fieldOverride}) { my %fieldOverride = %{ $params{fieldOverride} }; foreach my $field (@fields) { $field = $fieldOverride{$field} if exists $fieldOverride{$field}; } } my @fieldList; foreach my $start (0 .. $#keyfields) { my $line = "ADD INDEX ( "; $line .= join(", ", map { "`$_`(16)" } @keyfields[$start .. $#keyfields]); $line .= " )"; push @fieldList, $line; } my $fieldString = join(", ", @fieldList); my $tableName = $tableOverride || $table; my $stmt = "ALTER TABLE `$tableName` $fieldString;"; unless ($dbh->do($stmt)) { die "An error occured while trying to modify the course database.\n", "It is possible that the course database is in an inconsistent state.\n", "The DBI error message was:\n\n", $dbh->errstr, "\n"; } } $dbh->disconnect; } =head1 AUTHOR Written by Sam Hathaway, hathaway at users.sourceforge.net. =cut