bzdbcopy.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env perl -w
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Everything Solved.
  16. # Portions created by Everything Solved are Copyright (C) 2006
  17. # Everything Solved. All Rights Reserved.
  18. #
  19. # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
  20. use strict;
  21. use lib qw(. lib);
  22. use Bugzilla;
  23. use Bugzilla::Constants;
  24. use Bugzilla::DB;
  25. use Bugzilla::Install::Util qw(indicate_progress);
  26. use Bugzilla::Util;
  27. #####################################################################
  28. # User-Configurable Settings
  29. #####################################################################
  30. # Settings for the 'Source' DB that you are copying from.
  31. use constant SOURCE_DB_TYPE => 'Mysql';
  32. use constant SOURCE_DB_NAME => 'bugs';
  33. use constant SOURCE_DB_USER => 'bugs';
  34. use constant SOURCE_DB_PASSWORD => '';
  35. use constant SOURCE_DB_HOST => 'localhost';
  36. # Settings for the 'Target' DB that you are copying to.
  37. use constant TARGET_DB_TYPE => 'Pg';
  38. use constant TARGET_DB_NAME => 'bugs';
  39. use constant TARGET_DB_USER => 'bugs';
  40. use constant TARGET_DB_PASSWORD => '';
  41. use constant TARGET_DB_HOST => 'localhost';
  42. #####################################################################
  43. # MAIN SCRIPT
  44. #####################################################################
  45. Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
  46. print "Connecting to the '" . SOURCE_DB_NAME . "' source database on "
  47. . SOURCE_DB_TYPE . "...\n";
  48. my $source_db = Bugzilla::DB::_connect(SOURCE_DB_TYPE, SOURCE_DB_HOST,
  49. SOURCE_DB_NAME, undef, undef, SOURCE_DB_USER, SOURCE_DB_PASSWORD);
  50. # Don't read entire tables into memory.
  51. if (SOURCE_DB_TYPE eq 'Mysql') {
  52. $source_db->{'mysql_use_result'}=1;
  53. # MySQL cannot have two queries running at the same time. Ensure the schema
  54. # is loaded from the database so bz_column_info will not execute a query
  55. $source_db->_bz_real_schema;
  56. }
  57. print "Connecting to the '" . TARGET_DB_NAME . "' target database on "
  58. . TARGET_DB_TYPE . "...\n";
  59. my $target_db = Bugzilla::DB::_connect(TARGET_DB_TYPE, TARGET_DB_HOST,
  60. TARGET_DB_NAME, undef, undef, TARGET_DB_USER, TARGET_DB_PASSWORD);
  61. my $ident_char = $target_db->get_info( 29 ); # SQL_IDENTIFIER_QUOTE_CHAR
  62. # We use the table list from the target DB, because if somebody
  63. # has customized their source DB, we still want the script to work,
  64. # and it may otherwise fail in that situation (that is, the tables
  65. # may not exist in the target DB).
  66. my @table_list = $target_db->bz_table_list_real();
  67. # We don't want to copy over the bz_schema table's contents.
  68. my $bz_schema_location = lsearch(\@table_list, 'bz_schema');
  69. splice(@table_list, $bz_schema_location, 1) if $bz_schema_location > 0;
  70. # Instead of figuring out some fancy algorithm to insert data in the right
  71. # order and not break FK integrity, we just drop them all.
  72. $target_db->bz_drop_foreign_keys();
  73. # We start a transaction on the target DB, which helps when we're doing
  74. # so many inserts.
  75. $target_db->bz_start_transaction();
  76. foreach my $table (@table_list) {
  77. my @serial_cols;
  78. print "Reading data from the source '$table' table on "
  79. . SOURCE_DB_TYPE . "...\n";
  80. my @table_columns = $target_db->bz_table_columns_real($table);
  81. # The column names could be quoted using the quote identifier char
  82. # Remove these chars as different databases use different quote chars
  83. @table_columns = map { s/^\Q$ident_char\E?(.*?)\Q$ident_char\E?$/$1/; $_ }
  84. @table_columns;
  85. my ($total) = $source_db->selectrow_array("SELECT COUNT(*) FROM $table");
  86. my $select_query = "SELECT " . join(',', @table_columns) . " FROM $table";
  87. my $select_sth = $source_db->prepare($select_query);
  88. $select_sth->execute();
  89. my $insert_query = "INSERT INTO $table ( " . join(',', @table_columns)
  90. . " ) VALUES (";
  91. $insert_query .= '?,' foreach (@table_columns);
  92. # Remove the last comma.
  93. chop($insert_query);
  94. $insert_query .= ")";
  95. my $insert_sth = $target_db->prepare($insert_query);
  96. print "Clearing out the target '$table' table on "
  97. . TARGET_DB_TYPE . "...\n";
  98. $target_db->do("DELETE FROM $table");
  99. # Oracle doesn't like us manually inserting into tables that have
  100. # auto-increment PKs set, because of the way we made auto-increment
  101. # fields work.
  102. if ($target_db->isa('Bugzilla::DB::Oracle')) {
  103. foreach my $column (@table_columns) {
  104. my $col_info = $source_db->bz_column_info($table, $column);
  105. if ($col_info && $col_info->{TYPE} =~ /SERIAL/i) {
  106. print "Dropping the sequence + trigger on $table.$column...\n";
  107. $target_db->do("DROP TRIGGER ${table}_${column}_TR");
  108. $target_db->do("DROP SEQUENCE ${table}_${column}_SEQ");
  109. }
  110. }
  111. }
  112. print "Writing data to the target '$table' table on "
  113. . TARGET_DB_TYPE . "...\n";
  114. my $count = 0;
  115. while (my $row = $select_sth->fetchrow_arrayref) {
  116. # Each column needs to be bound separately, because
  117. # many columns need to be dealt with specially.
  118. my $colnum = 0;
  119. foreach my $column (@table_columns) {
  120. # bind_param args start at 1, but arrays start at 0.
  121. my $param_num = $colnum + 1;
  122. my $already_bound;
  123. # Certain types of columns need special handling.
  124. my $col_info = $source_db->bz_column_info($table, $column);
  125. if ($col_info && $col_info->{TYPE} eq 'LONGBLOB') {
  126. $insert_sth->bind_param($param_num,
  127. $row->[$colnum], $target_db->BLOB_TYPE);
  128. $already_bound = 1;
  129. }
  130. elsif ($col_info && $col_info->{TYPE} =~ /decimal/) {
  131. # In MySQL, decimal cols can be too long.
  132. my $col_type = $col_info->{TYPE};
  133. $col_type =~ /decimal\((\d+),(\d+)\)/;
  134. my ($precision, $decimals) = ($1, $2);
  135. # If it's longer than precision + decimal point
  136. if ( length($row->[$colnum]) > ($precision + 1) ) {
  137. # Truncate it to the highest allowed value.
  138. my $orig_value = $row->[$colnum];
  139. $row->[$colnum] = '';
  140. my $non_decimal = $precision - $decimals;
  141. $row->[$colnum] .= '9' while ($non_decimal--);
  142. $row->[$colnum] .= '.';
  143. $row->[$colnum] .= '9' while ($decimals--);
  144. print "Truncated value $orig_value to " . $row->[$colnum]
  145. . " for $table.$column.\n";
  146. }
  147. }
  148. elsif ($col_info && $col_info->{TYPE} =~ /DATETIME/i) {
  149. my $date = $row->[$colnum];
  150. # MySQL can have strange invalid values for Datetimes.
  151. $row->[$colnum] = '1901-01-01 00:00:00'
  152. if $date && $date eq '0000-00-00 00:00:00';
  153. }
  154. $insert_sth->bind_param($param_num, $row->[$colnum])
  155. unless $already_bound;
  156. $colnum++;
  157. }
  158. $insert_sth->execute();
  159. $count++;
  160. indicate_progress({ current => $count, total => $total, every => 100 });
  161. }
  162. # For some DBs, we have to do clever things with auto-increment fields.
  163. foreach my $column (@table_columns) {
  164. next if $target_db->isa('Bugzilla::DB::Mysql');
  165. my $col_info = $source_db->bz_column_info($table, $column);
  166. if ($col_info && $col_info->{TYPE} =~ /SERIAL/i) {
  167. my ($max_val) = $target_db->selectrow_array(
  168. "SELECT MAX($column) FROM $table");
  169. # Set the sequence to the current max value + 1.
  170. $max_val = 0 if !defined $max_val;
  171. $max_val++;
  172. print "\nSetting the next value for $table.$column to $max_val.";
  173. if ($target_db->isa('Bugzilla::DB::Pg')) {
  174. # PostgreSQL doesn't like it when you insert values into
  175. # a serial field; it doesn't increment the counter
  176. # automatically.
  177. $target_db->do("SELECT pg_catalog.setval
  178. ('${table}_${column}_seq', $max_val, false)");
  179. }
  180. elsif ($target_db->isa('Bugzilla::DB::Oracle')) {
  181. # Oracle increments the counter on every insert, and *always*
  182. # sets the field, even if you gave it a value. So if there
  183. # were already rows in the target DB (like the default rows
  184. # created by checksetup), you'll get crazy values in your
  185. # id columns. So we just dropped the sequences above and
  186. # we re-create them here, starting with the right number.
  187. my @sql = $target_db->_bz_real_schema->_get_create_seq_ddl(
  188. $table, $column, $max_val);
  189. $target_db->do($_) foreach @sql;
  190. }
  191. }
  192. }
  193. print "\n\n";
  194. }
  195. print "Committing changes to the target database...\n";
  196. $target_db->bz_commit_transaction();
  197. $target_db->bz_setup_foreign_keys();
  198. print "All done! Make sure to run checksetup.pl on the new DB.\n";
  199. $source_db->disconnect;
  200. $target_db->disconnect;
  201. 1;
  202. __END__
  203. =head1 NAME
  204. bzdbcopy.pl - Copies data from one Bugzilla database to another.
  205. =head1 DESCRIPTION
  206. The intended use of this script is to copy data from an installation
  207. running on one DB platform to an installation running on another
  208. DB platform.
  209. It must be run from the directory containing your Bugzilla installation.
  210. That means if this script is in the contrib/ directory, you should
  211. be running it as: C<./contrib/bzdbcopy.pl>
  212. Note: Both schemas must already exist and be B<IDENTICAL>. (That is,
  213. they must have both been created/updated by the same version of
  214. checksetup.pl.) This script will B<DESTROY ALL CURRENT DATA> in the
  215. target database.
  216. Both Schemas must be at least from Bugzilla 2.19.3, but if you're
  217. running a Bugzilla from before 2.20rc2, you'll need the patch at:
  218. L<http://bugzilla.mozilla.org/show_bug.cgi?id=300311> in order to
  219. be able to run this script.
  220. Before you using it, you have to correctly set all the variables
  221. in the "User-Configurable Settings" section at the top of the script.
  222. The C<SOURCE> settings are for the database you're copying from, and
  223. the C<TARGET> settings are for the database you're copying to. The
  224. C<DB_TYPE> is the name of a DB driver from the F<Bugzilla/DB/> directory.