Skip to content

Commit

Permalink
Split the CourseIntegrityCheck.pm module into two parts.
Browse files Browse the repository at this point in the history
The first is CourseDBIntegrityCheck.pm that checks the database
integrity and handles a database upgrade.

The second is the CourseDirectoryIntegrityCheck that checks the
directory structure and handles fixing that if not correct.  This
includes the improved directory upgrade method.
  • Loading branch information
drgrice1 committed Oct 9, 2023
1 parent e90d53f commit d6b5bd2
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 181 deletions.
8 changes: 4 additions & 4 deletions bin/upgrade_admin_db.pl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BEGIN

use WeBWorK::CourseEnvironment;
use WeBWorK::DB;
use WeBWorK::Utils::CourseIntegrityCheck;
use WeBWorK::Utils::CourseDBIntegrityCheck;

# Update admin course
my $upgrade_courseID = 'admin';
Expand All @@ -37,15 +37,15 @@ BEGIN

# Create integrity checker
my @update_report;
my $CIchecker = new WeBWorK::Utils::CourseIntegrityCheck(ce => $ce);
my $CIchecker = new WeBWorK::Utils::CourseDBIntegrityCheck($ce);

# Add missing tables and missing fields to existing tables
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($upgrade_courseID);
my @schema_table_names = keys %$dbStatus; # update tables missing from database;
my @tables_to_create =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A() } @schema_table_names;
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A() } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B() } @schema_table_names;
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B() } @schema_table_names;
push(@update_report, $CIchecker->updateCourseTables($upgrade_courseID, [@tables_to_create]));

for my $table_name (@tables_to_alter) {
Expand Down
66 changes: 34 additions & 32 deletions lib/WeBWorK/ContentGenerator/CourseAdmin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ use WeBWorK::Debug;
use WeBWorK::Utils qw(cryptPassword writeLog trim_spaces);
use WeBWorK::Utils::CourseManagement qw(addCourse renameCourse retitleCourse deleteCourse listCourses archiveCourse
unarchiveCourse initNonNativeTables);
use WeBWorK::Utils::CourseIntegrityCheck;
use WeBWorK::Utils::CourseDBIntegrityCheck;
use WeBWorK::Utils::CourseDirectoryIntegrityCheck qw(checkCourseDirectories updateCourseDirectories);
use WeBWorK::DB;

sub pre_header_initialize ($c) {
Expand Down Expand Up @@ -525,7 +526,7 @@ sub rename_course_confirm ($c) {
) unless $c->param('rename_newCourseID_checkbox');

if ($ce2->{dbLayoutName}) {
my $CIchecker = WeBWorK::Utils::CourseIntegrityCheck->new(ce => $ce2);
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);

# Check database
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($rename_oldCourseID);
Expand All @@ -535,9 +536,9 @@ sub rename_course_confirm ($c) {
if ($c->param('upgrade_course_tables')) {
my @schema_table_names = keys %$dbStatus;
my @tables_to_create =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A } @schema_table_names;
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B }
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B }
@schema_table_names;
push(@upgrade_report, $CIchecker->updateCourseTables($rename_oldCourseID, [@tables_to_create]));
for my $table_name (@tables_to_alter) {
Expand All @@ -548,7 +549,7 @@ sub rename_course_confirm ($c) {
}

# Check directories
my ($directories_ok, $directory_report) = $CIchecker->checkCourseDirectories($ce2);
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);

return $c->include(
'ContentGenerator/CourseAdmin/rename_course_confirm',
Expand Down Expand Up @@ -1004,7 +1005,7 @@ sub archive_course_confirm ($c) {
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $archive_courseID });

if ($ce2->{dbLayoutName}) {
my $CIchecker = WeBWorK::Utils::CourseIntegrityCheck->new(ce => $ce2);
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);

# Check database
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($archive_courseID);
Expand All @@ -1014,9 +1015,9 @@ sub archive_course_confirm ($c) {
if ($c->param('upgrade_course_tables')) {
my @schema_table_names = keys %$dbStatus;
my @tables_to_create =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A } @schema_table_names;
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B }
grep { $dbStatus->{$_}->[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B }
@schema_table_names;
push(@upgrade_report, $CIchecker->updateCourseTables($archive_courseID, [@tables_to_create]));
for my $table_name (@tables_to_alter) {
Expand All @@ -1027,8 +1028,8 @@ sub archive_course_confirm ($c) {
}

# Update and check directories.
my $dir_update_messages = $c->param('upgrade_course_tables') ? $CIchecker->updateCourseDirectories : [];
my ($directories_ok, $directory_report) = $CIchecker->checkCourseDirectories($ce2);
my $dir_update_messages = $c->param('upgrade_course_tables') ? updateCourseDirectories($ce2) : [];
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);

return $c->include(
'ContentGenerator/CourseAdmin/archive_course_confirm',
Expand Down Expand Up @@ -1350,7 +1351,7 @@ sub upgrade_course_confirm ($c) {
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $upgrade_courseID });

# Create integrity checker
my $CIchecker = WeBWorK::Utils::CourseIntegrityCheck->new(ce => $ce2);
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);

# Report on database status
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($upgrade_courseID);
Expand Down Expand Up @@ -1414,7 +1415,7 @@ sub upgrade_course_confirm ($c) {
}

# Report on directory status
my ($directories_ok, $directory_report) = $CIchecker->checkCourseDirectories;
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);
push(@$course_output, $c->tag('div', class => 'mb-2', $c->maketext('Directory structure:')));
push(
@$course_output,
Expand Down Expand Up @@ -1466,15 +1467,16 @@ sub do_upgrade_course ($c) {
my $ce2 = WeBWorK::CourseEnvironment->new({ courseName => $upgrade_courseID });

# Create integrity checker
my $CIchecker = WeBWorK::Utils::CourseIntegrityCheck->new(ce => $ce2);
my $CIchecker = WeBWorK::Utils::CourseDBIntegrityCheck->new($ce2);

# Add missing tables and missing fields to existing tables
my ($tables_ok, $dbStatus) = $CIchecker->checkCourseTables($upgrade_courseID);
my @schema_table_names = keys %$dbStatus;
my @tables_to_create =
grep { $dbStatus->{$_}[0] == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A } @schema_table_names;
grep { $dbStatus->{$_}[0] == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A } @schema_table_names;
my @tables_to_alter =
grep { $dbStatus->{$_}[0] == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B } @schema_table_names;
grep { $dbStatus->{$_}[0] == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B }
@schema_table_names;

my @upgrade_report;
push(
Expand Down Expand Up @@ -1526,8 +1528,8 @@ sub do_upgrade_course ($c) {
}

# Add missing directories and prepare report on directory status
my $dir_update_messages = $CIchecker->updateCourseDirectories; # Needs more error messages
my ($directories_ok, $directory_report) = $CIchecker->checkCourseDirectories;
my $dir_update_messages = updateCourseDirectories($ce2); # Needs more error messages
my ($directories_ok, $directory_report) = checkCourseDirectories($ce2);

# Show status
my $course_report = $c->c;
Expand Down Expand Up @@ -1563,8 +1565,8 @@ sub do_upgrade_course ($c) {
'li',
$c->tag(
'span',
class => $_->[2] ? 'text-success' : 'text-danger',
$_->[1]
class => $_->[1] ? 'text-success' : 'text-danger',
$_->[0]
)
)
} @$dir_update_messages
Expand Down Expand Up @@ -2264,32 +2266,32 @@ sub do_registration ($c) {
# Format a list of tables and fields in the database, and the status of each.
sub formatReportOnDatabaseTables ($c, $dbStatus, $courseID = undef) {
my %table_status_message = (
WeBWorK::Utils::CourseIntegrityCheck::SAME_IN_A_AND_B =>
WeBWorK::Utils::CourseDBIntegrityCheck::SAME_IN_A_AND_B =>
$c->tag('span', class => 'text-success me-2', $c->maketext('Table is ok')),
WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A => $c->tag(
WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A => $c->tag(
'span',
class => 'text-danger me-2',
$c->maketext('Table defined in schema but missing in database')
),
WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_B => $c->tag(
WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_B => $c->tag(
'span',
class => 'text-danger me-2',
$c->maketext('Table defined in database but missing in schema')
),
WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B => $c->tag(
WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B => $c->tag(
'span',
class => 'text-danger me-2',
$c->maketext('Schema and database table definitions do not agree')
)
);
my %field_status_message = (
WeBWorK::Utils::CourseIntegrityCheck::SAME_IN_A_AND_B =>
WeBWorK::Utils::CourseDBIntegrityCheck::SAME_IN_A_AND_B =>
$c->tag('span', class => 'text-success me-2', $c->maketext('Field is ok')),
WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A =>
WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A =>
$c->tag('span', class => 'text-danger me-2', $c->maketext('Field missing in database')),
WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_B =>
WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_B =>
$c->tag('span', class => 'text-danger me-2', $c->maketext('Field missing in schema')),
WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B => $c->tag(
WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B => $c->tag(
'span',
class => 'text-danger me-2',
$c->maketext('Schema and database field definitions do not agree')
Expand All @@ -2308,9 +2310,9 @@ sub formatReportOnDatabaseTables ($c, $dbStatus, $courseID = undef) {
my $table_status = $dbStatus->{$table}[0];
push(@$table_report, $table . ': ', $table_status_message{$table_status});

if ($table_status == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A) {
if ($table_status == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A) {
$all_tables_ok = 0;
} elsif ($table_status == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_B) {
} elsif ($table_status == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_B) {
$extra_database_tables = 1;
push(
@$table_report,
Expand All @@ -2325,15 +2327,15 @@ sub formatReportOnDatabaseTables ($c, $dbStatus, $courseID = undef) {
)
)
) if defined $courseID;
} elsif ($table_status == WeBWorK::Utils::CourseIntegrityCheck::DIFFER_IN_A_AND_B) {
} elsif ($table_status == WeBWorK::Utils::CourseDBIntegrityCheck::DIFFER_IN_A_AND_B) {
my %fieldInfo = %{ $dbStatus->{$table}[1] };
my $fields_report = $c->c;

for my $key (keys %fieldInfo) {
my $field_status = $fieldInfo{$key}[0];
my $field_report = $c->c("$key: $field_status_message{$field_status}");

if ($field_status == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_B) {
if ($field_status == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_B) {
$extra_database_fields = 1;
push(
@$field_report,
Expand All @@ -2353,7 +2355,7 @@ sub formatReportOnDatabaseTables ($c, $dbStatus, $courseID = undef) {
)
)
) if defined $courseID;
} elsif ($field_status == WeBWorK::Utils::CourseIntegrityCheck::ONLY_IN_A) {
} elsif ($field_status == WeBWorK::Utils::CourseDBIntegrityCheck::ONLY_IN_A) {
$all_tables_ok = 0;
}
push(@$fields_report, $c->tag('li', $field_report->join('')));
Expand Down
Loading

0 comments on commit d6b5bd2

Please sign in to comment.