Mysql.pm 39 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

=head1 NAME

Bugzilla::DB::Mysql - Bugzilla database compatibility layer for MySQL

=head1 DESCRIPTION

This module overrides methods of the Bugzilla::DB module with MySQL specific
implementation. It is instantiated by the Bugzilla::DB module and should never
be used directly.

For interface details see L<Bugzilla::DB> and L<DBI>.

=cut

package Bugzilla::DB::Mysql;
23 24

use 5.10.1;
25
use Moo;
26

27
extends qw(Bugzilla::DB);
28

29
use Bugzilla::Constants;
30
use Bugzilla::Install::Util qw(install_string);
31
use Bugzilla::Util;
32
use Bugzilla::Error;
33
use Bugzilla::DB::Schema::Mysql;
34

35
use List::Util qw(max any);
36
use Text::ParseWords;
37
use Carp;
38

39
# This is how many comments of MAX_COMMENT_LENGTH we expect on a single bug.
40
# In reality, you could have a LOT more comments than this, because
41 42 43
# MAX_COMMENT_LENGTH is big.
use constant MAX_COMMENTS => 50;

44
use constant FULLTEXT_OR => '|';
45

46
sub BUILDARGS {
47 48 49 50 51 52 53 54 55
  my ($class, $params) = @_;
  my ($user, $pass, $host, $dbname, $port, $sock)
    = @$params{qw(db_user db_pass db_host db_name db_port db_sock)};

  # construct the DSN from the parameters we got
  my $dsn = "dbi:mysql:host=$host;database=$dbname";
  $dsn .= ";port=$port"         if $port;
  $dsn .= ";mysql_socket=$sock" if $sock;

56
  my %attrs = (mysql_enable_utf8 => 1);
57 58 59 60 61 62 63 64 65 66 67 68 69 70

  # MySQL SSL options
  my ($ssl_ca_file, $ssl_ca_path, $ssl_cert, $ssl_key) = @$params{
    qw(db_mysql_ssl_ca_file db_mysql_ssl_ca_path
      db_mysql_ssl_client_cert db_mysql_ssl_client_key)
  };
  if ($ssl_ca_file || $ssl_ca_path || $ssl_cert || $ssl_key) {
    $attrs{'mysql_ssl'}             = 1;
    $attrs{'mysql_ssl_ca_file'}     = $ssl_ca_file if $ssl_ca_file;
    $attrs{'mysql_ssl_ca_path'}     = $ssl_ca_path if $ssl_ca_path;
    $attrs{'mysql_ssl_client_cert'} = $ssl_cert if $ssl_cert;
    $attrs{'mysql_ssl_client_key'}  = $ssl_key if $ssl_key;
  }

71 72 73 74 75
  return {dsn => $dsn, user => $user, pass => $pass, attrs => \%attrs};
}

sub on_dbi_connected {
  my ($class, $dbh) = @_;
76 77 78

  # This makes sure that if the tables are encoded as UTF-8, we
  # return their data correctly.
79 80 81
  my $charset = $class->utf8_charset;
  my $collate = $class->utf8_collate;
  $dbh->do("SET NAMES $charset COLLATE $collate");
82 83 84

  # Check for MySQL modes.
  my ($var, $sql_mode)
85
    = $dbh->selectrow_array("SHOW VARIABLES LIKE 'sql\\_mode'");
86 87 88 89 90 91 92 93 94 95 96 97

  # Disable ANSI and strict modes, else Bugzilla will crash.
  if ($sql_mode) {

    # STRICT_TRANS_TABLE or STRICT_ALL_TABLES enable MySQL strict mode,
    # causing bug 321645. TRADITIONAL sets these modes (among others) as
    # well, so it has to be stipped as well
    my $new_sql_mode = join(",",
      grep { $_ !~ /^(?:ANSI|STRICT_(?:TRANS|ALL)_TABLES|TRADITIONAL)$/ }
        split(/,/, $sql_mode));

    if ($sql_mode ne $new_sql_mode) {
98
      $dbh->do("SET SESSION sql_mode = ?", undef, $new_sql_mode);
99
    }
100
  }
101

102 103
  # Allow large GROUP_CONCATs (largely for inserting comments
  # into bugs_fulltext).
104
  $dbh->do('SET SESSION group_concat_max_len = 128000000');
105

106 107
  # MySQL 5.5.2 and older have this variable set to true, which causes
  # trouble, see bug 870369.
108
  $dbh->do('SET SESSION sql_auto_is_null = 0');
109 110 111 112 113
}

# when last_insert_id() is supported on MySQL by lowest DBI/DBD version
# required by Bugzilla, this implementation can be removed.
sub bz_last_key {
114
  my ($self) = @_;
115

116
  my ($last_insert_id) = $self->selectrow_array('SELECT LAST_INSERT_ID()');
117

118
  return $last_insert_id;
119 120
}

121
sub sql_group_concat {
122 123 124 125 126 127 128 129 130 131 132 133
  my ($self, $column, $separator, $sort, $order_by) = @_;
  $separator = $self->quote(', ') if !defined $separator;
  $sort = 1 if !defined $sort;
  if ($order_by) {
    $column .= " ORDER BY $order_by";
  }
  elsif ($sort) {
    my $sort_order = $column;
    $sort_order =~ s/^DISTINCT\s+//i;
    $column = "$column ORDER BY $sort_order";
  }
  return "GROUP_CONCAT($column SEPARATOR $separator)";
134 135
}

136
sub sql_regexp {
137 138
  my ($self, $expr, $pattern, $nocheck, $real_pattern) = @_;
  $real_pattern ||= $pattern;
139

140
  $self->bz_check_regexp($real_pattern) if !$nocheck;
141

142
  return "$expr REGEXP $pattern";
143 144 145
}

sub sql_not_regexp {
146 147
  my ($self, $expr, $pattern, $nocheck, $real_pattern) = @_;
  $real_pattern ||= $pattern;
148

149
  $self->bz_check_regexp($real_pattern) if !$nocheck;
150

151
  return "$expr NOT REGEXP $pattern";
152 153 154
}

sub sql_limit {
155 156 157 158 159 160 161 162
  my ($self, $limit, $offset) = @_;

  if (defined($offset)) {
    return "LIMIT $offset, $limit";
  }
  else {
    return "LIMIT $limit";
  }
163 164
}

165
sub sql_string_concat {
166 167 168
  my ($self, @params) = @_;

  return 'CONCAT(' . join(', ', @params) . ')';
169 170 171
}

sub sql_fulltext_search {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  my ($self, $column, $text) = @_;

  # Add the boolean mode modifier if the search string contains
  # boolean operators at the start or end of a word.
  my $mode = '';
  if ($text =~ /(?:^|\W)[+\-<>~"()]/ || $text =~ /[()"*](?:$|\W)/) {
    $mode = 'IN BOOLEAN MODE';

    my @terms = split(quotemeta(FULLTEXT_OR), $text);
    foreach my $term (@terms) {

      # quote un-quoted compound words
      my @words = quotewords('[\s()]+', 'delimiters', $term);
      foreach my $word (@words) {

        # match words that have non-word chars in the middle of them
        if ($word =~ /\w\W+\w/ && $word !~ m/"/) {
          $word = '"' . $word . '"';
190
        }
191 192
      }
      $term = join('', @words);
193
    }
194 195
    $text = join(FULLTEXT_OR, @terms);
  }
196

197 198
  # quote the text for use in the MATCH AGAINST expression
  $text = $self->quote($text);
199

200 201
  # untaint the text, since it's safe to use now that we've quoted it
  trick_taint($text);
202

203
  return "MATCH($column) AGAINST($text $mode)";
204 205
}

206
sub sql_istring {
207 208 209
  my ($self, $string) = @_;

  return $string;
210 211
}

212
sub sql_from_days {
213
  my ($self, $days) = @_;
214

215
  return "FROM_DAYS($days)";
216 217
}

218
sub sql_to_days {
219
  my ($self, $date) = @_;
220

221
  return "TO_DAYS($date)";
222 223 224
}

sub sql_date_format {
225 226 227
  my ($self, $date, $format) = @_;

  $format = "%Y.%m.%d %H:%i:%s" if !$format;
228

229
  return "DATE_FORMAT($date, " . $self->quote($format) . ")";
230 231
}

232
sub sql_date_math {
233 234 235
  my ($self, $date, $operator, $interval, $units) = @_;

  return "$date $operator INTERVAL $interval $units";
236 237
}

238
sub sql_iposition {
239 240
  my ($self, $fragment, $text) = @_;
  return "INSTR($text, $fragment)";
241
}
242

243
sub sql_position {
244
  my ($self, $fragment, $text) = @_;
245

246
  return "INSTR(CAST($text AS BINARY), CAST($fragment AS BINARY))";
247 248
}

249
sub sql_group_by {
250
  my ($self, $needed_columns, $optional_columns) = @_;
251

252 253 254 255 256
  # MySQL allows you to specify the minimal subset of columns to get
  # a unique result. While it does allow specifying all columns as
  # ANSI SQL requires, according to MySQL documentation, the fewer
  # columns you specify, the faster the query runs.
  return "GROUP BY $needed_columns";
257 258
}

259
sub bz_explain {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  my ($self, $sql) = @_;
  my $sth = $self->prepare("EXPLAIN $sql");
  $sth->execute();
  my $columns       = $sth->{'NAME'};
  my $lengths       = $sth->{'mysql_max_length'};
  my $format_string = '|';
  my $i             = 0;
  foreach my $column (@$columns) {

    # Sometimes the column name is longer than the contents.
    my $length = max($lengths->[$i], length($column));
    $format_string .= ' %-' . $length . 's |';
    $i++;
  }

275
  my $first_row    = sprintf($format_string, @$columns);
276 277 278 279 280 281 282
  my @explain_rows = ($first_row, '-' x length($first_row));
  while (my $row = $sth->fetchrow_arrayref) {
    my @fixed = map { defined $_ ? $_ : 'NULL' } @$row;
    push(@explain_rows, sprintf($format_string, @fixed));
  }

  return join("\n", @explain_rows);
283
}
284

285
sub _bz_get_initial_schema {
286 287
  my ($self) = @_;
  return $self->_bz_build_schema_from_disk();
288 289
}

290 291 292 293
#####################################################################
# Database Setup
#####################################################################

294
sub bz_check_server_version {
295
  my $self = shift;
296

297 298 299 300 301
  my $lc = Bugzilla->localconfig;
  if (lc(Bugzilla->localconfig->{db_name}) eq 'mysql') {
    die "It is not safe to run Bugzilla inside a database named 'mysql'.\n"
      . " Please pick a different value for \$db_name in localconfig.\n";
  }
302

303
  $self->SUPER::bz_check_server_version(@_);
304 305
}

306
sub bz_setup_database {
307 308 309 310 311 312 313 314 315 316 317 318 319
  my ($self) = @_;

  # The "comments" field of the bugs_fulltext table could easily exceed
  # MySQL's default max_allowed_packet. Also, MySQL should never have
  # a max_allowed_packet smaller than our max_attachment_size. So, we
  # warn the user here if max_allowed_packet is too small.
  my $min_max_allowed = MAX_COMMENTS * MAX_COMMENT_LENGTH;
  my (undef, $current_max_allowed)
    = $self->selectrow_array(q{SHOW VARIABLES LIKE 'max\_allowed\_packet'});

  # This parameter is not yet defined when the DB is being built for
  # the very first time. The code below still works properly, however,
  # because the default maxattachmentsize is smaller than $min_max_allowed.
320
  my $max_attachment     = (Bugzilla->params->{'maxattachmentsize'} || 0) * 1024;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  my $needed_max_allowed = max($min_max_allowed, $max_attachment);
  if ($current_max_allowed < $needed_max_allowed) {
    warn install_string('max_allowed_packet',
      {current => $current_max_allowed, needed => $needed_max_allowed})
      . "\n";
  }

  # Make sure the installation has InnoDB turned on, or we're going to be
  # doing silly things like making foreign keys on MyISAM tables, which is
  # hard to fix later. We do this up here because none of the code below
  # works if InnoDB is off. (Particularly if we've already converted the
  # tables to InnoDB.)
  my %engines = @{$self->selectcol_arrayref('SHOW ENGINES', {Columns => [1, 2]})};
  if (!$engines{InnoDB} || $engines{InnoDB} !~ /^(YES|DEFAULT)$/) {
    die install_string('mysql_innodb_disabled');
  }

338 339 340 341 342 343 344 345 346 347 348 349 350
  if ($self->utf8_charset eq 'utf8mb4') {
    my %global = map {@$_}
      @{$self->selectall_arrayref(q(SHOW GLOBAL VARIABLES LIKE 'innodb_%'))};
    my $utf8mb4_supported
      = $global{innodb_file_format} eq 'Barracuda'
      && $global{innodb_file_per_table} eq 'ON'
      && $global{innodb_large_prefix} eq 'ON';

    die install_string('mysql_innodb_settings') unless $utf8mb4_supported;

    my $tables = $self->selectall_arrayref('SHOW TABLE STATUS');
    foreach my $table_status (@$tables) {
      my ($table, undef, undef, $row_format) = @$table_status;
351
      my $table_type     = $table_status->[-1];
352 353 354 355 356 357 358 359 360 361 362 363 364 365
      my $new_row_format = $self->default_row_format($table);
      next if lc($table_type) eq 'view';
      next if lc($new_row_format) eq 'compact';
      next if lc($row_format) eq 'dynamic';
      next if lc($row_format) eq 'compressed';
      if (lc($new_row_format) ne lc($row_format)) {
        print install_string(
          'mysql_row_format_conversion', {table => $table, format => $new_row_format}
          ),
          "\n";
        $self->do(sprintf 'ALTER TABLE %s ROW_FORMAT=%s', $table, $new_row_format);
      }
    }
  }
366 367 368 369 370 371 372 373 374 375

  my ($sd_index_deleted, $longdescs_index_deleted);
  my @tables = $self->bz_table_list_real();

  # We want to convert tables to InnoDB, but it's possible that they have
  # fulltext indexes on them, and conversion will fail unless we remove
  # the indexes.
  if (grep($_ eq 'bugs', @tables) and !grep($_ eq 'bugs_fulltext', @tables)) {
    if ($self->bz_index_info_real('bugs', 'short_desc')) {
      $self->bz_drop_index_raw('bugs', 'short_desc');
376
    }
377 378 379
    if ($self->bz_index_info_real('bugs', 'bugs_short_desc_idx')) {
      $self->bz_drop_index_raw('bugs', 'bugs_short_desc_idx');
      $sd_index_deleted = 1;    # Used for later schema cleanup.
380
    }
381 382 383 384 385
  }
  if (grep($_ eq 'longdescs', @tables) and !grep($_ eq 'bugs_fulltext', @tables))
  {
    if ($self->bz_index_info_real('longdescs', 'thetext')) {
      $self->bz_drop_index_raw('longdescs', 'thetext');
386
    }
387 388 389
    if ($self->bz_index_info_real('longdescs', 'longdescs_thetext_idx')) {
      $self->bz_drop_index_raw('longdescs', 'longdescs_thetext_idx');
      $longdescs_index_deleted = 1;    # For later schema cleanup.
390
    }
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  }

  # Upgrade tables from MyISAM to InnoDB
  my $db_name       = Bugzilla->localconfig->{db_name};
  my $myisam_tables = $self->selectcol_arrayref(
    'SELECT TABLE_NAME FROM information_schema.TABLES 
          WHERE TABLE_SCHEMA = ? AND ENGINE = ?', undef, $db_name, 'MyISAM'
  );

  if (scalar @$myisam_tables) {
    print "Bugzilla now uses the InnoDB storage engine in MySQL for",
      " most tables.\nConverting tables to InnoDB:\n";
    foreach my $table (@$myisam_tables) {
      print "Converting table $table... ";
      $self->do("ALTER TABLE $table ENGINE = InnoDB");
      print "done.\n";
407
    }
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  }

  # Versions of Bugzilla before the existence of Bugzilla::DB::Schema did
  # not provide explicit names for the table indexes. This means
  # that our upgrades will not be reliable, because we look for the name
  # of the index, not what fields it is on, when doing upgrades.
  # (using the name is much better for cross-database compatibility
  # and general reliability). It's also very important that our
  # Schema object be consistent with what is on the disk.
  #
  # While we're at it, we also fix some inconsistent index naming
  # from the original checkin of Bugzilla::DB::Schema.

  # We check for the existence of a particular "short name" index that
  # has existed at least since Bugzilla 2.8, and probably earlier.
  # For fixing the inconsistent naming of Schema indexes,
  # we also check for one of those inconsistently-named indexes.
  if (
    grep($_ eq 'bugs', @tables)
    && ( $self->bz_index_info_real('bugs', 'assigned_to')
      || $self->bz_index_info_real('flags', 'flags_bidattid_idx'))
    )
  {

    # This is a check unrelated to the indexes, to see if people are
    # upgrading from 2.18 or below, but somehow have a bz_schema table
    # already. This only happens if they have done a mysqldump into
    # a database without doing a DROP DATABASE first.
    # We just do the check here since this check is a reliable way
    # of telling that we are upgrading from a version pre-2.20.
    if (grep($_ eq 'bz_schema', $self->bz_table_list_real())) {
      die install_string('bz_schema_exists_before_220');
440
    }
441

442
    my $bug_count = $self->selectrow_array("SELECT COUNT(*) FROM bugs");
443

444 445 446
    # We estimate one minute for each 3000 bugs, plus 3 minutes just
    # to handle basic MySQL stuff.
    my $rename_time = int($bug_count / 3000) + 3;
447

448 449 450 451
    # And 45 minutes for every 15,000 attachments, per some experiments.
    my ($attachment_count)
      = $self->selectrow_array("SELECT COUNT(*) FROM attachments");
    $rename_time += int(($attachment_count * 45) / 15000);
452

453 454 455 456
    # If we're going to take longer than 5 minutes, we let the user know
    # and allow them to abort.
    if ($rename_time > 5) {
      print "\n", install_string('mysql_index_renaming', {minutes => $rename_time});
457

458 459
      # Wait 45 seconds for them to respond.
      sleep(45) unless Bugzilla->installation_answers->{NO_PAUSE};
460
    }
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    print "Renaming indexes...\n";

    # We can't be interrupted, because of how the "if"
    # works above.
    local $SIG{INT}  = 'IGNORE';
    local $SIG{TERM} = 'IGNORE';
    local $SIG{PIPE} = 'IGNORE';

    # Certain indexes had names in Schema that did not easily conform
    # to a standard. We store those names here, so that they
    # can be properly renamed.
    # Also, sometimes an old mysqldump would incorrectly rename
    # unique indexes to "PRIMARY", so we address that here, also.
    my $bad_names = {

      # 'when' is a possible leftover from Bugzillas before 2.8
      bugs_activity =>
        ['when', 'bugs_activity_bugid_idx', 'bugs_activity_bugwhen_idx'],
      cc                 => ['PRIMARY'],
      longdescs          => ['longdescs_bugid_idx', 'longdescs_bugwhen_idx'],
      flags              => ['flags_bidattid_idx'],
      flaginclusions     => ['flaginclusions_tpcid_idx'],
      flagexclusions     => ['flagexclusions_tpc_id_idx'],
      keywords           => ['PRIMARY'],
      milestones         => ['PRIMARY'],
      profiles_activity  => ['profiles_activity_when_idx'],
      group_control_map  => ['group_control_map_gid_idx', 'PRIMARY'],
      user_group_map     => ['PRIMARY'],
      group_group_map    => ['PRIMARY'],
      email_setting      => ['PRIMARY'],
      bug_group_map      => ['PRIMARY'],
      category_group_map => ['PRIMARY'],
      watch              => ['PRIMARY'],
      namedqueries       => ['PRIMARY'],
      series_data        => ['PRIMARY'],

      # series_categories is dealt with below, not here.
    };

    # The series table is broken and needs to have one index
    # dropped before we begin the renaming, because it had a
    # useless index on it that would cause a naming conflict here.
    if (grep($_ eq 'series', @tables)) {
      my $dropname;

      # This is what the bad index was called before Schema.
      if ($self->bz_index_info_real('series', 'creator_2')) {
        $dropname = 'creator_2';
      }

      # This is what the bad index is called in Schema.
      elsif ($self->bz_index_info_real('series', 'series_creator_idx')) {
        $dropname = 'series_creator_idx';
      }
      $self->bz_drop_index_raw('series', $dropname) if $dropname;
516
    }
517

518 519 520 521 522 523
    # The email_setting table also had the same problem.
    if (grep($_ eq 'email_setting', @tables)
      && $self->bz_index_info_real('email_setting', 'email_settings_user_id_idx'))
    {
      $self->bz_drop_index_raw('email_setting', 'email_settings_user_id_idx');
    }
524

525 526
    # Go through all the tables.
    foreach my $table (@tables) {
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
      # Will contain the names of old indexes as keys, and the
      # definition of the new indexes as a value. The values
      # include an extra hash key, NAME, with the new name of
      # the index.
      my %rename_indexes;

      # And go through all the columns on each table.
      my @columns = $self->bz_table_columns_real($table);

      # We also want to fix the silly naming of unique indexes
      # that happened when we first checked-in Bugzilla::DB::Schema.
      if ($table eq 'series_categories') {

        # The series_categories index had a nonstandard name.
        push(@columns, 'series_cats_unique_idx');
      }
      elsif ($table eq 'email_setting') {

        # The email_setting table had a similar problem.
        push(@columns, 'email_settings_unique_idx');
      }
      else {
        push(@columns, "${table}_unique_idx");
      }

      # And this is how we fix the other inconsistent Schema naming.
      push(@columns, @{$bad_names->{$table}}) if (exists $bad_names->{$table});
      foreach my $column (@columns) {

        # If we have an index named after this column, it's an
        # old-style-name index.
        if (my $index = $self->bz_index_info_real($table, $column)) {

          # Fix the name to fit in with the new naming scheme.
          $index->{NAME} = $table . "_" . $index->{FIELDS}->[0] . "_idx";
          print "Renaming index $column to " . $index->{NAME} . "...\n";
          $rename_indexes{$column} = $index;
        }    # if
      }    # foreach column

      my @rename_sql
        = $self->_bz_schema->get_rename_indexes_ddl($table, %rename_indexes);
      $self->do($_) foreach (@rename_sql);

    }    # foreach table
  }    # if old-name indexes

  # If there are no tables, but the DB isn't utf8 and it should be,
  # then we should alter the database to be utf8. We know it should be
  # if the utf8 parameter is true or there are no params at all.
  # This kind of situation happens when people create the database
  # themselves, and if we don't do this they will get the big
  # scary WARNING statement about conversion to UTF8.
581
  unless ($self->bz_db_is_utf8) {
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    $self->_alter_db_charset_to_utf8();
  }

  # And now we create the tables and the Schema object.
  $self->SUPER::bz_setup_database();

  if ($sd_index_deleted) {
    $self->_bz_real_schema->delete_index('bugs', 'bugs_short_desc_idx');
    $self->_bz_store_real_schema;
  }
  if ($longdescs_index_deleted) {
    $self->_bz_real_schema->delete_index('longdescs', 'longdescs_thetext_idx');
    $self->_bz_store_real_schema;
  }

  # The old timestamp fields need to be adjusted here instead of in
  # checksetup. Otherwise the UPDATE statements inside of bz_add_column
  # will cause accidental timestamp updates.
  # The code that does this was moved here from checksetup.

  # 2002-08-14 - bbaetz@student.usyd.edu.au - bug 153578
  # attachments creation time needs to be a datetime, not a timestamp
  my $attach_creation = $self->bz_column_info("attachments", "creation_ts");
  if ($attach_creation && $attach_creation->{TYPE} =~ /^TIMESTAMP/i) {
    print "Fixing creation time on attachments...\n";

    my $sth = $self->prepare("SELECT COUNT(attach_id) FROM attachments");
    $sth->execute();
    my ($attach_count) = $sth->fetchrow_array();
611

612 613
    if ($attach_count > 1000) {
      print "This may take a while...\n";
614
    }
615
    my $i = 0;
616

617 618 619 620 621 622 623
    # This isn't just as simple as changing the field type, because
    # the creation_ts was previously updated when an attachment was made
    # obsolete from the attachment creation screen. So we have to go
    # and recreate these times from the comments..
    $sth = $self->prepare(
      "SELECT bug_id, attach_id, submitter_id " . "FROM attachments");
    $sth->execute();
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    # Restrict this as much as possible in order to avoid false
    # positives, and keep the db search time down
    my $sth2 = $self->prepare(
      "SELECT bug_when FROM longdescs 
                                    WHERE bug_id=? AND who=? 
                                          AND thetext LIKE ?
                                 ORDER BY bug_when " . $self->sql_limit(1)
    );
    while (my ($bug_id, $attach_id, $submitter_id) = $sth->fetchrow_array()) {
      $sth2->execute($bug_id, $submitter_id,
        "Created an attachment (id=$attach_id)%");
      my ($when) = $sth2->fetchrow_array();
      if ($when) {
        $self->do("UPDATE attachments "
            . "SET creation_ts='$when' "
            . "WHERE attach_id=$attach_id");
      }
      else {
        print "Warning - could not determine correct creation"
          . " time for attachment $attach_id on bug $bug_id\n";
      }
      ++$i;
      print "Converted $i of $attach_count attachments\n" if !($i % 1000);
648
    }
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    print "Done - converted $i attachments\n";

    $self->bz_alter_column("attachments", "creation_ts",
      {TYPE => 'DATETIME', NOTNULL => 1});
  }

  # 2004-08-29 - Tomas.Kopal@altap.cz, bug 257303
  # Change logincookies.lastused type from timestamp to datetime
  my $login_lastused = $self->bz_column_info("logincookies", "lastused");
  if ($login_lastused && $login_lastused->{TYPE} =~ /^TIMESTAMP/i) {
    $self->bz_alter_column('logincookies', 'lastused',
      {TYPE => 'DATETIME', NOTNULL => 1});
  }

  # 2005-01-17 - Tomas.Kopal@altap.cz, bug 257315
  # Change bugs.delta_ts type from timestamp to datetime
  my $bugs_deltats = $self->bz_column_info("bugs", "delta_ts");
  if ($bugs_deltats && $bugs_deltats->{TYPE} =~ /^TIMESTAMP/i) {
    $self->bz_alter_column('bugs', 'delta_ts', {TYPE => 'DATETIME', NOTNULL => 1});
  }

  # 2005-09-24 - bugreport@peshkin.net, bug 307602
  # Make sure that default 4G table limit is overridden
  my $attach_data_create = $self->selectrow_array(
    'SELECT CREATE_OPTIONS FROM information_schema.TABLES 
          WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', undef, $db_name, 'attach_data'
  );
  if ($attach_data_create !~ /MAX_ROWS/i) {
    print "Converting attach_data maximum size to 100G...\n";
    $self->do(
      "ALTER TABLE attach_data
680
                   AVG_ROW_LENGTH=1000000,
681 682 683 684 685 686 687 688 689 690 691
                   MAX_ROWS=100000"
    );
  }

  # Convert the database to UTF-8 if the utf8 parameter is on.
  # We check if any table isn't utf8, because lots of crazy
  # partial-conversion situations can happen, and this handles anything
  # that could come up (including having the DB charset be utf8 but not
  # the table charsets.
  #
  # TABLE_COLLATION IS NOT NULL prevents us from trying to convert views.
692 693
  my $charset         = $self->utf8_charset;
  my $collate         = $self->utf8_collate;
694 695
  my $non_utf8_tables = $self->selectrow_array(
    "SELECT 1 FROM information_schema.TABLES 
696
          WHERE TABLE_SCHEMA = ? AND TABLE_COLLATION IS NOT NULL 
697 698
                AND TABLE_COLLATION != ?
          LIMIT 1", undef, $db_name, $collate
699 700 701 702 703 704 705 706 707 708 709 710 711
  );

  if (Bugzilla->params->{'utf8'} && $non_utf8_tables) {
    print "\n", install_string('mysql_utf8_conversion');

    if (!Bugzilla->installation_answers->{NO_PAUSE}) {
      if (Bugzilla->installation_mode == INSTALLATION_MODE_NON_INTERACTIVE) {
        die install_string('continue_without_answers'), "\n";
      }
      else {
        print "\n         " . install_string('enter_or_ctrl_c');
        getc;
      }
712
    }
713

714 715
    print
      "Converting table storage format to $charset (collate $collate). This may take a while.\n";
716
    foreach my $table ($self->bz_table_list_real) {
717
      my $info_sth = $self->prepare("SHOW FULL COLUMNS FROM `$table`");
718 719 720 721 722 723 724 725 726 727 728
      $info_sth->execute();
      my (@binary_sql, @utf8_sql);
      while (my $column = $info_sth->fetchrow_hashref) {

        # Our conversion code doesn't work on enum fields, but they
        # all go away later in checksetup anyway.
        next if $column->{Type} =~ /enum/i;

        # If this particular column isn't stored in utf-8
        if ( $column->{Collation}
          && $column->{Collation} ne 'NULL'
729
          && $column->{Collation} ne $collate)
730 731
        {
          my $name = $column->{Field};
732

733
          print "$table.$name needs to be converted to $charset (collate $collate)...\n";
734

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
          # These will be automatically re-created at the end
          # of checksetup.
          $self->bz_drop_related_fks($table, $name);

          my $col_info = $self->bz_column_info_real($table, $name);

          # CHANGE COLUMN doesn't take PRIMARY KEY
          delete $col_info->{PRIMARYKEY};
          my $sql_def = $self->_bz_schema->get_type_ddl($col_info);

          # We don't want MySQL to actually try to *convert*
          # from our current charset to UTF-8, we just want to
          # transfer the bytes directly. This is how we do that.

          # The CHARACTER SET part of the definition has to come
          # right after the type, which will always come first.
          my ($binary, $utf8) = ($sql_def, $sql_def);
          my $type = $self->_bz_schema->convert_type($col_info->{TYPE});
          $binary =~ s/(\Q$type\E)/$1 CHARACTER SET binary/;
754
          $utf8   =~ s/(\Q$type\E)/$1 CHARACTER SET $charset COLLATE $collate/;
755 756
          push(@binary_sql, "MODIFY COLUMN $name $binary");
          push(@utf8_sql,   "MODIFY COLUMN $name $utf8");
757
        }
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
      }    # foreach column

      if (@binary_sql) {
        my %indexes = %{$self->bz_table_indexes($table)};
        foreach my $index_name (keys %indexes) {
          my $index = $indexes{$index_name};
          if ($index->{TYPE} and $index->{TYPE} eq 'FULLTEXT') {
            $self->bz_drop_index($table, $index_name);
          }
          else {
            delete $indexes{$index_name};
          }
        }

        print "Converting the $table table to UTF-8...\n";
        my $bin = "ALTER TABLE $table " . join(', ', @binary_sql);
774 775
        my $utf = "ALTER TABLE $table "
          . join(', ', @utf8_sql, "DEFAULT CHARACTER SET $charset COLLATE $collate");
776 777 778 779 780 781 782 783 784
        $self->do($bin);
        $self->do($utf);

        # Re-add any removed FULLTEXT indexes.
        foreach my $index (keys %indexes) {
          $self->bz_add_index($table, $index, $indexes{$index});
        }
      }
      else {
785
        $self->do("ALTER TABLE $table DEFAULT CHARACTER SET $charset COLLATE $collate");
786 787 788 789 790 791 792 793 794 795
      }

    }    # foreach my $table (@tables)
  }

  # Sometimes you can have a situation where all the tables are utf8,
  # but the database isn't. (This tends to happen when you've done
  # a mysqldump.) So we have this change outside of the above block,
  # so that it just happens silently if no actual *table* conversion
  # needs to happen.
796
  unless ($self->bz_db_is_utf8) {
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    $self->_alter_db_charset_to_utf8();
  }

  $self->_fix_defaults();

  # Bug 451735 highlighted a bug in bz_drop_index() which didn't
  # check for FKs before trying to delete an index. Consequently,
  # the series_creator_idx index was considered to be deleted
  # despite it was still present in the DB. That's why we have to
  # force the deletion, bypassing the DB schema.
  if (!$self->bz_index_info('series', 'series_category_idx')) {
    if (!$self->bz_index_info('series', 'series_creator_idx')
      && $self->bz_index_info_real('series', 'series_creator_idx'))
    {
      foreach my $column (qw(creator category subcategory name)) {
        $self->bz_drop_related_fks('series', $column);
      }
      $self->bz_drop_index_raw('series', 'series_creator_idx');
815
    }
816
  }
817 818 819 820 821 822 823 824 825
}

# When you import a MySQL 3/4 mysqldump into MySQL 5, columns that
# aren't supposed to have defaults will have defaults. This is only
# a minor issue, but it makes our tests fail, and it's good to keep
# the DB actually consistent with what DB::Schema thinks the database
# looks like. So we remove defaults from columns that aren't supposed
# to have them
sub _fix_defaults {
826
  my $self        = shift;
827 828 829 830 831
  my $maj_version = substr($self->bz_server_version, 0, 1);
  return if $maj_version < 5;

  # The oldest column that could have this problem is bugs.assigned_to,
  # so if it doesn't have the problem, we just skip doing this entirely.
832
  my $assi_def     = $self->_bz_raw_column_info('bugs', 'assigned_to');
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
  my $assi_default = $assi_def->{COLUMN_DEF};

  # This "ne ''" thing is necessary because _raw_column_info seems to
  # return COLUMN_DEF as an empty string for columns that don't have
  # a default.
  return unless (defined $assi_default && $assi_default ne '');

  my %fix_columns;
  foreach my $table ($self->_bz_real_schema->get_table_list()) {
    foreach my $column ($self->bz_table_columns($table)) {
      my $abs_def = $self->bz_column_info($table, $column);

      # BLOB/TEXT columns never have defaults
      next if $abs_def->{TYPE} =~ /BLOB|TEXT/i;
      if (!defined $abs_def->{DEFAULT}) {

        # Get the exact default from the database without any
        # "fixing" by bz_column_info_real.
851
        my $raw_info    = $self->_bz_raw_column_info($table, $column);
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
        my $raw_default = $raw_info->{COLUMN_DEF};
        if (defined $raw_default) {
          if ($raw_default eq '') {

            # Only (var)char columns can have empty strings as
            # defaults, so if we got an empty string for some
            # other default type, then it's bogus.
            next unless $abs_def->{TYPE} =~ /char/i;
            $raw_default = "''";
          }
          $fix_columns{$table} ||= [];
          push(@{$fix_columns{$table}}, $column);
          print "$table.$column has incorrect DB default: $raw_default\n";
        }
      }
    }    # foreach $column
  }    # foreach $table

  print "Fixing defaults...\n";
  foreach my $table (reverse sort keys %fix_columns) {
    my @alters = map("ALTER COLUMN $_ DROP DEFAULT", @{$fix_columns{$table}});
873
    my $sql    = "ALTER TABLE $table " . join(',', @alters);
874 875
    $self->do($sql);
  }
876 877
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
sub utf8_charset {
  return 'utf8' unless Bugzilla->params->{'utf8'};
  return Bugzilla->params->{'utf8'} eq 'utf8mb4' ? 'utf8mb4' : 'utf8';
}

sub utf8_collate {
  my $charset = utf8_charset();
  if ($charset eq 'utf8') {
    return 'utf8_general_ci';
  }
  elsif ($charset eq 'utf8mb4') {
    return 'utf8mb4_unicode_520_ci';
  }
  else {
    croak "invalid charset: $charset";
  }
}

sub default_row_format {
  my ($class, $table) = @_;
  my $charset = utf8_charset();
  if ($charset eq 'utf8') {
    return 'Compact';
  }
  elsif ($charset eq 'utf8mb4') {
    my @no_compress = qw(
      bug_user_last_visit
      cc
      email_rates
      logincookies
      token_data
      tokens
      ts_error
      ts_exitstatus
      ts_funcmap
      ts_job
      ts_note
      user_request_log
      votes
    );
    return 'Dynamic' if any { $table eq $_ } @no_compress;
    return 'Compressed';
  }
  else {
    croak "invalid charset: $charset";
  }
}

926
sub _alter_db_charset_to_utf8 {
927 928
  my $self    = shift;
  my $db_name = Bugzilla->localconfig->{db_name};
929 930 931
  my $charset = $self->utf8_charset;
  my $collate = $self->utf8_collate;
  $self->do("ALTER DATABASE $db_name CHARACTER SET $charset COLLATE $collate");
932 933
}

934
sub bz_db_is_utf8 {
935
  my $self = shift;
936
  my $db_charset
937 938 939
    = $self->selectrow_arrayref("SHOW VARIABLES LIKE 'character_set_database'");

  # First column holds the variable name, second column holds the value.
940 941
  my $charset = $self->utf8_charset;
  return $db_charset->[1] eq $charset ? 1 : 0;
942 943
}

944

945
sub bz_enum_initial_values {
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
  my ($self) = @_;
  my %enum_values = %{$self->ENUM_DEFAULTS};

  # Get a complete description of the 'bugs' table; with DBD::MySQL
  # there isn't a column-by-column way of doing this.  Could use
  # $dbh->column_info, but it would go slower and we would have to
  # use the undocumented mysql_type_name accessor to get the type
  # of each row.
  my $sth = $self->prepare("DESCRIBE bugs");
  $sth->execute();

  # Look for the particular columns we are interested in.
  while (my ($thiscol, $thistype) = $sth->fetchrow_array()) {
    if (defined $enum_values{$thiscol}) {

      # this is a column of interest.
      my @value_list;
      if ($thistype and ($thistype =~ /^enum\(/)) {

        # it has an enum type; get the set of values.
        while ($thistype =~ /'([^']*)'(.*)/) {
          push(@value_list, $1);
          $thistype = $2;
969
        }
970 971 972 973 974 975
      }
      if (@value_list) {

        # record the enum values found.
        $enum_values{$thiscol} = \@value_list;
      }
976
    }
977
  }
978

979
  return \%enum_values;
980 981
}

982 983 984 985 986 987
#####################################################################
# MySQL-specific Database-Reading Methods
#####################################################################

=begin private

988
=head1 MYSQL-SPECIFIC DATABASE-READING METHODS
989 990 991 992 993 994 995 996 997

These methods read information about the database from the disk,
instead of from a Schema object. They are only reliable for MySQL 
(see bug 285111 for the reasons why not all DBs use/have functions
like this), but that's OK because we only need them for 
backwards-compatibility anyway, for versions of Bugzilla before 2.20.

=over 4

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
=item C<bz_column_info_real($table, $column)>

 Description: Returns an abstract column definition for a column
              as it actually exists on disk in the database.
 Params:      $table - The name of the table the column is on.
              $column - The name of the column you want info about.
 Returns:     An abstract column definition.
              If the column does not exist, returns undef.

=cut

sub bz_column_info_real {
1010 1011 1012
  my ($self, $table, $column) = @_;
  my $col_data = $self->_bz_raw_column_info($table, $column);
  return $self->_bz_schema->column_info_to_column($col_data);
1013 1014 1015
}

sub _bz_raw_column_info {
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  my ($self, $table, $column) = @_;

  # DBD::mysql does not support selecting a specific column,
  # so we have to get all the columns on the table and find
  # the one we want.
  my $info_sth = $self->column_info(undef, undef, $table, '%');

  # Don't use fetchall_hashref as there's a Win32 DBI bug (292821)
  my $col_data;
  while ($col_data = $info_sth->fetchrow_hashref) {
    last if $col_data->{'COLUMN_NAME'} eq $column;
  }

  if (!defined $col_data) {
    return undef;
  }
  return $col_data;
1033 1034
}

1035 1036 1037 1038 1039 1040 1041
=item C<bz_index_info_real($table, $index)>

 Description: Returns information about an index on a table in the database.
 Params:      $table = name of table containing the index
              $index = name of an index
 Returns:     An abstract index definition, always in hashref format.
              If the index does not exist, the function returns undef.
1042

1043
=cut
1044

1045
sub bz_index_info_real {
1046 1047
  my ($self, $table, $index) = @_;

1048
  my $sth = $self->prepare("SHOW INDEX FROM `$table`");
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  $sth->execute;

  my @fields;
  my $index_type;

  # $raw_def will be an arrayref containing the following information:
  # 0 = name of the table that the index is on
  # 1 = 0 if unique, 1 if not unique
  # 2 = name of the index
  # 3 = seq_in_index (The order of the current field in the index).
  # 4 = Name of ONE column that the index is on
  # 5 = 'Collation' of the index. Usually 'A'.
  # 6 = Cardinality. Either a number or undef.
  # 7 = sub_part. Usually undef. Sometimes 1.
  # 8 = "packed". Usually undef.
  # 9 = Null. Sometimes undef, sometimes 'YES'.
  # 10 = Index_type. The type of the index. Usually either 'BTREE' or 'FULLTEXT'
  # 11 = 'Comment.' Usually undef.
  while (my $raw_def = $sth->fetchrow_arrayref) {
    if ($raw_def->[2] eq $index) {
      push(@fields, $raw_def->[4]);

      # No index can be both UNIQUE and FULLTEXT, that's why
      # this is written this way.
      $index_type = $raw_def->[1] ? '' : 'UNIQUE';
      $index_type = $raw_def->[10] eq 'FULLTEXT' ? 'FULLTEXT' : $index_type;
1075
    }
1076
  }
1077

1078 1079 1080 1081 1082
  my $retval;
  if (scalar(@fields)) {
    $retval = {FIELDS => \@fields, TYPE => $index_type};
  }
  return $retval;
1083 1084
}

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
=item C<bz_index_list_real($table)>

 Description: Returns a list of index names on a table in 
              the database, as it actually exists on disk.
 Params:      $table - The name of the table you want info about.
 Returns:     An array of index names.

=cut

sub bz_index_list_real {
1095
  my ($self, $table) = @_;
1096
  my $sth = $self->prepare("SHOW INDEX FROM `$table`");
1097 1098 1099

  # Column 3 of a SHOW INDEX statement contains the name of the index.
  return @{$self->selectcol_arrayref($sth, {Columns => [3]})};
1100 1101 1102 1103 1104 1105 1106 1107
}

#####################################################################
# MySQL-Specific "Schema Builder"
#####################################################################

=back

1108
=head1 MYSQL-SPECIFIC "SCHEMA BUILDER"
1109 1110 1111 1112 1113

MySQL needs to be able to read in a legacy database (from before 
Schema existed) and create a Schema object out of it. That's what
this code does.

1114 1115
=end private

1116 1117 1118 1119 1120 1121 1122
=cut

# This sub itself is actually written generically, but the subroutines
# that it depends on are database-specific. In particular, the
# bz_column_info_real function would be very difficult to create
# properly for any other DB besides MySQL.
sub _bz_build_schema_from_disk {
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  my ($self) = @_;

  my $schema = $self->_bz_schema->get_empty_schema();

  my @tables = $self->bz_table_list_real();
  if (@tables) {
    print "Building Schema object from database...\n";
  }
  foreach my $table (@tables) {
    $schema->add_table($table);
    my @columns = $self->bz_table_columns_real($table);
    foreach my $column (@columns) {
      my $type_info = $self->bz_column_info_real($table, $column);
      $schema->set_column($table, $column, $type_info);
1137
    }
1138

1139 1140 1141 1142 1143 1144 1145
    my @indexes = $self->bz_index_list_real($table);
    foreach my $index (@indexes) {
      unless ($index eq 'PRIMARY') {
        my $index_info = $self->bz_index_info_real($table, $index);
        ($index_info = $index_info->{FIELDS}) if (!$index_info->{TYPE});
        $schema->set_index($table, $index, $index_info);
      }
1146
    }
1147
  }
1148

1149
  return $schema;
1150
}
1151

1152
1;
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

=head1 B<Methods in need of POD>

=over

=item sql_date_format

=item bz_explain

=item bz_last_key

=item sql_position

=item sql_fulltext_search

=item sql_iposition

=item bz_enum_initial_values

=item sql_group_by

=item sql_limit

=item sql_not_regexp

=item sql_string_concat

=item sql_date_math

=item sql_to_days

=item bz_check_server_version

=item sql_from_days

=item sql_regexp

=item sql_istring

=item sql_group_concat

=item bz_setup_database

=item bz_db_is_utf8

=back