Skip to content

Commit d3997e0

Browse files
authored
Merge pull request #2650 from drgrice1/course-integrity-check-rework
Add the capability of changing database field types when upgrading the database.
2 parents 45224fc + a96f56a commit d3997e0

17 files changed

+735
-1368
lines changed

bin/update-OPL-statistics.pl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ BEGIN
3030
use String::ShellQuote;
3131

3232
use DBI;
33-
use WeBWorK::Utils::CourseIntegrityCheck;
3433
use WeBWorK::Utils::CourseManagement qw/listCourses/;
3534

3635
my $time = time();

bin/upgrade-database-to-utf8mb4.pl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ BEGIN
165165
my $dbuser = shell_quote($ce->{database_username});
166166
my $dbpass = $ce->{database_password};
167167

168-
$ENV{'MYSQL_PWD'} = $dbpass;
168+
local $ENV{MYSQL_PWD} = $dbpass;
169169

170170
if (!$no_backup) {
171171
# Backup the database
@@ -212,7 +212,7 @@ BEGIN
212212
},
213213
);
214214

215-
my $db = new WeBWorK::DB($ce->{dbLayouts}{ $ce->{dbLayoutName} });
215+
my $db = WeBWorK::DB->new($ce->{dbLayouts}{ $ce->{dbLayoutName} });
216216
my @table_types = sort(grep { !$db->{$_}{params}{non_native} } keys %$db);
217217

218218
sub checkAndUpdateTableColumnTypes {
@@ -222,29 +222,30 @@ sub checkAndUpdateTableColumnTypes {
222222

223223
print "\tChecking '$table' (pass $pass)\n" if $verbose;
224224
my $schema_field_data = $db->{$table_type}{record}->FIELD_DATA;
225-
for my $field (keys %$schema_field_data) {
226-
my $field_name = $db->{$table_type}{params}{fieldOverride}{$field} || $field;
227-
my @name_type = @{
225+
for my $field_name (keys %$schema_field_data) {
226+
my @name_type = @{
228227
$dbh->selectall_arrayref(
229228
"SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS "
230229
. "WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$table' AND COLUMN_NAME='$field_name';"
231230
)
232231
};
233232

234-
print("\t\tThe '$field_name' column is missing from '$table'.\n"
235-
. "\t\tYou should upgrade the course via course administration to fix this.\n"
236-
. "\t\tYou may need to run this script again after doing that.\n"), next
237-
if !exists($name_type[0][0]);
233+
if (!exists($name_type[0][0])) {
234+
print("\t\tThe '$field_name' column is missing from '$table'.\n"
235+
. "\t\tYou should upgrade the course via course administration to fix this.\n"
236+
. "\t\tYou may need to run this script again after doing that.\n");
237+
next;
238+
}
238239

239240
my $data_type = $name_type[0][0];
240241
next if !$data_type;
241242
$data_type =~ s/\(\d*\)$// if $data_type =~ /^(big|small)?int\(\d*\)$/;
242243
$data_type = lc($data_type);
243-
my $schema_data_type = lc($schema_field_data->{$field}{type} =~ s/ .*$//r);
244+
my $schema_data_type = lc($schema_field_data->{$field_name}{type} =~ s/ .*$//r);
244245
if ($data_type ne $schema_data_type) {
245246
print "\t\tUpdating data type for column '$field_name' in table '$table'\n" if $verbose;
246247
print "\t\t\t$data_type -> $schema_data_type\n" if $verbose;
247-
eval { $dbh->do("ALTER TABLE `$table` MODIFY $field_name $schema_field_data->{$field}{type};"); };
248+
eval { $dbh->do("ALTER TABLE `$table` MODIFY $field_name $schema_field_data->{$field_name}{type};"); };
248249
my $indent = $verbose ? "\t\t" : "";
249250
die("${indent}Failed to modify '$field_name' in '$table' from '$data_type' to '$schema_data_type.\n"
250251
. "${indent}It is recommended that you restore a database backup. Make note of the\n"
@@ -286,8 +287,10 @@ sub checkAndChangeTableCharacterSet {
286287
my $error = 0;
287288

288289
for my $course (@courses) {
289-
print("The course '$course' does not exist on the server\n"), next
290-
if !grep($course eq $_, @server_courses);
290+
if (!grep { $course eq $_ } @server_courses) {
291+
print("The course '$course' does not exist on the server\n");
292+
next;
293+
}
291294

292295
print "Checking tables for '$course'\n" if $verbose;
293296
for my $table_type (@table_types) {

bin/upgrade_admin_db.pl

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,31 @@ BEGIN
2424
use lib "$ENV{WEBWORK_ROOT}/lib";
2525

2626
use WeBWorK::CourseEnvironment;
27-
2827
use WeBWorK::DB;
29-
use WeBWorK::Utils::CourseIntegrityCheck;
28+
use WeBWorK::Utils::CourseDBIntegrityCheck;
3029

31-
##########################
32-
# update admin course
33-
##########################
30+
# Update admin course
3431
my $ce = WeBWorK::CourseEnvironment->new({ webwork_dir => $ENV{WEBWORK_ROOT} });
3532
my $upgrade_courseID = $ce->{admin_course_id};
3633
$ce = WeBWorK::CourseEnvironment->new({
3734
webwork_dir => $ENV{WEBWORK_ROOT},
3835
courseName => $upgrade_courseID,
3936
});
40-
#warn "do_upgrade_course: updating |$upgrade_courseID| from" , join("|",@upgrade_courseIDs);
41-
#############################################################################
42-
# Create integrity checker
43-
#############################################################################
4437

38+
# Create integrity checker
4539
my @update_report;
46-
my $CIchecker = new WeBWorK::Utils::CourseIntegrityCheck(ce => $ce);
40+
my $CIchecker = new WeBWorK::Utils::CourseDBIntegrityCheck($ce);
4741

48-
#############################################################################
4942
# Add missing tables and missing fields to existing tables
50-
#############################################################################
51-
5243
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($upgrade_courseID);
5344
my @schema_table_names = keys %$dbStatus; # update tables missing from database;
5445
my @tables_to_create =
55-
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A() } @schema_table_names;
46+
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A() } @schema_table_names;
5647
my @tables_to_alter =
57-
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B() } @schema_table_names;
48+
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B() } @schema_table_names;
5849
push(@update_report, $CIchecker->updateCourseTables($upgrade_courseID, [@tables_to_create]));
59-
foreach my $table_name (@tables_to_alter)
60-
{ #warn "do_upgrade_course: adding new fields to table $table_name in course $upgrade_courseID";
50+
51+
for my $table_name (@tables_to_alter) {
6152
push(@update_report, $CIchecker->updateTableFields($upgrade_courseID, $table_name));
6253
}
6354

0 commit comments

Comments
 (0)