Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the capability of changing database field types when upgrading the database. #2650

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/update-OPL-statistics.pl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ BEGIN
use String::ShellQuote;

use DBI;
use WeBWorK::Utils::CourseIntegrityCheck;
use WeBWorK::Utils::CourseManagement qw/listCourses/;

my $time = time();
Expand Down
29 changes: 16 additions & 13 deletions bin/upgrade-database-to-utf8mb4.pl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ BEGIN
my $dbuser = shell_quote($ce->{database_username});
my $dbpass = $ce->{database_password};

$ENV{'MYSQL_PWD'} = $dbpass;
local $ENV{MYSQL_PWD} = $dbpass;

if (!$no_backup) {
# Backup the database
Expand Down Expand Up @@ -212,7 +212,7 @@ BEGIN
},
);

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

sub checkAndUpdateTableColumnTypes {
Expand All @@ -222,29 +222,30 @@ sub checkAndUpdateTableColumnTypes {

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

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

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

for my $course (@courses) {
print("The course '$course' does not exist on the server\n"), next
if !grep($course eq $_, @server_courses);
if (!grep { $course eq $_ } @server_courses) {
print("The course '$course' does not exist on the server\n");
next;
}

print "Checking tables for '$course'\n" if $verbose;
for my $table_type (@table_types) {
Expand Down
25 changes: 8 additions & 17 deletions bin/upgrade_admin_db.pl
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,31 @@ BEGIN
use lib "$ENV{WEBWORK_ROOT}/lib";

use WeBWorK::CourseEnvironment;

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

##########################
# update admin course
##########################
# Update admin course
my $ce = WeBWorK::CourseEnvironment->new({ webwork_dir => $ENV{WEBWORK_ROOT} });
my $upgrade_courseID = $ce->{admin_course_id};
$ce = WeBWorK::CourseEnvironment->new({
webwork_dir => $ENV{WEBWORK_ROOT},
courseName => $upgrade_courseID,
});
#warn "do_upgrade_course: updating |$upgrade_courseID| from" , join("|",@upgrade_courseIDs);
#############################################################################
# Create integrity checker
#############################################################################

# 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]));
foreach my $table_name (@tables_to_alter)
{ #warn "do_upgrade_course: adding new fields to table $table_name in course $upgrade_courseID";

for my $table_name (@tables_to_alter) {
push(@update_report, $CIchecker->updateTableFields($upgrade_courseID, $table_name));
}

Expand Down
Loading