diff --git a/scripts/convert_db_to_v2 b/scripts/convert_db_to_v2 index efca034..98b1b27 100644 --- a/scripts/convert_db_to_v2 +++ b/scripts/convert_db_to_v2 @@ -24,7 +24,7 @@ my $ca = Simba::CA->new({ my $dbh = $ca->{dbh}; -$dbh->{mysql_use_result} = 1; # fetch row by row, not all at once +# $dbh->{mysql_use_result} = 1; # fetch row by row, not all at once $dbh->do(q{ create table versions2 ( @@ -60,7 +60,15 @@ $dbh->do(q{ ) ENGINE=MyISAM DEFAULT CHARSET=utf8; }); -my $sth_select = $dbh->prepare("select * from versions"); +# mysql doesn't support nested commands, +# so we need to read a chunk of data from versions, +# insert it into versions2 and instances, +# then read the next chunk ... + +my $chunk_size = 1_000_000; +my $max_version_id = $dbh->selectrow_array("select max(id) from versions"); + +my $sth_select = $dbh->prepare("select * from versions where id >= ? and id < ?"); my @version2_fields = qw( file_type @@ -96,24 +104,31 @@ my $sth_ins_instances session, version ) - values(?, ?, ?, ?, ?, ?) + values(?, ?, ?, ?, ?, ?, ?) } ); -$sth_select->execute(); my %versions2; -while(my $r = $sth_select->fetchrow_hashref) { - my $key = join($;, map((defined $_ ? $_ : ''), - @{$r}{@version2_fields} - ) - ); - my $version2_id; - if ($versions2{$key}) { - $version2_id = $versions2{$key}; - } else { - $sth_ins_versions2->execute(@{$r}{@version2_fields}); - $version2_id = $sth_ins_versions2->{mysql_insertid}; - $versions2{$key} = $version2_id; +for (my $version_id = 0; $version_id <= $max_version_id; $version_id += $chunk_size) { + print time - $^T, " ", $version_id, " ", scalar keys %versions2, "\n"; + $sth_select->execute($version_id, $version_id + $chunk_size); + while(my $r = $sth_select->fetchrow_hashref) { + my $key = join($;, map((defined $_ ? $_ : ''), + @{$r}{@version2_fields} + ) + ); + my $version2_id; + if ($versions2{$key}) { + $version2_id = $versions2{$key}; + } else { + $sth_ins_versions2->execute(@{$r}{@version2_fields}); + $version2_id = $sth_ins_versions2->{mysql_insertid}; + $versions2{$key} = $version2_id; + } + $sth_ins_instances->execute(@{$r}{qw(id file file_id date online session)}, $version2_id); } - $sth_ins_instances->execute(@{$r}{qw(id file file_id date online session)}, $version2_id); } +print time - $^T, " ", $max_version_id, " ", scalar keys %versions2, "\n"; + + +# vim: tw=132 expandtab sw=4 ts=8