From 3dcfda49cafd05b35844fdb194bd783ae3857d02 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Sat, 29 Jul 2006 17:36:56 +0000 Subject: Bug 346375: Move the code that gets the initial $dbh into Bugzilla::DB Patch By Max Kanat-Alexander (module owner) a=justdave --- Bugzilla/DB.pm | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 158 insertions(+), 4 deletions(-) (limited to 'Bugzilla/DB.pm') diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 044aa3dea..2cd974117 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -37,6 +37,8 @@ use base qw(DBI::db); use Bugzilla::Config qw(:db); use Bugzilla::Constants; +use Bugzilla::Install::Requirements; +use Bugzilla::Install::Localconfig; use Bugzilla::Util; use Bugzilla::Error; use Bugzilla::DB::Schema; @@ -62,10 +64,7 @@ sub connect_shadow { } sub connect_main { - my ($no_db_name) = @_; - my $connect_to_db = $db_name; - $connect_to_db = "" if $no_db_name; - return _connect($db_driver, $db_host, $connect_to_db, $db_port, + return _connect($db_driver, $db_host, $db_name, $db_port, $db_sock, $db_user, $db_pass); } @@ -95,6 +94,134 @@ sub _handle_error { return 0; # Now let DBI handle raising the error } +sub bz_check_requirements { + my ($output) = @_; + + my $db = DB_MODULE->{lc($db_driver)}; + # Only certain values are allowed for $db_driver. + if (!defined $db) { + die "$db_driver is not a valid choice for \$db_driver in" + . bz_locations()->{'localconfig'}; + } + + # Check the existence and version of the DBD that we need. + my $dbd = $db->{dbd}; + my $dbd_ver = $db->{dbd_version}; + my $sql_server = $db->{name}; + my $sql_want = $db->{db_version}; + unless (have_vers($dbd, $dbd_ver, $output)) { + my $command = install_command($dbd); + my $root = ROOT_USER; + my $version = $dbd_ver ? " $dbd_ver or higher" : ''; + print <bz_server_version; + $dbh->disconnect; + + # Check what version of the database server is installed and let + # the user know if the version is too old to be used with Bugzilla. + if ( vers_cmp($sql_vers,$sql_want) > -1 ) { + print "ok: found v$sql_vers\n" if $output; + } else { + print <do("CREATE DATABASE $db_name") }) { + my $error = $dbh->errstr; + chomp($error); + print STDERR "The '$db_name' database could not be created.", + " The error returned was:\n\n $error\n\n", + _bz_connect_error_reasons(); + exit; + } + } + + $dbh->disconnect; +} + +# A helper for bz_create_database and bz_check_requirements. +sub _get_no_db_connection { + my ($sql_server) = @_; + my $dbh; + my $conn_success = eval { + $dbh = _connect($db_driver, $db_host, '', $db_port, + $db_sock, $db_user, $db_pass); + }; + if (!$conn_success) { + my $sql_server = DB_MODULE->{lc($db_driver)}->{name}; + # Can't use $dbh->errstr because $dbh is undef. + my $error = $DBI::errstr; + chomp($error); + print STDERR "There was an error connecting to $sql_server:\n\n", + " $error\n\n", _bz_connect_error_reasons(); + exit; + } + return $dbh; +} + +# Just a helper because we have to re-use this text. +# We don't use this in db_new because it gives away the database +# username, and db_new errors can show up on CGIs. +sub _bz_connect_error_reasons { + my $lc_file = bz_locations()->{'localconfig'}; + my $db = DB_MODULE->{lc($db_driver)}; + my $server = $db->{name}; + +return < + +Description: Checks to make sure that you have the correct + DBD and database version installed for the + database that Bugzilla will be using. + Prints a message and exits if you don't + pass the requirements. + If C<$db_check> is true (from F), we won't + check the database version. + +Params: C<$output> - C if the function should display + informational output about what it's doing, such + as versions found. + +Returns: nothing + +=item C + +Description: Creates an empty database with the name + C<$db_name>, if that database doesn't + already exist. Prints an error message and + exits if we can't create the database. + +Params: none + +Returns: nothing + =item C<_connect> Description: Internal function, creates and returns a new, connected -- cgit v1.2.3-24-g4f1b