-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathwwaddindexing
executable file
·134 lines (104 loc) · 3.57 KB
/
wwaddindexing
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
131
132
133
134
#!/usr/bin/env perl
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2023 The WeBWorK Project, https://github.com/openwebwork
#
# 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 course.
=head1 SYNOPSIS
wwaddindexing COURSEID
=head1 DESCRIPTION
Adds indices to the course named COURSEID.
=cut
BEGIN {
# hide arguments (there could be passwords there!)
$0 = "$0";
}
use strict;
use warnings;
use DBI;
my $pg_dir;
BEGIN {
die "WEBWORK_ROOT not found in environment.\n" unless exists $ENV{WEBWORK_ROOT};
$pg_dir = $ENV{PG_ROOT} // "$ENV{WEBWORK_ROOT}/../pg";
die "The pg directory must be defined in PG_ROOT" unless (-e $pg_dir);
}
use lib "$ENV{WEBWORK_ROOT}/lib";
use lib "$pg_dir/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,
});
# 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 occurred 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