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

Modifications to support use of the DBD::MariaDB driver instead of DBD::mysql #1160

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 22 additions & 8 deletions bin/OPL-update
Expand Up @@ -92,15 +92,29 @@ my $ce = new WeBWorK::CourseEnvironment({webwork_dir=>$ENV{WEBWORK_ROOT}});
my $ENABLE_UTF8MB4 = ($ce->{ENABLE_UTF8MB4})?1:0;
print "using utf8mb4 \n\n" if $ENABLE_UTF8MB4;

# The DBD::MariaDB driver should not get the
# mysql_enable_utf8mb4 or mysql_enable_utf8 settings,
# but DBD::mysql should.
my %utf8_parameters = ();

if ( $ce->{database_driver} =~ /^mysql$/i ) {
# Only needed for older DBI:mysql driver
if ( $ENABLE_UTF8MB4 ) {
$utf8_parameters{mysql_enable_utf8mb4} = 1;
} else {
$utf8_parameters{mysql_enable_utf8} = 1;
}
}

my $dbh = DBI->connect(
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
{
PrintError => 0,
RaiseError => 1,
($ENABLE_UTF8MB4)?(mysql_enable_utf8mb4 =>1):(mysql_enable_utf8 => 1),
},
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
{
PrintError => 0,
RaiseError => 1,
%utf8_parameters,
},
);

my $character_set='';
Expand Down
Empty file modified bin/OPLUtils.pm 100755 → 100644
Empty file.
21 changes: 14 additions & 7 deletions bin/dump-OPL-tables.pl
Expand Up @@ -64,12 +64,9 @@

# Get DB connection settings

my ($dbi,$dbtype,$db,$host,$port) = split(':',$ce->{database_dsn});

$host = 'localhost' unless $host;

$port = 3306 unless $port;

my $db = $ce->{database_name};
my $host = $ce->{database_host};
my $port = $ce->{database_port};
my $dbuser = $ce->{database_username};
my $dbpass = $ce->{database_password};

Expand Down Expand Up @@ -100,7 +97,17 @@

print "Dumping OPL tables\n";

`$mysqldump_command --host=$host --port=$port --user=$dbuser --default-character-set=$character_set $db $OPL_tables_to_dump > $prepared_OPL_tables_file`;
# Conditionally add --column-statistics=0 as MariaDB databases do not support it
# see: https://serverfault.com/questions/912162/mysqldump-throws-unknown-table-column-statistics-in-information-schema-1109
# https://github.com/drush-ops/drush/issues/4410

my $column_statistics_off = "";
my $test_for_column_statistics = `$mysqldump_command --help | grep 'column-statistics'`;
if ( $test_for_column_statistics ) {
$column_statistics_off = " --column-statistics=0 ";
}
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious as to if this ever occurs. In my testing on both Ubuntu 18.04 and Ubuntu 20.04, the column-statistics option for mysqldump does not exist, and furthermore is not enabled by default. So mysqldump works without the above check. Is this caused by having mysql-client installed instead of mariadb-client?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - this occurs.

In Docker running this branch I did:

cd /opt/webwork/webwork2/bin
./dump-OPL-tables.pl
cp dump-OPL-tables.pl dump-OPL-tables.pl2
vim dump-OPL-tables.pl2
# force $test_for_column_statistics = 0;
diff dump-OPL-tables.pl dump-OPL-tables.pl2
./dump-OPL-tables.pl2
rm dump-OPL-tables.pl2
/usr/bin/mysqldump --version

and get

root@localhost:~# cd /opt/webwork/webwork2/bin
root@localhost:/opt/webwork/webwork2/bin# ./dump-OPL-tables.pl
OPL path seems to be /opt/webwork/libraries/webwork-open-problem-library/
Dumping OPL tables
OPL database dump created: /opt/webwork/libraries/webwork-open-problem-library//TABLE-DUMP/OPL-tables.sql
root@localhost:/opt/webwork/webwork2/bin# cp dump-OPL-tables.pl dump-OPL-tables.pl2
root@localhost:/opt/webwork/webwork2/bin# vim dump-OPL-tables.pl2
root@localhost:/opt/webwork/webwork2/bin# diff dump-OPL-tables.pl dump-OPL-tables.pl2
105c105
< my $test_for_column_statistics = `$mysqldump_command --help | grep 'column-statistics'`;
---
> my $test_for_column_statistics = 0;
root@localhost:/opt/webwork/webwork2/bin# ./dump-OPL-tables.pl2
OPL path seems to be /opt/webwork/libraries/webwork-open-problem-library/
Dumping OPL tables
mysqldump: Couldn't execute 'SELECT COLUMN_NAME,                       JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"')                FROM information_schema.COLUMN_STATISTICS                WHERE SCHEMA_NAME = 'webwork' AND TABLE_NAME = 'OPL_DBsubject';': Unknown table 'COLUMN_STATISTICS' in information_schema (1109)
OPL database dump created: /opt/webwork/libraries/webwork-open-problem-library//TABLE-DUMP/OPL-tables.sql
root@localhost:/opt/webwork/webwork2/bin# rm dump-OPL-tables.pl2
root@localhost:/opt/webwork/webwork2/bin# /usr/bin/mysqldump --version
mysqldump  Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

That error is what led to the changes your are pointing out.


`$mysqldump_command --host=$host --port=$port --user=$dbuser --default-character-set=$character_set $column_statistics_off $db $OPL_tables_to_dump > $prepared_OPL_tables_file`;

print "OPL database dump created: $prepared_OPL_tables_file\n";

Expand Down
10 changes: 3 additions & 7 deletions bin/load-OPL-global-statistics.pl
Expand Up @@ -52,15 +52,11 @@

if (-e $global_sql_file) {

my ($dbi,$dbtype,$db,$host,$port) = split(':',$ce->{database_dsn});

$host = 'localhost' unless $host;

$port = 3306 unless $port;

my $db = $ce->{database_name};
my $host = $ce->{database_host};
my $port = $ce->{database_port};
my $dbuser = $ce->{database_username};
my $dbpass = $ce->{database_password};


$dbh->do(<<EOS);
DROP TABLE IF EXISTS OPL_global_statistics;
Expand Down
9 changes: 3 additions & 6 deletions bin/restore-OPL-tables.pl
Expand Up @@ -64,12 +64,9 @@

# Get DB connection settings

my ($dbi,$dbtype,$db,$host,$port) = split(':',$ce->{database_dsn});

$host = 'localhost' unless $host;

$port = 3306 unless $port;

my $db = $ce->{database_name};
my $host = $ce->{database_host};
my $port = $ce->{database_port};
my $dbuser = $ce->{database_username};
my $dbpass = $ce->{database_password};

Expand Down
44 changes: 34 additions & 10 deletions bin/updateOPLextras.pl
Expand Up @@ -14,7 +14,7 @@ =head1 SYNOPSIS
-d --directories (rebuild directory tree)
-a --all (rebuild all trees)
-h --help (display this text)

-v --verbose (turn on verbosity mode)
=head1 OPTIONS

=over 8
Expand All @@ -30,7 +30,11 @@ =head1 OPTIONS
=item B<-d> I<--directories>

Rebuild the directory tree and write to a JSON file.


=item B<-v> I<--verbosity>

Turn on verbosity mode.

=back

=head1 DESCRIPTION
Expand Down Expand Up @@ -76,15 +80,35 @@ BEGIN

my $ce = new WeBWorK::CourseEnvironment({webwork_dir=>$ENV{WEBWORK_ROOT}});

# decide whether the mysql installation can handle
# utf8mb4 and that should be used for the OPL

my $ENABLE_UTF8MB4 = ($ce->{ENABLE_UTF8MB4})?1:0;
print "using utf8mb4 \n\n" if $ENABLE_UTF8MB4;

# The DBD::MariaDB driver should not get the
# mysql_enable_utf8mb4 or mysql_enable_utf8 settings,
# but DBD::mysql should.
my %utf8_parameters = ();

if ( $ce->{database_driver} =~ /^mysql$/i ) {
# Only needed for older DBI:mysql driver
if ( $ENABLE_UTF8MB4 ) {
$utf8_parameters{mysql_enable_utf8mb4} = 1;
} else {
$utf8_parameters{mysql_enable_utf8} = 1;
}
}

my $dbh = DBI->connect(
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
{
PrintError => 0,
RaiseError => 1,
($ce->{ENABLE_UTF8MB4})?(mysql_enable_utf8mb4 =>1):(mysql_enable_utf8 => 1),
},
$ce->{problemLibrary_db}->{dbsource},
$ce->{problemLibrary_db}->{user},
$ce->{problemLibrary_db}->{passwd},
{
PrintError => 0,
RaiseError => 1,
%utf8_parameters,
},
);

build_library_textbook_tree($ce,$dbh,$verbose) if ($all || $textbooks);
Expand Down
24 changes: 17 additions & 7 deletions bin/upload-OPL-statistics.pl
Expand Up @@ -28,14 +28,14 @@
use String::ShellQuote;

my $ce = new WeBWorK::CourseEnvironment({
webwork_dir => $ENV{WEBWORK_ROOT},});
webwork_dir => $ENV{WEBWORK_ROOT},
});

my ($dbi,$dbtype,$db,$host,$port) = split(':',$ce->{database_dsn});

$host = 'localhost' unless $host;

$port = 3306 unless $port;
# Get DB connection settings

my $db = $ce->{database_name};
my $host = $ce->{database_host};
my $port = $ce->{database_port};
my $dbuser = $ce->{database_username};
my $dbpass = $ce->{database_password};

Expand All @@ -52,7 +52,17 @@

my $mysqldump_command = $ce->{externalPrograms}->{mysqldump};

`$mysqldump_command --host=$host --port=$port --user=$dbuser $db OPL_local_statistics > $output_file`;
# Conditionally add --column-statistics=0 as MariaDB databases do not support it
# see: https://serverfault.com/questions/912162/mysqldump-throws-unknown-table-column-statistics-in-information-schema-1109
# https://github.com/drush-ops/drush/issues/4410

my $column_statistics_off = "";
my $test_for_column_statistics = `$mysqldump_command --help | grep 'column-statistics'`;
if ( $test_for_column_statistics ) {
$column_statistics_off = " --column-statistics=0 ";
}

`$mysqldump_command --host=$host --port=$port --user=$dbuser $column_statistics_off $db OPL_local_statistics > $output_file`;

print "Database File Created\n";

Expand Down
14 changes: 11 additions & 3 deletions conf/database.conf.dist
@@ -1,8 +1,7 @@
#!perl
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright &copy; 2000-2018 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader: webwork2/conf/database.conf.dist,v 1.38 2007/08/13 22:59:51 sh002i Exp $
# Copyright &copy; 2000-2021 The WeBWorK Project, http://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
Expand Down Expand Up @@ -80,12 +79,21 @@ my %sqlParams = (
username => $database_username,
password => $database_password,
debug => $database_debug,
($ENABLE_UTF8MB4)?(mysql_enable_utf8mb4 =>1):(mysql_enable_utf8 => 1),
# kinda hacky, but needed for table dumping
mysql_path => $externalPrograms{mysql},
mysqldump_path => $externalPrograms{mysqldump},
);

if ( $ce->{database_driver} =~ /^mysql$/i ) {
# The extra UTF8 connection setting is ONLY needed for older DBD:mysql driver
# and forbidden by the newer DBD::MariaDB driver
if ( $ENABLE_UTF8MB4 ) {
$sqlParams{mysql_enable_utf8mb4} = 1; # Full 4-bit UTF-8
} else {
$sqlParams{mysql_enable_utf8} = 1; # Only the partial 3-bit mySQL UTF-8
}
}

$dbLayouts{sql_single} = {
locations => {
record => "WeBWorK::DB::Record::Locations",
Expand Down
25 changes: 2 additions & 23 deletions conf/defaults.config
@@ -1,9 +1,8 @@
#!perl
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright &copy; 2000-2018 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader: webwork2/conf/defaults.config,v 1.225 2010/05/18 18:03:31 apizer Exp $
#
# Copyright &copy; 2000-2021 The WeBWorK Project, http://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
Expand Down Expand Up @@ -555,29 +554,9 @@ $default_status = "Enrolled";
# Database options
################################################################################

# these variables are used by database.conf. we define them here so that editing
# database.conf isn't necessary.

# required permissions
# GRANT SELECT ON webwork.* TO webworkRead@localhost IDENTIFIED BY 'passwordRO';
# GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX, LOCK TABLES ON webwork.* TO webworkWrite@localhost IDENTIFIED BY 'passwordRW';


################################################################################
# set these variables in site.conf
#
# $database_dsn = "dbi:mysql:webwork";
# $database_username = "webworkWrite";
# $database_password = ""; #set this in site.conf
# $database_debug = 0;
################################################################################



# Database schemas are defined in the file conf/database.conf and stored in the
# hash %dbLayouts. The standard schema is called "sql_single";


include( "./conf/database.conf.dist"); # always include database.conf.dist

# in the rare case where you want local overrides
Expand Down
51 changes: 43 additions & 8 deletions conf/site.conf.dist
@@ -1,8 +1,7 @@
#!perl
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright &copy; 2000-2018 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader: webwork2/conf/site.conf.dist,v 1.225 2010/05/18 18:03:31 apizer Exp $
# Copyright &copy; 2000-2021 The WeBWorK Project, http://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
Expand Down Expand Up @@ -161,6 +160,7 @@ $externalPrograms{mysqldump} ="/usr/bin/mysqldump";
# Database options
################################################################################

# $database_debug = 0;

# Standard permissions command used to initialize the webwork database
# GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX, LOCK TABLES ON webwork.* TO webworkWrite@localhost IDENTIFIED BY 'passwordRW';
Expand All @@ -174,12 +174,47 @@ $externalPrograms{mysqldump} ="/usr/bin/mysqldump";
# Edit the $database_password line and replace 'passwordRW' by the actual password used in the GRANT command above
################################################################################

# The database dsn is the path to the WeBWorK database which you have created.
# Unless you have given the database a different name or the database resides on another
# server you do not need to change this first value.
# The format is dbi:mysql:[databasename] for databases on the local machine
# For a remote database the format is dbi:mysql:[databasename]:[hostname]:[port]
$database_dsn ="dbi:mysql:webwork";
# The database DSN is the path to the WeBWorK database which you have created.

# Modern database DSN format:
# DBI:driver:database=$database;host=$hostname;port=$port (when DB not on localhost)
# or DBI:driver:database=$database;host=127.0.0.1;port=$port (when DB on localhost, using TCP)
# See: https://metacpan.org/pod/DBD::MariaDB#port
# "To connect to a MariaDB or MySQL server on localhost using TCP/IP,
# you must specify the host as 127.0.0.1 with the optional port, e.g. 3306."
# or DBI:driver:database=$database (when DB on localhost, using socket)

# One thing on which it depends is the driver name, which you may want to modify.
# It also depends on the database name, which may be non-standard in some settings,
# as may be the hostname and port of the database server.

# driver should be one of:
# "mysql" for the DBD:mysql driver
# "MariaDB" for the DBD:mysql driver

# Select the desired DB driver:
#$database_driver="mysql";
$database_driver="MariaDB";

$database_host="localhost";
$database_port="3306";
$database_name="webwork";

# For a DB on localhost - default to using Unix socket.
# Change to 0 to use a TCP connection to 127.0.0.1.
$database_use_socket_if_localhost=1;

if ( $database_host eq "localhost" ) {
if ( $database_use_socket_if_localhost ) {
$database_dsn="DBI:$database_driver:database=$database_name";
} else {
$database_dsn="DBI:$database_driver:database=$database_name;host=127.0.0.1;port=$database_port";
}
} else {
$database_dsn="DBI:$database_driver:database=$database_name;host=$database_host;port=$database_port";
}

# The default storange engine to use is set here:
$database_storage_engine = 'myisam';

#########################
Expand Down
12 changes: 10 additions & 2 deletions lib/WeBWorK/DB/Driver/SQL.pm
Expand Up @@ -61,6 +61,15 @@ sub new($$$) {

my $self = $proto->SUPER::new($source, $params);

# The DBD::MariaDB driver should not get the
# mysql_enable_utf8mb4 or mysql_enable_utf8 settings,
# but DBD::mysql should.
my %utf8_parameters = ();
if ( $source =~ /DBI:mysql/ ) {
$utf8_parameters{mysql_enable_utf8mb4} = 1;
$utf8_parameters{mysql_enable_utf8} = 1;
}

# add handle
$self->{handle} = DBI->connect_cached(
$source,
Expand All @@ -70,8 +79,7 @@ sub new($$$) {
PrintError => 0,
RaiseError => 1,

mysql_enable_utf8mb4 => 1,
mysql_enable_utf8 => 1, # for older versions of DBD-mysql Perl modules
%utf8_parameters,
},
);
die $DBI::errstr unless defined $self->{handle};
Expand Down